Source code for timApp.util.logger

"""Sets up logging for the application."""
import logging
import os
import sys
from logging.handlers import TimedRotatingFileHandler

from flask import Flask

tim_logger = logging.getLogger("tim")
tim_logger.setLevel(logging.DEBUG)

wz = logging.getLogger("werkzeug")
wz.setLevel(logging.ERROR)


[docs]def setup_logging(app: Flask) -> None: if not app.config["TESTING"]: logging.getLogger("alembic").level = logging.INFO formatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s ") os.makedirs(app.config["LOG_DIR"], exist_ok=True) file_handler = TimedRotatingFileHandler( app.config["LOG_PATH"], when="D", interval=30, backupCount=12, ) file_handler.setLevel(app.config["LOG_LEVEL"]) file_handler.setFormatter(formatter) global tim_logger tim_logger.handlers = [] tim_logger.propagate = False tim_logger.addHandler(file_handler) app.logger.addHandler(file_handler) if app.config["LOG_LEVEL_STDOUT"] is not None: stdout_handler = logging.StreamHandler(stream=sys.stdout) stdout_handler.setLevel(app.config["LOG_LEVEL_STDOUT"]) stdout_handler.setFormatter( logging.Formatter("%(asctime)s %(levelname)s: %(message)s") ) tim_logger.addHandler(stdout_handler)
[docs]def enable_loggers() -> None: tim_logger.disabled = False wz.disabled = False
[docs]def log_debug(message: str) -> None: tim_logger.debug(message)
[docs]def log_info(message: str) -> None: tim_logger.info(message)
[docs]def log_error(message: str) -> None: tim_logger.error(message)
[docs]def log_warning(message: str) -> None: tim_logger.warning(message)