Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions xcdat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Top-level package for xcdat."""

from xcdat import tutorial # noqa: F401
from xcdat._logger import _setup_root_logger
from xcdat.axis import ( # noqa: F401
center_times,
get_coords_by_name,
Expand All @@ -25,4 +26,7 @@
from xcdat.temporal import TemporalAccessor # noqa: F401
from xcdat.utils import compare_datasets # noqa: F401

# Initialize root logger once when the package is imported
_setup_root_logger()

__version__ = "0.10.1"
42 changes: 33 additions & 9 deletions xcdat/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@
import logging.handlers

# Logging module setup
log_format = (
LOG_FORMAT = (
"%(asctime)s [%(levelname)s]: %(filename)s(%(funcName)s:%(lineno)s) >> %(message)s"
)
logging.basicConfig(format=log_format, filemode="w", level=logging.INFO)

# Console handler setup
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
logFormatter = logging.Formatter(log_format)
console_handler.setFormatter(logFormatter)
logging.getLogger().addHandler(console_handler)

LOG_LEVEL = logging.INFO


def _setup_root_logger():
"""Configures the root logger.

This function sets up the root logger with a predefined format and log level.
It also enables capturing of warnings issued by the `warnings` module and
redirects them to the logging system.

Notes
-----
- The `force=True` parameter ensures that any existing logging configuration
is overridden.
- The file handler is added dynamically to the root logger later in the
``Run`` class once the log file path is known.
"""
logging.basicConfig(
format=LOG_FORMAT,
level=LOG_LEVEL,
force=True,
)

logging.captureWarnings(True)

# Add a console handler to display warnings in the console. This is useful
# for when other package loggers raise warnings (e.g, NumPy, Xarray).
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(logging.Formatter(LOG_FORMAT))
logging.getLogger().addHandler(console_handler)


def _setup_custom_logger(name, propagate=True) -> logging.Logger:
Expand Down
Loading