-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_logger.py
53 lines (42 loc) · 1.27 KB
/
custom_logger.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
50
51
52
53
import logging
from logging.handlers import TimedRotatingFileHandler
custom_formatter = logging.Formatter(
"{asctime} {levelname:<8} - ({funcName:20.20}) {message}",
datefmt="%Y-%m-%d %H:%M:%S",
style="{",
)
def dummy_log():
"""
Dummy logger, mostly for tests.
"""
class DummyLogger:
def info(self, text):
print(text)
def warning(self, text):
print(text)
def debug(self, text):
print(text)
def error(self, text):
print(text)
return DummyLogger()
def set_log_handler(
logger: logging.Logger,
path: str,
interval: int,
nlogs: int,
enable_debug: bool = False,
):
"""Configures the logger given with a formatter and a handler"""
try:
# add a rotating handler
formatter = custom_formatter
handler = TimedRotatingFileHandler(
path, when="h", interval=interval, backupCount=nlogs
)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG if enable_debug else logging.INFO)
logger.setLevel(logging.DEBUG if enable_debug else logging.INFO)
logger.addHandler(handler)
logger.info(f"Logging started in '{path}'")
except Exception as e:
return dummy_log()