From bfe065583a8194e184df1402ce4834c12bf8fa9b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Apr 2020 10:17:40 -0700 Subject: [PATCH] Add alternative logging module --- amanuensis/log/__init__.py | 5 +++++ amanuensis/log/setup.py | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 amanuensis/log/__init__.py create mode 100644 amanuensis/log/setup.py diff --git a/amanuensis/log/__init__.py b/amanuensis/log/__init__.py new file mode 100644 index 0000000..fb04046 --- /dev/null +++ b/amanuensis/log/__init__.py @@ -0,0 +1,5 @@ +from amanuensis.log.setup import init_logging + +__all__ = [member.__name__ for member in [ + init_logging +]] diff --git a/amanuensis/log/setup.py b/amanuensis/log/setup.py new file mode 100644 index 0000000..bd58115 --- /dev/null +++ b/amanuensis/log/setup.py @@ -0,0 +1,42 @@ +import logging +import logging.handlers + + +basic_formatter = logging.Formatter( + fmt='[{levelname}] {message}', + style='{') +detailed_formatter = logging.Formatter( + fmt='[{asctime}] [{levelname}:{filename}:{lineno}] {message}', + style='{') +basic_console_handler = logging.StreamHandler() +basic_console_handler.setLevel(logging.INFO) +basic_console_handler.setFormatter(basic_formatter) +detailed_console_handler = logging.StreamHandler() +detailed_console_handler.setLevel(logging.DEBUG) +detailed_console_handler.setFormatter(detailed_formatter) + + +def get_file_handler(filename: str) -> logging.Handler: + handler = logging.handlers.RotatingFileHandler( + filename=filename, + maxBytes=1000000, + backupCount=10, + delay=True, + encoding='utf8', + ) + handler.setLevel(logging.DEBUG) + handler.setFormatter(detailed_formatter) + return handler + + +def init_logging(verbose: bool, log_filename: str): + """ + Initializes the Amanuensis logger settings + """ + logger = logging.getLogger("amanuensis") + if log_filename: + logger.addHandler(get_file_handler(log_filename)) + elif verbose: + logger.addHandler(detailed_console_handler) + else: + logger.addHandler(basic_console_handler)