-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_setup.py
More file actions
33 lines (27 loc) · 954 Bytes
/
Copy pathlogger_setup.py
File metadata and controls
33 lines (27 loc) · 954 Bytes
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
import logging
import os
from logging.handlers import RotatingFileHandler
from config import LOG_DIR
os.makedirs(LOG_DIR, exist_ok=True)
def get_logger(name: str) -> logging.Logger:
"""Return a named logger that writes to both console and a rotating file."""
logger = logging.getLogger(name)
if logger.handlers:
return logger
logger.setLevel(logging.DEBUG)
fmt = logging.Formatter(
"%(asctime)s %(levelname)-8s [%(name)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# Console handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(fmt)
# Rotating file handler (10 MB per file, keep 5 backups)
log_path = os.path.join(LOG_DIR, f"{name}.log")
fh = RotatingFileHandler(log_path, maxBytes=10 * 1024 * 1024, backupCount=5)
fh.setLevel(logging.DEBUG)
fh.setFormatter(fmt)
logger.addHandler(ch)
logger.addHandler(fh)
return logger