Skip to content

Commit

Permalink
logger: add timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Apr 3, 2023
1 parent 4594ded commit 98b6996
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/config/global.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The global configuration file consists of the following settings:
* `file`: the file where controller logging is dumped (Default: `controller.log`)
* `console`: boolean indicating whether the logging should be dumped at console or not (Default: `false`)
* `level`: debugging level of the controller (Default: `INFO`)
* `timestamp`: adds timestamp to genereated logs (Default: `true`)

## Example

Expand Down
1 change: 1 addition & 0 deletions global.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ logging:
console: False
file: controller.log
level: INFO
timestamp: True
22 changes: 18 additions & 4 deletions sipssert/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import os
import time
import logging

slog = None # pylint: disable=invalid-name
Expand All @@ -46,15 +47,19 @@ class ColoredLogger(logging.Logger):

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
FORMAT = "$BOLD%(levelname)s$RESET: %(message)s"
COLOR_FORMAT = formatter_message(FORMAT, True)

def __init__(self, name):
global LOG_CONSOLE # pylint: disable=global-statement
global LOG_FILE # pylint: disable=global-statement
global LOG_TIMESTAMP # pylint: disable=global-statement

logging.Logger.__init__(self, name)
color_formatter = ColoredFormatter(self.COLOR_FORMAT)
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'):
Expand Down Expand Up @@ -100,8 +105,8 @@ class ColoredFormatter(logging.Formatter):
'ERROR': ColoredLogger.RED
}

def __init__(self, msg, use_color = True):
logging.Formatter.__init__(self, msg)
def __init__(self, msg, time_format, use_color = True):
logging.Formatter.__init__(self, msg, time_format)
self.use_color = use_color

def format(self, record):
Expand All @@ -112,14 +117,22 @@ def format(self, record):
record.levelname = levelname_color
return logging.Formatter.format(self, record)

def formatTime(self, record, datefmt=ColoredLogger.TIME_FORMAT):
ct = self.converter(record.created)
t = time.strftime(datefmt, ct)
s = "%s.%03d" % (t, record.msecs)
return s

def init_logger(config, logs_dir=None):
"""Initializes the logger according to config"""
global slog # pylint: disable=global-statement,invalid-name
global LOG_CONSOLE # pylint: disable=global-statement
global LOG_LEVEL # pylint: disable=global-statement
global LOG_FILE # pylint: disable=global-statement
global LOG_TIMESTAMP # pylint: disable=global-statement

LOG_CONSOLE = config.get("console", DEFAULT_LOG_CONSOLE) if config else DEFAULT_LOG_CONSOLE
LOG_TIMESTAMP = config.get("timestamp", DEFAULT_LOG_TIMESTAMP) if config else DEFAULT_LOG_TIMESTAMP
log_file = config.get("file", DEFAULT_LOG_FILE) if config else DEFAULT_LOG_FILE
LOG_LEVEL = config.get("level", DEFAULT_LOG_LEVEL) if config else DEFAULT_LOG_LEVEL

Expand All @@ -134,5 +147,6 @@ def init_logger(config, logs_dir=None):
DEFAULT_LOG_FILE = "controller.log"
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_LOG_CONSOLE = False
DEFAULT_LOG_TIMESTAMP = True

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

0 comments on commit 98b6996

Please sign in to comment.