From 131d96bd2b952eae912843655deb6a3db5a2e563 Mon Sep 17 00:00:00 2001 From: Antoine van Gelder Date: Tue, 23 Jan 2024 15:28:27 +0200 Subject: [PATCH] Move facedancer logging to a sub-logger of the python root Issue originally reported in #51 Initial pull-request in #52 Closes #51 Closes #52 --- facedancer/logging.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 facedancer/logging.py diff --git a/facedancer/logging.py b/facedancer/logging.py new file mode 100644 index 0000000..9f6192f --- /dev/null +++ b/facedancer/logging.py @@ -0,0 +1,36 @@ +import functools +import logging +import sys + + +LOGLEVEL_TRACE = logging.DEBUG + 2 + +LOG_FORMAT_COLOR = "\u001b[37;1m%(levelname)-8s| \u001b[0m\u001b[1m%(module)-15s|\u001b[0m %(message)s" +LOG_FORMAT_PLAIN = "%(levelname)-8s| %(module)-15s| %(message)s" + + +def configure_default_logging(level=logging.INFO, logger=logging): + if sys.stdout.isatty(): + log_format = LOG_FORMAT_COLOR + else: + log_format = LOG_FORMAT_PLAIN + + logger.basicConfig(level=level, format=log_format) + logging.getLogger("facedancer").level = level + + +def _initialize_logging(): + # add a TRACE level to logging + logging.TRACE = LOGLEVEL_TRACE + logging.addLevelName(logging.TRACE, "TRACE") + logging.Logger.trace = functools.partialmethod(logging.Logger.log, logging.TRACE) + logging.trace = functools.partial(logging.log, logging.TRACE) + + # Configure facedancer logger + logger = logging.getLogger("facedancer") + logger.level = logging.WARN + + return logger + + +log = _initialize_logging()