Skip to content

Commit 48f61b3

Browse files
authored
Add CLI arguments for enabling the development logger and setting logging level (#1048)
Fixes #1047
1 parent ae617c7 commit 48f61b3

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
* Added the `-p`/`--include-path` CLI command to prepend entries to the `sys.path` as an alternative to `PYTHONPATH` (#1027)
1010
* Added an empty entry to `sys.path` for all CLI entrypoints (`basilisp run`, `basilisp repl`, etc.) (#1027)
11+
* Added command line arguments for enabling the development logger and configuring the logging level (#1047)
1112

1213
### Changed
1314
* 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)

src/basilisp/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,32 @@ def _add_debug_arg_group(parser: argparse.ArgumentParser) -> None:
281281
"(env: BASILISP_DO_NOT_CACHE_NAMESPACES; default: false)"
282282
),
283283
)
284+
group.add_argument(
285+
"--enable-logger",
286+
action=_set_envvar_action(
287+
"BASILISP_USE_DEV_LOGGER", parent=argparse._StoreAction
288+
),
289+
nargs="?",
290+
const=True,
291+
type=_to_bool,
292+
help=(
293+
"if true, enable the Basilisp root logger "
294+
"(env: BASILISP_USE_DEV_LOGGER; default: false)"
295+
),
296+
)
297+
group.add_argument(
298+
"-l",
299+
"--log-level",
300+
action=_set_envvar_action(
301+
"BASILISP_LOGGING_LEVEL", parent=argparse._StoreAction
302+
),
303+
type=lambda s: s.upper(),
304+
default="WARNING",
305+
help=(
306+
"the logging level for logs emitted by the Basilisp compiler and runtime "
307+
"(env: BASILISP_LOGGING_LEVEL; default: WARNING)"
308+
),
309+
)
284310

285311

286312
def _add_import_arg_group(parser: argparse.ArgumentParser) -> None:

src/basilisp/logconfig.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
import logging
22
import os
3+
from typing import Optional
4+
5+
TRACE = 5
6+
7+
logging.addLevelName(TRACE, "TRACE")
8+
9+
10+
DEFAULT_FORMAT = (
11+
"%(asctime)s %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] - %(message)s"
12+
)
313

414

515
def get_level() -> str:
616
"""Get the default logging level for Basilisp."""
717
return os.getenv("BASILISP_LOGGING_LEVEL", "WARNING")
818

919

10-
def get_handler(level: str, fmt: str) -> logging.Handler:
20+
def get_handler(
21+
level: Optional[str] = None, fmt: str = DEFAULT_FORMAT
22+
) -> logging.Handler:
1123
"""Get the default logging handler for Basilisp."""
1224
handler = (
1325
logging.StreamHandler()
1426
if os.getenv("BASILISP_USE_DEV_LOGGER", "").lower() == "true"
1527
else logging.NullHandler()
1628
)
1729
handler.setFormatter(logging.Formatter(fmt))
18-
handler.setLevel(level)
30+
handler.setLevel(level or get_level())
1931
return handler
2032

2133

22-
TRACE = 5
23-
24-
logging.addLevelName(TRACE, "TRACE")
25-
26-
DEFAULT_FORMAT = (
27-
"%(asctime)s %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] - %(message)s"
28-
)
29-
DEFAULT_LEVEL = get_level()
30-
DEFAULT_HANDLER = get_handler(DEFAULT_LEVEL, DEFAULT_FORMAT)
34+
def configure_root_logger(
35+
level: Optional[str] = None, fmt: str = DEFAULT_FORMAT
36+
) -> None:
37+
"""Configure the Basilisp root logger."""
38+
level = level or get_level()
39+
logger = logging.getLogger("basilisp")
40+
logger.setLevel(level)
41+
logger.addHandler(get_handler(level=level, fmt=fmt))

src/basilisp/main.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import importlib
2-
import logging
32
import site
43
from pathlib import Path
54
from typing import List, Optional
65

76
from basilisp import importer as importer
7+
from basilisp import logconfig
88
from basilisp.lang import runtime as runtime
99
from basilisp.lang.compiler import compiler_opts
1010
from basilisp.lang.typing import CompilerOpts
1111
from basilisp.lang.util import munge
12-
from basilisp.logconfig import DEFAULT_HANDLER, DEFAULT_LEVEL
13-
14-
logger = logging.getLogger("basilisp")
15-
logger.setLevel(DEFAULT_LEVEL)
16-
logger.addHandler(DEFAULT_HANDLER)
1712

1813

1914
def init(opts: Optional[CompilerOpts] = None) -> None:
@@ -27,6 +22,7 @@ def init(opts: Optional[CompilerOpts] = None) -> None:
2722
If you want to execute a Basilisp file which is stored in a well-formed package
2823
or module structure, you probably want to use :py:func:`bootstrap`.
2924
"""
25+
logconfig.configure_root_logger()
3026
runtime.init_ns_var()
3127
runtime.bootstrap_core(opts if opts is not None else compiler_opts())
3228
importer.hook_imports()

0 commit comments

Comments
 (0)