File size: 1,019 Bytes
25e4412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Rotating file logs in <data>/logs plus console output."""

from __future__ import annotations

import logging
from logging.handlers import RotatingFileHandler

from stemdeck.settings import get_settings

LOG_FORMAT = "%(asctime)s %(levelname)s %(name)s [%(process)d]: %(message)s"


def setup_logging(filename: str) -> None:
    settings = get_settings()
    root = logging.getLogger()
    if any(getattr(h, "_stemdeck", False) for h in root.handlers):
        return
    root.setLevel(settings.log_level.upper())
    file_handler = RotatingFileHandler(settings.logs_dir / filename, maxBytes=5_000_000, backupCount=3,
                                       encoding="utf-8")
    file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
    file_handler._stemdeck = True  # type: ignore[attr-defined]
    console = logging.StreamHandler()
    console.setFormatter(logging.Formatter(LOG_FORMAT))
    console._stemdeck = True  # type: ignore[attr-defined]
    root.addHandler(file_handler)
    root.addHandler(console)