From 8b0073d1c8f4ecdc4d6f6445924151c279cad7c9 Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Sun, 15 Sep 2024 09:47:41 -0400 Subject: [PATCH 1/4] Add CLI arguments for enabling the development logger and setting log level --- CHANGELOG.md | 1 + src/basilisp/cli.py | 26 ++++++++++++++++++++++++++ src/basilisp/logconfig.py | 28 +++++++++++++++------------- src/basilisp/main.py | 11 +++++++---- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95555ce86..089277aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Added the `-p`/`--include-path` CLI command to prepend entries to the `sys.path` as an alternative to `PYTHONPATH` (#1027) * Added an empty entry to `sys.path` for all CLI entrypoints (`basilisp run`, `basilisp repl`, etc.) (#1027) + * Added command line arguments for enabling the development logger and configuring the logging level (#???) ### Changed * The compiler will no longer require `Var` indirection for top-level `do` forms unless those forms specify `^:use-var-indirection` metadata (which currently is only used in the `ns` macro) (#1034) diff --git a/src/basilisp/cli.py b/src/basilisp/cli.py index 48bfb8583..b2ee352de 100644 --- a/src/basilisp/cli.py +++ b/src/basilisp/cli.py @@ -281,6 +281,32 @@ def _add_debug_arg_group(parser: argparse.ArgumentParser) -> None: "(env: BASILISP_DO_NOT_CACHE_NAMESPACES; default: false)" ), ) + group.add_argument( + "--enable-logger", + action=_set_envvar_action( + "BASILISP_USE_DEV_LOGGER", parent=argparse._StoreAction + ), + nargs="?", + const=True, + type=_to_bool, + help=( + "if true, enable the Basilisp root logger " + "(env: BASILISP_USE_DEV_LOGGER; default: false)" + ), + ) + group.add_argument( + "-l", + "--log-level", + action=_set_envvar_action( + "BASILISP_LOGGING_LEVEL", parent=argparse._StoreAction + ), + type=lambda s: s.upper(), + default="WARNING", + help=( + "the logging level for logs emitted by the Basilisp compiler and runtime " + "(env: BASILISP_LOGGING_LEVEL; default: WARNING)" + ), + ) def _add_import_arg_group(parser: argparse.ArgumentParser) -> None: diff --git a/src/basilisp/logconfig.py b/src/basilisp/logconfig.py index bc5104daf..56b7191c3 100644 --- a/src/basilisp/logconfig.py +++ b/src/basilisp/logconfig.py @@ -1,5 +1,16 @@ import logging import os +from typing import Optional + + +TRACE = 5 + +logging.addLevelName(TRACE, "TRACE") + + +DEFAULT_FORMAT = ( + "%(asctime)s %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] - %(message)s" +) def get_level() -> str: @@ -7,7 +18,9 @@ def get_level() -> str: return os.getenv("BASILISP_LOGGING_LEVEL", "WARNING") -def get_handler(level: str, fmt: str) -> logging.Handler: +def get_handler( + level: Optional[str] = None, fmt: str = DEFAULT_FORMAT +) -> logging.Handler: """Get the default logging handler for Basilisp.""" handler = ( logging.StreamHandler() @@ -15,16 +28,5 @@ def get_handler(level: str, fmt: str) -> logging.Handler: else logging.NullHandler() ) handler.setFormatter(logging.Formatter(fmt)) - handler.setLevel(level) + handler.setLevel(level or get_level()) return handler - - -TRACE = 5 - -logging.addLevelName(TRACE, "TRACE") - -DEFAULT_FORMAT = ( - "%(asctime)s %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] - %(message)s" -) -DEFAULT_LEVEL = get_level() -DEFAULT_HANDLER = get_handler(DEFAULT_LEVEL, DEFAULT_FORMAT) diff --git a/src/basilisp/main.py b/src/basilisp/main.py index 7cfce8d4c..e6ff2e00c 100644 --- a/src/basilisp/main.py +++ b/src/basilisp/main.py @@ -5,15 +5,17 @@ from typing import List, Optional from basilisp import importer as importer +from basilisp import logconfig from basilisp.lang import runtime as runtime from basilisp.lang.compiler import compiler_opts from basilisp.lang.typing import CompilerOpts from basilisp.lang.util import munge -from basilisp.logconfig import DEFAULT_HANDLER, DEFAULT_LEVEL -logger = logging.getLogger("basilisp") -logger.setLevel(DEFAULT_LEVEL) -logger.addHandler(DEFAULT_HANDLER) + +def configure_root_logger() -> None: + logger = logging.getLogger("basilisp") + logger.setLevel(logconfig.get_level()) + logger.addHandler(logconfig.get_handler()) def init(opts: Optional[CompilerOpts] = None) -> None: @@ -27,6 +29,7 @@ def init(opts: Optional[CompilerOpts] = None) -> None: If you want to execute a Basilisp file which is stored in a well-formed package or module structure, you probably want to use :py:func:`bootstrap`. """ + configure_root_logger() runtime.init_ns_var() runtime.bootstrap_core(opts if opts is not None else compiler_opts()) importer.hook_imports() From 525d59e4c6ec58df209404294be38261684f714b Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Sun, 15 Sep 2024 09:49:49 -0400 Subject: [PATCH 2/4] CHangelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 089277aee..025a36c66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Added the `-p`/`--include-path` CLI command to prepend entries to the `sys.path` as an alternative to `PYTHONPATH` (#1027) * Added an empty entry to `sys.path` for all CLI entrypoints (`basilisp run`, `basilisp repl`, etc.) (#1027) - * Added command line arguments for enabling the development logger and configuring the logging level (#???) + * Added command line arguments for enabling the development logger and configuring the logging level (#1047) ### Changed * The compiler will no longer require `Var` indirection for top-level `do` forms unless those forms specify `^:use-var-indirection` metadata (which currently is only used in the `ns` macro) (#1034) From 98a0a2da72a7abfc667e8b81498c3d9a1b3259b1 Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Sun, 15 Sep 2024 09:53:23 -0400 Subject: [PATCH 3/4] Tweak --- src/basilisp/logconfig.py | 10 ++++++++++ src/basilisp/main.py | 9 +-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/basilisp/logconfig.py b/src/basilisp/logconfig.py index 56b7191c3..096e622ec 100644 --- a/src/basilisp/logconfig.py +++ b/src/basilisp/logconfig.py @@ -30,3 +30,13 @@ def get_handler( handler.setFormatter(logging.Formatter(fmt)) handler.setLevel(level or get_level()) return handler + + +def configure_root_logger( + level: Optional[str] = None, fmt: str = DEFAULT_FORMAT +) -> None: + """Configure the Basilisp root logger.""" + level = level or get_level() + logger = logging.getLogger("basilisp") + logger.setLevel(level) + logger.addHandler(get_handler(level=level, fmt=fmt)) diff --git a/src/basilisp/main.py b/src/basilisp/main.py index e6ff2e00c..442942ad9 100644 --- a/src/basilisp/main.py +++ b/src/basilisp/main.py @@ -1,5 +1,4 @@ import importlib -import logging import site from pathlib import Path from typing import List, Optional @@ -12,12 +11,6 @@ from basilisp.lang.util import munge -def configure_root_logger() -> None: - logger = logging.getLogger("basilisp") - logger.setLevel(logconfig.get_level()) - logger.addHandler(logconfig.get_handler()) - - def init(opts: Optional[CompilerOpts] = None) -> None: """ Initialize the runtime environment for Basilisp code evaluation. @@ -29,7 +22,7 @@ def init(opts: Optional[CompilerOpts] = None) -> None: If you want to execute a Basilisp file which is stored in a well-formed package or module structure, you probably want to use :py:func:`bootstrap`. """ - configure_root_logger() + logconfig.configure_root_logger() runtime.init_ns_var() runtime.bootstrap_core(opts if opts is not None else compiler_opts()) importer.hook_imports() From a8f746a1b82323e5064908242118bdf5eca47e01 Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Sun, 15 Sep 2024 10:08:48 -0400 Subject: [PATCH 4/4] Ugh --- src/basilisp/logconfig.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/basilisp/logconfig.py b/src/basilisp/logconfig.py index 096e622ec..8b6350dd9 100644 --- a/src/basilisp/logconfig.py +++ b/src/basilisp/logconfig.py @@ -2,7 +2,6 @@ import os from typing import Optional - TRACE = 5 logging.addLevelName(TRACE, "TRACE")