Skip to content

Commit

Permalink
logger: fix console writing
Browse files Browse the repository at this point in the history
Add formatter to the console writing, especially for timestamps
  • Loading branch information
razvancrainea committed Mar 21, 2024
1 parent 59d2814 commit 81f2b14
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions sipssert/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,19 @@ def __init__(self, name):
logging.Logger.__init__(self, name)
if LOG_TIMESTAMP:
self.FORMAT = "%(asctime)s " + self.FORMAT
color_format = formatter_message(self.FORMAT, True)
color_formatter = ColoredFormatter(color_format, self.TIME_FORMAT)
def build_handler_filters(handler: str):
def handler_filter(record: logging.LogRecord):
if hasattr(record, 'block'):
if record.block == handler:
return False
return True
return handler_filter
if LOG_CONSOLE:
console = logging.StreamHandler()
console.addFilter(build_handler_filters('console'))
console.setFormatter(color_formatter)
self.addHandler(console)

file_format = formatter_message(self.FORMAT, False)
file_formatter = ColoredFormatter(file_format, self.TIME_FORMAT, False)
file_handler = logging.FileHandler(LOG_FILE)
file_handler.addFilter(build_handler_filters('file'))
file_handler.setFormatter(file_formatter)
self.addHandler(file_handler)

def color(self, color, message): # pylint: disable=no-self-use
"""returns the message coloured appropriately"""
return COLOR_SEQ % (30 + color) + message + RESET_SEQ
if LOG_CONSOLE:
color_format = formatter_message(self.FORMAT, True)
color_formatter = ColoredFormatter(color_format, self.TIME_FORMAT)
console = logging.StreamHandler()
console.setFormatter(color_formatter)
self.addHandler(console)

class IdenfierAdapter(logging.LoggerAdapter):

Expand Down

0 comments on commit 81f2b14

Please sign in to comment.