Module dlr.ki.logging¶
DLR Logging Base Module
View Source
# SPDX-FileCopyrightText: 2024 Benedikt Franke <benedikt.franke@dlr.de>
# SPDX-FileCopyrightText: 2024 Florian Heinrich <florian.heinrich@dlr.de>
#
# SPDX-License-Identifier: Apache-2.0
"""DLR Logging Base Module"""
from .config import get_default_logging_dict
from .loader import load, load_default
__all__ = ["get_default_logging_dict", "load", "load_default"]
Sub-modules¶
Functions¶
get_default_logging_dict¶
def get_default_logging_dict(
log_filepath: Optional[str] = None,
ensure_log_dir: bool = True
) -> Dict[str, Any]
Get default logging configuration dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_filepath | str | Log file path. Defaults to None. | None |
ensure_log_dir | bool | Create directory for the log file if not exists. Defaults to True. | True |
Returns:
Type | Description |
---|---|
Dict[str, Any] | logging configuration dict |
View Source
def get_default_logging_dict(log_filepath: Optional[str] = None, ensure_log_dir: bool = True) -> Dict[str, Any]:
"""
Get default logging configuration dict.
Args:
log_filepath (str, optional): Log file path. Defaults to None.
ensure_log_dir (bool, optional): Create directory for the log file if not exists. Defaults to True.
Returns:
Dict[str, Any]: logging configuration dict
"""
if log_filepath and ensure_log_dir:
Path(log_filepath).parent.mkdir(parents=True, exist_ok=True)
return {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"console_formatter": {
"class": "dlr.ki.logging.formatter.colored_console.ColoredConsoleFormatter",
"format": "%(clearline)s[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s" # noqa: E501
},
"file_formatter": {
"class": "dlr.ki.logging.formatter.term_escape_code.TermEscapeCodeFormatter",
"format": "%(asctime)s [%(levelname)-8s] [%(name)s][%(filename)s:%(lineno)d] %(message)s"
}
},
"handlers": {
"console_handler": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
"level": "INFO",
"formatter": "console_formatter"
},
**({"file_handler": {
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": log_filepath,
"when": "midnight",
"level": "DEBUG",
"formatter": "file_formatter"
}} if log_filepath else {})
},
"loggers": {
"": {
"handlers": ["console_handler", "file_handler"] if log_filepath else ["console_handler"],
"level": "DEBUG",
"propagate": True
},
"celery": {
"level": "WARNING"
},
"matplotlib": {
"level": "WARNING"
},
"numba": {
"level": "WARNING"
},
"PIL": {
"level": "WARNING"
}
}
}
load¶
Load logging configuration from file or dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config | Union[str, Dict[str, Any]] | configuration file path or dict | None |
Raises:
Type | Description |
---|---|
ValueError | if configuration file could not be loaded |
View Source
def load(config: Union[str, Dict[str, Any]], **kwargs) -> None:
"""
Load logging configuration from file or dict.
Args:
config (Union[str, Dict[str, Any]]): configuration file path or dict
Raises:
ValueError: if configuration file could not be loaded
"""
if isinstance(config, str):
try:
load_yaml(config)
except yaml.parser.ParserError:
try:
load_conf(config, **kwargs)
except Exception:
raise ValueError(f"Cannot load config from '{config}'")
else:
load_dict(config)
load_default¶
Load default logging configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_filepath | str | Log file path. Defaults to None. | None |
ensure_log_dir | bool | Create directory for the log file if not exists. Defaults to True. | True |
View Source
def load_default(log_filepath: Optional[str] = None, ensure_log_dir: bool = True) -> None:
"""
Load default logging configuration.
Args:
log_filepath (str, optional): Log file path. Defaults to None.
ensure_log_dir (bool, optional): Create directory for the log file if not exists. Defaults to True.
"""
load_dict(get_default_logging_dict(log_filepath, ensure_log_dir))