tuxbot-bot/tuxbot/logging.py

53 lines
1.2 KiB
Python
Raw Normal View History

2020-06-03 19:41:30 +02:00
import logging.handlers
import logging
2020-06-03 19:41:30 +02:00
import pathlib
import sys
MAX_OLD_LOGS = 8
MAX_BYTES = 5_000_000
2020-06-03 19:41:30 +02:00
formatter = logging.Formatter(
"[{asctime}] [{levelname}] {name}: {message}",
datefmt="%Y-%m-%d %H:%M:%S",
style="{",
)
2020-06-03 19:41:30 +02:00
def _setup_logging(level: int, location: pathlib.Path, name: str) -> None:
logger = logging.getLogger(name)
logger.setLevel(level)
logger_file = location / f"{name}.log"
handler = logging.handlers.RotatingFileHandler(
str(logger_file.resolve()),
maxBytes=MAX_BYTES,
backupCount=MAX_OLD_LOGS,
)
base_handler = logging.handlers.RotatingFileHandler(
str(logger_file.resolve()),
maxBytes=MAX_BYTES,
backupCount=MAX_OLD_LOGS,
)
handler.setFormatter(formatter)
base_handler.setFormatter(formatter)
2020-06-03 19:41:30 +02:00
def init_logging(level: int, location: pathlib.Path) -> None:
2020-06-04 00:46:53 +02:00
"""Initialize loggers.
Parameters
----------
level:int
Level of debug.
location:Path
2020-11-09 01:18:55 +01:00
Where to store Logs.
2020-06-04 00:46:53 +02:00
"""
2020-06-05 00:29:14 +02:00
_setup_logging(level, location, "discord")
_setup_logging(level, location, "tuxbot")
2020-06-03 19:41:30 +02:00
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)