From ea8cf5de961d8bf7d2c915cbfad66a25a12a627e Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Wed, 13 Dec 2023 21:06:43 +0100 Subject: [PATCH 1/2] fix: Make console handler work on Python 3.8, tested in container --- core/opl/skelet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/opl/skelet.py b/core/opl/skelet.py index 1da2173..6351f0a 100644 --- a/core/opl/skelet.py +++ b/core/opl/skelet.py @@ -32,7 +32,7 @@ def setup_logger(app_name, stderr_log_lvl): console = logging.StreamHandler() console.setFormatter(formatter) console.setLevel(stderr_log_lvl) - logging.getLogger(app_name).addHandler(console) + logging.getLogger().addHandler(console) # Add file rotating handler, with level DEBUG rotating_handler = logging.handlers.RotatingFileHandler( From 1319c45011f0cf6a4fab0c2543fae4e07f2f5654 Mon Sep 17 00:00:00 2001 From: Jan Hutar Date: Wed, 13 Dec 2023 21:12:05 +0100 Subject: [PATCH 2/2] fix: Make the logging setup bit more readable --- core/opl/skelet.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/core/opl/skelet.py b/core/opl/skelet.py index 6351f0a..404d1ce 100644 --- a/core/opl/skelet.py +++ b/core/opl/skelet.py @@ -9,11 +9,11 @@ def setup_logger(app_name, stderr_log_lvl): """ - Create logger that logs to both stderr and log file but with different log level + Create logger that logs to both stderr and log file but with different log levels """ # Remove all handlers from root logger if any - logging.basicConfig(level=logging.NOTSET, handlers=[]) - # Change root logger level from WARNING (default) to NOTSET in order for all messages to be delegated. + logging.basicConfig(level=logging.NOTSET, handlers=[]) # `force=True` was added in Python 3.8 :-( + # Change root logger level from WARNING (default) to NOTSET in order for all messages to be delegated logging.getLogger().setLevel(logging.NOTSET) # Log message format @@ -22,30 +22,30 @@ def setup_logger(app_name, stderr_log_lvl): ) formatter.converter = time.gmtime - # Configure loggers of libraries we use + # Silence loggers of some chatty libraries we use urllib_logger = logging.getLogger("urllib3.connectionpool") urllib_logger.setLevel(stderr_log_lvl) selenium_logger = logging.getLogger("selenium.webdriver.remote.remote_connection") selenium_logger.setLevel(stderr_log_lvl) - # Add stderr handler, with level INFO - console = logging.StreamHandler() - console.setFormatter(formatter) - console.setLevel(stderr_log_lvl) - logging.getLogger().addHandler(console) + # Add stderr handler, with provided level + console_handler = logging.StreamHandler() + console_handler.setFormatter(formatter) + console_handler.setLevel(stderr_log_lvl) + logging.getLogger().addHandler(console_handler) # Add file rotating handler, with level DEBUG rotating_handler = logging.handlers.RotatingFileHandler( filename=f"/tmp/{app_name}.log", maxBytes=100 * 1000, backupCount=2 ) - rotating_handler.setLevel(logging.DEBUG) rotating_handler.setFormatter(formatter) + rotating_handler.setLevel(logging.DEBUG) logging.getLogger().addHandler(rotating_handler) return logging.getLogger(app_name) @contextmanager -def test_setup(parser, logger_name=None): +def test_setup(parser, logger_name="root"): parser.add_argument( "--status-data-file", default=os.getenv("STATUS_DATA_FILE", "/tmp/status-data.json"), @@ -63,9 +63,6 @@ def test_setup(parser, logger_name=None): ) args = parser.parse_args() - if logger_name is None: - logger_name = "root" - if args.debug: logger = setup_logger(logger_name, logging.DEBUG) elif args.verbose: