Skip to content

Commit

Permalink
python: Implemented saving a log file with all messages independently…
Browse files Browse the repository at this point in the history
… of how logger levels are set.
  • Loading branch information
levy committed Nov 19, 2024
1 parent cecc83a commit 37cc2e1
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions python/inet/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

__sphinx_mock__ = True # ignore this module in documentation

_logger = logging.getLogger(__name__)

COLOR_GRAY = "\033[38;20m"
COLOR_RED = "\033[1;31m"
COLOR_YELLOW = "\033[1;33m"
Expand All @@ -33,10 +31,52 @@ def enable_autoreload():
ipython.magic("load_ext autoreload")
ipython.magic("autoreload 2")

_file_handler = None

class LocalLogger(logging.Logger):
def stdout(self, message, *args, **kwargs):
self._log(STDOUT_LEVEL, message, args, **kwargs)

def stderr(self, message, *args, **kwargs):
self._log(STDERR_LEVEL, message, args, **kwargs)

def debug(self, msg, *args, **kwargs):
self._log(logging.DEBUG, msg, args, **kwargs)

def info(self, msg, *args, **kwargs):
self._log(logging.INFO, msg, args, **kwargs)

def warning(self, msg, *args, **kwargs):
self._log(logging.WARNING, msg, args, **kwargs)

def error(self, msg, *args, **kwargs):
self._log(logging.ERROR, msg, args, **kwargs)

def critical(self, msg, *args, **kwargs):
self._log(logging.CRITICAL, msg, args, **kwargs)

def log(self, level, msg, *args, **kwargs):
self._log(level, msg, args, **kwargs)

def handle(self, record):
global _file_handler
if _file_handler:
_file_handler.handle(record)
if self.isEnabledFor(record.levelno):
super().handle(record)

logging.setLoggerClass(LocalLogger)

_logger = logging.getLogger(__name__)

_logging_initialized = False

def initialize_logging(log_level, external_command_log_level, log_file):
global _logging_initialized
global _file_handler, _logging_initialized
if log_file:
_file_handler = logging.FileHandler(log_file, mode="w")
_file_handler.setLevel(logging.DEBUG)
_file_handler.setFormatter(logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'))
formatter = ColoredLoggingFormatter()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
Expand All @@ -54,14 +94,6 @@ def initialize_logging(log_level, external_command_log_level, log_file):
logging.getLogger("opp_test").setLevel(external_command_log_level)
logging.addLevelName(STDOUT_LEVEL, "STDOUT")
logging.addLevelName(STDERR_LEVEL, "STDERR")
def stdout(self, message, *args, **kwargs):
if self.isEnabledFor(STDOUT_LEVEL):
self._log(STDOUT_LEVEL, message, args, **kwargs)
def stderr(self, message, *args, **kwargs):
if self.isEnabledFor(STDERR_LEVEL):
self._log(STDERR_LEVEL, message, args, **kwargs)
logging.Logger.stdout = stdout
logging.Logger.stderr = stderr
_logging_initialized = True

def ensure_logging_initialized(log_level, external_command_log_level, log_file):
Expand Down

0 comments on commit 37cc2e1

Please sign in to comment.