Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#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)
Expand Down
26 changes: 26 additions & 0 deletions src/basilisp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 22 additions & 11 deletions src/basilisp/logconfig.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
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:
"""Get the default logging level for Basilisp."""
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()
if os.getenv("BASILISP_USE_DEV_LOGGER", "").lower() == "true"
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)
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))
8 changes: 2 additions & 6 deletions src/basilisp/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import importlib
import logging
import site
from pathlib import Path
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 init(opts: Optional[CompilerOpts] = None) -> None:
Expand All @@ -27,6 +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`.
"""
logconfig.configure_root_logger()
runtime.init_ns_var()
runtime.bootstrap_core(opts if opts is not None else compiler_opts())
importer.hook_imports()
Expand Down