-
Notifications
You must be signed in to change notification settings - Fork 11
/
logger.py
34 lines (24 loc) · 971 Bytes
/
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
import logging
import os
import time
import constants
def setup_logger() -> logging.Logger:
os.makedirs(constants.LOG_DIR, exist_ok=True)
logger = logging.getLogger(__package__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
logger.handlers = []
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console_formatter = logging.Formatter("%(message)s")
console.setFormatter(console_formatter)
logger.addHandler(console)
timestamp = time.strftime("%Y%m%d%H%M%S")
log_filename = f"log_{timestamp}.log"
log_file_path = os.path.join(constants.LOG_DIR, log_filename)
file_handler = logging.FileHandler(filename=log_file_path, mode="w", encoding="utf-8")
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
return logger