Skip to content

logging.config

Functions:

Name Description
get_default_logging_dict

Get default logging configuration dict.

Functions

get_default_logging_dict

get_default_logging_dict(log_filepath: str | None = 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]

Dict[str, Any]: logging configuration dict

Source code in dlr/ki/logging/config.py
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%(message)s%(colorreset)s"
            },
            "file_formatter": {
                "class": "dlr.ki.logging.formatter.term_escape_code.TermEscapeCodeFormatter",
                "format": "%(asctime)s [%(levelname)s] [%(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"
            }
        }
    }