Skip to content

Commit

Permalink
add global settings to catching errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zlElo committed Apr 3, 2024
1 parent 47fac44 commit e88b7b4
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions froggius/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def debug(self, log_msg, file_path=None, print_out=None):
log_string = f'[DBG] [{current_date.strftime("%d/%m/%Y %H:%M:%S")}] {log_msg}'

# check for vars for filepath
if self.glob_file_path is not None:
if self.glob_file_path is not None and file_path is None:
file_path = self.glob_file_path

if file_path is not None:
Expand Down Expand Up @@ -87,7 +87,7 @@ def error(self, log_msg, file_path=None, highlighting=True, print_out=None, line
log_string = f'[ERR] [{current_date.strftime("%d/%m/%Y %H:%M:%S")}] {log_msg} {f"| Occured on line: {line[0]} in {line[1]}, {line[2]}()" if line is not None else ""}'

# check for filepath
if self.glob_file_path is not None:
if self.glob_file_path is not None and file_path is None:
file_path = self.glob_file_path

if file_path is not None:
Expand All @@ -104,8 +104,8 @@ def error(self, log_msg, file_path=None, highlighting=True, print_out=None, line
if self.glob_print_out is None and print_out is None:
print(log_string, file=sys.stderr)

@staticmethod
def catch(file_path=None, continue_onexpception=True):
# @staticmethod
def catch(self, file_path=None, continue_onexpception=True, print_out=None):
"""
A decorator that catches exceptions and logs them with LogMx.error
Expand All @@ -120,6 +120,7 @@ def catch(file_path=None, continue_onexpception=True):
"""
def decorator(func):
def wrapper(*args, **kwargs):
file_path = None
try:
return func(*args, **kwargs)
except Exception as e:
Expand All @@ -128,9 +129,21 @@ def wrapper(*args, **kwargs):
line = traceback_info[-1][1]
file = traceback_info[-1][0]
function_name = traceback_info[-1][2]

errorinstance = Froggius()
errorinstance.error(log_msg=str(e), highlighting=True, print_out=True, line=[line, file, function_name], file_path=file_path)
highliting_loc = None

if self.glob_file_path is not None and file_path is None:
file_path = self.glob_file_path
if self.glob_print_out:
if print_out == False:
pass
else:
highliting_loc = True
if print_out and not self.glob_print_out:
highliting_loc = True
if self.glob_print_out is None and print_out is None:
highliting_loc = True

self.error(log_msg=str(e), highlighting=highliting_loc, print_out=print_out, line=[line, file, function_name], file_path=file_path)

if not continue_onexpception:
raise e
Expand All @@ -156,7 +169,7 @@ def information(self, log_msg, file_path=None, highlighting=True, print_out=None
log_string = f'[INF] [{current_date.strftime("%d/%m/%Y %H:%M:%S")}] {log_msg}'

# check for filepath
if self.glob_file_path is not None:
if self.glob_file_path is not None and file_path is None:
file_path = self.glob_file_path

if file_path is not None:
Expand Down Expand Up @@ -194,7 +207,7 @@ def warning(self, log_msg, file_path=None, highlighting=True, print_out=None):
log_string = f'[WRN] [{current_date.strftime("%d/%m/%Y %H:%M:%S")}] {log_msg}'

# check for filepath
if self.glob_file_path is not None:
if self.glob_file_path is not None and file_path is None:
file_path = self.glob_file_path

if file_path is not None:
Expand Down

0 comments on commit e88b7b4

Please sign in to comment.