diff --git a/.gitignore b/.gitignore index d8ab9c17..6f460810 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ __pypackages__/ # macos .DS_Store + +# tad tool +tad.log* diff --git a/tad/core/log.py b/tad/core/log.py new file mode 100644 index 00000000..b0a98a29 --- /dev/null +++ b/tad/core/log.py @@ -0,0 +1,48 @@ +import copy +import logging +import logging.config +from typing import Any + +from tad.core.types import LoggingLevelType + +LOGGING_SIZE = 10 * 1024 * 1024 +LOGGING_BACKUP_COUNT = 5 + +LOGGING_CONFIG = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "generic": { + "()": "logging.Formatter", + "style": "{", + "fmt": "{asctime}({levelname},{name}): {message}", + "datefmt": "[%Y-%m-%d %H:%M:%S %z]", + } + }, + "handlers": { + "console": {"formatter": "generic", "class": "logging.StreamHandler", "stream": "ext://sys.stdout"}, + "file": { + "formatter": "generic", + "()": "logging.handlers.RotatingFileHandler", + "filename": "tad.log", + "maxBytes": LOGGING_SIZE, + "backupCount": LOGGING_BACKUP_COUNT, + }, + }, + "loggers": { + "tad": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": True}, + }, +} + + +def configure_logging(level: LoggingLevelType = "INFO", config: dict[str, Any] | None = None) -> None: + log_config = copy.deepcopy(LOGGING_CONFIG) + + if config: + log_config.update(config) + + logging.config.dictConfig(log_config) + + logger = logging.getLogger("tad") + + logger.setLevel(level) diff --git a/tad/core/logger.py b/tad/core/logger.py deleted file mode 100644 index 0efe6f43..00000000 --- a/tad/core/logger.py +++ /dev/null @@ -1,9 +0,0 @@ -import logging - -from tad.core.config import settings - - -def set_default_logging_setup(): - logging.basicConfig(level=settings.LOGGING_LEVEL, style="{", format="{asctime}({levelname},{name}): {message}") - # todo(berry): load from logging.config.dictConfig - # todo(berry): create RotatingFileHandler for logging to append to the default stream handler