2020-06-03 19:41:30 +02:00
|
|
|
import logging.handlers
|
2020-06-04 00:14:50 +02:00
|
|
|
import logging
|
2020-06-03 19:41:30 +02:00
|
|
|
import pathlib
|
|
|
|
import sys
|
|
|
|
|
|
|
|
MAX_OLD_LOGS = 8
|
2020-06-04 00:14:50 +02:00
|
|
|
MAX_BYTES = 5_000_000
|
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
|
|
|
|
Where to store logs.
|
|
|
|
"""
|
2020-06-05 00:29:14 +02:00
|
|
|
|
2020-06-03 19:41:30 +02:00
|
|
|
dpy_logger = logging.getLogger("discord")
|
2020-06-04 00:14:50 +02:00
|
|
|
dpy_logger.setLevel(logging.WARN)
|
|
|
|
dpy_logger_file = location / 'discord.log'
|
|
|
|
|
|
|
|
base_logger = logging.getLogger("tuxbot")
|
2020-06-03 19:41:30 +02:00
|
|
|
base_logger.setLevel(level)
|
2020-06-04 00:14:50 +02:00
|
|
|
base_logger_file = location / 'tuxbot.log'
|
2020-06-03 19:41:30 +02:00
|
|
|
|
|
|
|
formatter = logging.Formatter(
|
2020-06-04 00:14:50 +02:00
|
|
|
"[{asctime}] [{levelname}] {name}: {message}",
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S", style="{"
|
|
|
|
)
|
|
|
|
|
|
|
|
dpy_handler = logging.handlers.RotatingFileHandler(
|
|
|
|
str(dpy_logger_file.resolve()),
|
|
|
|
maxBytes=MAX_BYTES, backupCount=MAX_OLD_LOGS
|
|
|
|
)
|
|
|
|
base_handler = logging.handlers.RotatingFileHandler(
|
|
|
|
str(base_logger_file.resolve()),
|
|
|
|
maxBytes=MAX_BYTES, backupCount=MAX_OLD_LOGS
|
2020-06-03 19:41:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
stdout_handler.setFormatter(formatter)
|
2020-06-05 00:29:14 +02:00
|
|
|
dpy_logger.addHandler(dpy_handler)
|
|
|
|
base_logger.addHandler(base_handler)
|