From a7aaa31bff37fe969fa27933b79d7978f003c165 Mon Sep 17 00:00:00 2001 From: JoeZiminski Date: Mon, 29 Apr 2024 21:39:44 +0100 Subject: [PATCH 1/5] Add logger name and test. --- fancylog/fancylog.py | 15 +++++++++++++-- tests/tests/test_general.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/fancylog/fancylog.py b/fancylog/fancylog.py index 10ab091..ca35eef 100644 --- a/fancylog/fancylog.py +++ b/fancylog/fancylog.py @@ -37,6 +37,7 @@ def start_logging( log_to_file=True, log_to_console=True, timestamp=True, + logger_name=False, ): """Prepares the log file, and then begins logging. @@ -59,8 +60,10 @@ def start_logging( Default: True :param log_to_file: If True, write a log file, otherwise just print to terminal. - :param timestamp: If True, add a timestamp to the filename :param log_to_console: Print logs to the console or not: Default: True + :param timestamp: If True, add a timestamp to the filename + :param logger_name: If False, logger uses default logger. Otherwise, + logger name is set to `logger_name`. :return: Path to the logging file# """ output_dir = str(output_dir) @@ -98,6 +101,7 @@ def start_logging( file_level=file_log_level, multiprocessing_aware=multiprocessing_aware, log_to_console=log_to_console, + logger_name=logger_name, ) return logging_file @@ -225,6 +229,7 @@ def initialise_logger( print_level="INFO", file_level="DEBUG", log_to_console=True, + logger_name=False, ): """Sets up (possibly multiprocessing aware) logging. :param filename: Where to save the logs to @@ -234,7 +239,11 @@ def initialise_logger( Default: 'DEBUG' :param log_to_console: Print logs to the console or not """ - logger = logging.getLogger() + if logger_name: + logger = logging.getLogger(logger_name) + else: + logger = logging.getLogger() + logger.setLevel(getattr(logging, file_level)) formatter = logging.Formatter( @@ -265,6 +274,7 @@ def setup_logging( file_level="DEBUG", multiprocessing_aware=True, log_to_console=True, + logger_name=False, ): """Sets up (possibly multiprocessing aware) logging. :param filename: Where to save the logs to @@ -282,6 +292,7 @@ def setup_logging( print_level=print_level, file_level=file_level, log_to_console=log_to_console, + logger_name=logger_name, ) if multiprocessing_aware: try: diff --git a/tests/tests/test_general.py b/tests/tests/test_general.py index 0669630..f2a256c 100644 --- a/tests/tests/test_general.py +++ b/tests/tests/test_general.py @@ -1,4 +1,5 @@ import fancylog +import logging lateral_separator = "**************" @@ -20,3 +21,21 @@ def test_start_logging(tmp_path): assert lines[3].startswith("Output directory: ") assert lines[4].startswith("Current directory: ") assert lines[5].startswith("Version: ") + + +def test_logger_name(tmp_path): + """ + Quick check that expecter logger name is created + when specified. + """ + logger_name = "hello_world" + + # Logger name should not already exist + assert logger_name not in logging.root.manager.loggerDict.keys() + + # Logger name should be created + fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name) + + assert logger_name in logging.root.manager.loggerDict.keys() + + From 1ee8d8ceddd4e21d1615a7cace1e6035d09161d9 Mon Sep 17 00:00:00 2001 From: JoeZiminski Date: Mon, 29 Apr 2024 21:45:38 +0100 Subject: [PATCH 2/5] Run pre-commit. --- tests/tests/test_general.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/tests/test_general.py b/tests/tests/test_general.py index f2a256c..c154029 100644 --- a/tests/tests/test_general.py +++ b/tests/tests/test_general.py @@ -1,6 +1,7 @@ -import fancylog import logging +import fancylog + lateral_separator = "**************" @@ -31,11 +32,9 @@ def test_logger_name(tmp_path): logger_name = "hello_world" # Logger name should not already exist - assert logger_name not in logging.root.manager.loggerDict.keys() + assert logger_name not in logging.root.manager.loggerDict # Logger name should be created fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name) - assert logger_name in logging.root.manager.loggerDict.keys() - - + assert logger_name in logging.root.manager.loggerDict From 5ee2520b497a0437cba5fc1a1f5ec064ed3fe28d Mon Sep 17 00:00:00 2001 From: JoeZiminski Date: Tue, 30 Apr 2024 16:55:37 +0100 Subject: [PATCH 3/5] Change 'logger_name' default from 'False' to 'None'. --- fancylog/fancylog.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/fancylog/fancylog.py b/fancylog/fancylog.py index ca35eef..f76420c 100644 --- a/fancylog/fancylog.py +++ b/fancylog/fancylog.py @@ -37,7 +37,7 @@ def start_logging( log_to_file=True, log_to_console=True, timestamp=True, - logger_name=False, + logger_name=None, ): """Prepares the log file, and then begins logging. @@ -62,7 +62,7 @@ def start_logging( terminal. :param log_to_console: Print logs to the console or not: Default: True :param timestamp: If True, add a timestamp to the filename - :param logger_name: If False, logger uses default logger. Otherwise, + :param logger_name: If None, logger uses default logger. Otherwise, logger name is set to `logger_name`. :return: Path to the logging file# """ @@ -229,7 +229,7 @@ def initialise_logger( print_level="INFO", file_level="DEBUG", log_to_console=True, - logger_name=False, + logger_name=None, ): """Sets up (possibly multiprocessing aware) logging. :param filename: Where to save the logs to @@ -238,6 +238,8 @@ def initialise_logger( :param file_level: What level of logging to print to file. Default: 'DEBUG' :param log_to_console: Print logs to the console or not + :param logger_name: If None, logger uses default logger. Otherwise, + logger name is set to `logger_name`. """ if logger_name: logger = logging.getLogger(logger_name) @@ -274,7 +276,7 @@ def setup_logging( file_level="DEBUG", multiprocessing_aware=True, log_to_console=True, - logger_name=False, + logger_name=None, ): """Sets up (possibly multiprocessing aware) logging. :param filename: Where to save the logs to @@ -285,7 +287,8 @@ def setup_logging( :param multiprocessing_aware: Default: True :param log_to_console: Print logs to the console or no. Default: True - + :param logger_name: If None, logger uses default logger. Otherwise, + logger name is set to `logger_name`. """ initialise_logger( filename, From 60718f8b78f48498b2d54f2f02c197c682cb657a Mon Sep 17 00:00:00 2001 From: JoeZiminski Date: Tue, 30 Apr 2024 16:58:25 +0100 Subject: [PATCH 4/5] Add 'logger_name' to the example. --- example.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/example.py b/example.py index 136b659..4d33983 100644 --- a/example.py +++ b/example.py @@ -30,11 +30,14 @@ def main(directory): variables=[args, args.paths], verbose=verbose, timestamp=True, + logger_name="my_logger" ) - logging.info("This is an info message") - logging.debug("This is a debug message") - logging.warning("This fun logging experience is about to end :(") + logger = logging.getLogger("my_logger") + + logger.info("This is an info message") + logger.debug("This is a debug message") + logger.warning("This fun logging experience is about to end :(") if __name__ == "__main__": From f0db84e0fe4f32a58aa4ee3925c9ea3671a35332 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 30 Apr 2024 15:59:31 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index 4d33983..c6ff0f2 100644 --- a/example.py +++ b/example.py @@ -30,7 +30,7 @@ def main(directory): variables=[args, args.paths], verbose=verbose, timestamp=True, - logger_name="my_logger" + logger_name="my_logger", ) logger = logging.getLogger("my_logger")