diff --git a/backend/capellacollab/__main__.py b/backend/capellacollab/__main__.py index 150b6512a..c5a0a099a 100644 --- a/backend/capellacollab/__main__.py +++ b/backend/capellacollab/__main__.py @@ -48,17 +48,20 @@ from . import __version__ -handlers: list[logging.Handler] = [ - logging.StreamHandler(), - core_logging.CustomTimedRotatingFileHandler( - str(config["logging"]["logPath"]) + "backend.log" - ), -] +stream_handler = logging.StreamHandler() +stream_handler.setFormatter(core_logging.CustomFormatter()) -for handler in handlers: - handler.setFormatter(core_logging.CustomFormatter()) +timed_rotating_file_handler = core_logging.CustomTimedRotatingFileHandler( + str(config["logging"]["logPath"]) + "backend.log" +) +timed_rotating_file_handler.setFormatter( + core_logging.CustomFormatter(colored_output=False) +) -logging.basicConfig(level=config["logging"]["level"], handlers=handlers) +logging.basicConfig( + level=config["logging"]["level"], + handlers=[stream_handler, timed_rotating_file_handler], +) async def startup(): diff --git a/backend/capellacollab/core/logging/__init__.py b/backend/capellacollab/core/logging/__init__.py index 64e413d6b..a5474d0d5 100644 --- a/backend/capellacollab/core/logging/__init__.py +++ b/backend/capellacollab/core/logging/__init__.py @@ -20,20 +20,40 @@ class CustomFormatter(logging.Formatter): - def __init__(self): - self._request_formatters = logging.Formatter( - 'time="%(asctime)s" level=%(levelname)s function=%(funcName)s %(message)s' - ) - self._default_formatter = logging.Formatter( - 'time="%(asctime)s" level=%(levelname)s name=%(name)s function=%(funcName)s message="%(message)s"' - ) + _colors = { + logging.DEBUG: "\x1b[37;40m", # white + logging.INFO: "\x1b[34;40m", # blue + logging.WARNING: "\x1b[93;40m", # yellow + logging.ERROR: "\x1b[31;40m", # red + logging.CRITICAL: "\x1b[31;40m", # red + "reset": "\x1b[0m", + } + + def __init__(self, colored_output: bool = True) -> None: super().__init__() + self.colored_output = colored_output def format(self, record): + log_format = 'time="%(asctime)s" level=%(levelname)s ' if record.name == "capellacollab.request": - return self._request_formatters.format(record) + log_format += "function=%(funcName)s %(message)s" + else: + log_format += ( + 'name=%(name)s function=%(funcName)s message="%(message)s"' + ) + + if self.colored_output: + log_format = ( + self._colors[record.levelno] + + log_format + + self._colors["reset"] + ) + + formatter = logging.Formatter( + log_format, datefmt="%Y-%m-%dT%H:%M:%S%z" + ) - return self._default_formatter.format(record) + return formatter.format(record) class CustomTimedRotatingFileHandler(handlers.TimedRotatingFileHandler): diff --git a/helm/templates/promtail/promtail.configmap.yaml b/helm/templates/promtail/promtail.configmap.yaml index ad3d12d19..1cb1c53fb 100644 --- a/helm/templates/promtail/promtail.configmap.yaml +++ b/helm/templates/promtail/promtail.configmap.yaml @@ -20,7 +20,7 @@ data: - job_name: system pipeline_stages: - multiline: - firstline: '^time=\"\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2},\d{3}\"' + firstline: '^time=' static_configs: - targets: - localhost