Skip to content

Commit

Permalink
logger_utils: Refactor formatter_insert
Browse files Browse the repository at this point in the history
  • Loading branch information
deajan committed Nov 7, 2022
1 parent d9d55f7 commit 73dadca
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ofunctions/logger_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Logging functions ########################################################

FORMATTER = logging.Formatter("%(asctime)s :: %(levelname)s :: ##OPTINAL_STRING##%(message)s")
FORMATTER = "%(asctime)s :: %(levelname)s :: ##OPTINAL_STRING##%(message)s"


class FixPython2Logging(logging.Filter):
Expand Down Expand Up @@ -98,18 +98,23 @@ def filter(self, record):
return True


def get_logger_formatter(formatter_insert=None):
# type: (Optional[str]) -> logging.Formatter
if formatter_insert:
return logging.Formatter(FORMATTER.replace('##OPTINAL_STRING##', '{} :: '.format(formatter_insert)))
else:
return logging.Formatter(FORMATTER.replace('##OPTINAL_STRING##', ""))



def logger_get_console_handler(
formatter_insert=None,
):
# type: (Optional[str]) -> Union[logging.StreamHandler, None]
"""
Returns a console handler that outputs as UTF-8 regardless of the platform
"""
if formatter_insert:
formatter = FORMATTER.replace('##OPTINAL_STRING##', formatter_insert + ' :: ')
else:
formatter = FORMATTER.replace('##OPTINAL_STRING##', "")

formatter = get_logger_formatter(formatter_insert)
# When Nuitka compiled under Windows, calls to subshells are opened as cp850 / other system locale
# This behavior makes logging popen output to stdout/stderr fail
# Let's force stdout and stderr to always be utf-8
Expand Down Expand Up @@ -152,10 +157,7 @@ def logger_get_file_handler(log_file, formatter_insert=None):
Returns a log file handler
On failire, will return a temporary file log handler
"""
if formatter_insert:
formatter = FORMATTER.replace('##OPTINAL_STRING##', formatter_insert + ' :: ')
else:
formatter = FORMATTER.replace('##OPTINAL_STRING##', "")
formatter = get_logger_formatter(formatter_insert)
err_output = None
try:
file_handler = RotatingFileHandler(
Expand Down

0 comments on commit 73dadca

Please sign in to comment.