From 7d10f9aa581462847b4355f4a59e879aa646207b Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 11 Sep 2024 09:38:36 +0100 Subject: [PATCH 1/4] Simplify usage of logging in main script There is no need to call logging.getLogger("codebasin") each time we use the logger; we can adopt the convention used by other parts of the code. Signed-off-by: John Pennycook --- codebasin/__main__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codebasin/__main__.py b/codebasin/__main__.py index 872b933..cad1f03 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -13,6 +13,7 @@ from codebasin import CodeBase, config, finder, report, util from codebasin.walkers.platform_mapper import PlatformMapper +log = logging.getLogger("codebasin") version = "1.2.0" @@ -146,8 +147,8 @@ def main(): stdout_log = logging.StreamHandler(sys.stdout) stdout_log.setFormatter(logging.Formatter("[%(levelname)-8s] %(message)s")) - logging.getLogger("codebasin").addHandler(stdout_log) - logging.getLogger("codebasin").setLevel( + log.addHandler(stdout_log) + log.setLevel( max(1, logging.WARNING - 10 * (args.verbose - args.quiet)), ) @@ -243,5 +244,5 @@ def report_enabled(name): sys.argv[0] = "codebasin" main() except Exception as e: - logging.getLogger("codebasin").error(str(e)) + log.error(str(e)) sys.exit(1) From 296ea500f1cff662534e2af70d2ccb05221724a0 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 11 Sep 2024 09:53:32 +0100 Subject: [PATCH 2/4] Adjust logging configuration - All messages are written to a log file ("cbi.log") - Only errors are written to the terminal by default - Messages written to terminal are based on -q and -v flags Signed-off-by: John Pennycook --- codebasin/__main__.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/codebasin/__main__.py b/codebasin/__main__.py index cad1f03..c0a0b5b 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -145,12 +145,25 @@ def main(): args = parser.parse_args() - stdout_log = logging.StreamHandler(sys.stdout) - stdout_log.setFormatter(logging.Formatter("[%(levelname)-8s] %(message)s")) - log.addHandler(stdout_log) - log.setLevel( - max(1, logging.WARNING - 10 * (args.verbose - args.quiet)), - ) + # Configure logging such that: + # - All messages are written to a log file + # - Only errors are written to the terminal by default + # - Messages written to terminal are based on -q and -v flags + formatter = logging.Formatter("[%(levelname)-8s] %(message)s") + log.setLevel(logging.DEBUG) + + file_handler = logging.FileHandler("cbi.log", mode="w") + file_handler.setLevel(logging.INFO) + file_handler.setFormatter(formatter) + log.addHandler(file_handler) + log_path = os.path.realpath("cbi.log") + print(f"Log file created at {log_path}") + + log_level = max(1, logging.ERROR - 10 * (args.verbose - args.quiet)) + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setLevel(log_level) + stdout_handler.setFormatter(formatter) + log.addHandler(stdout_handler) # If no specific report was specified, generate all reports. # Handled here to prevent "all" always being in the list. From 6c055b3e31e5ee456e6f2ee1167999492cd38c2e Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 25 Sep 2024 14:06:18 +0100 Subject: [PATCH 3/4] Use abspath to print cbi.log location Signed-off-by: John Pennycook --- codebasin/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codebasin/__main__.py b/codebasin/__main__.py index c0a0b5b..e4fa49d 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -156,7 +156,7 @@ def main(): file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) log.addHandler(file_handler) - log_path = os.path.realpath("cbi.log") + log_path = os.path.abspath("cbi.log") print(f"Log file created at {log_path}") log_level = max(1, logging.ERROR - 10 * (args.verbose - args.quiet)) From 326fa8e6da84a9aec99dca9215fe1702fd5c53b5 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 25 Sep 2024 16:06:22 +0100 Subject: [PATCH 4/4] Add comment explaining usage of 'print' vs 'log' Signed-off-by: John Pennycook --- codebasin/__main__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/codebasin/__main__.py b/codebasin/__main__.py index e4fa49d..bd6fa18 100755 --- a/codebasin/__main__.py +++ b/codebasin/__main__.py @@ -156,6 +156,9 @@ def main(): file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) log.addHandler(file_handler) + + # Inform the user that a log file has been created. + # 'print' instead of 'log' to ensure the message is visible in the output. log_path = os.path.abspath("cbi.log") print(f"Log file created at {log_path}")