This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggers.py
49 lines (39 loc) · 1.42 KB
/
loggers.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
""" Sets up the loggers for the TaskBot project """
from datetime import datetime
import logging
import os
def make_logger(
name: str, file: str, level=logging.DEBUG, to_file=True, to_stdout=True, create_dirs=True,
formatter=logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] [%(module)s] %(message)s')
) -> logging.Logger:
""" Creates and returns a new logger """
logger = logging.getLogger(name)
logger.setLevel(level)
if create_dirs:
path = os.path.dirname(os.path.abspath(file)) # Get path from file
if not os.path.exists(path):
os.makedirs(path)
if to_file:
file_handler = logging.FileHandler(file, encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if to_stdout:
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
# Loggers configurations
LOGS_DIR = 'logs'
discord_logger = make_logger(
name='discord',
file=f'{LOGS_DIR}/discord/{datetime.utcnow().strftime("%Y-%m-%d %H-%M-%S")}.log',
to_stdout=False
)
sqlalchemy_logger = make_logger(
name='sqlalchemy.engine.Engine',
file=f'{LOGS_DIR}/sqlalchemy/{datetime.utcnow().strftime("%Y-%m-%d %H-%M-%S")}.log'
)
logger = make_logger(
name='custom',
file=f'{LOGS_DIR}/custom/{datetime.utcnow().strftime("%Y-%m-%d %H-%M-%S")}.log'
)