-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
40 lines (30 loc) · 1.16 KB
/
Copy pathlogger.py
File metadata and controls
40 lines (30 loc) · 1.16 KB
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
import logging
from logging import Logger
from config import resource_path
LOG_FILE = resource_path("bucket/app.log") # adjust path as needed
def get_logger(name: str) -> Logger:
"""
Returns a logger that writes INFO+ to both console and a log file.
- name: typically `__name__` of the module.
- Creates handlers only once per logger to avoid duplicate lines.
"""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# If the logger already has handlers, we assume it's already configured.
if logger.handlers:
return logger
# 1) File handler
file_handler = logging.FileHandler(LOG_FILE, mode="a", encoding="utf-8")
file_handler.setLevel(logging.INFO)
# 2) Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# 3) Shared formatter
fmt = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
formatter = logging.Formatter(fmt)
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# 4) Attach handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger