-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.py
More file actions
69 lines (50 loc) · 1.79 KB
/
Logger.py
File metadata and controls
69 lines (50 loc) · 1.79 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# logger_config.py
import logging
import os
from datetime import datetime
import platform
from pathlib import Path
class EscapingFormatter(logging.Formatter):
"""Formatter that escapes newline characters in log messages."""
def format(self, record):
s = super().format(record)
return s.replace('\n', r'\n')
def get_documents_dir():
"""Return the user's Documents directory in a cross-platform safe way."""
return Path(os.path.expanduser("~/Documents"))
def setup_logger():
logger = logging.getLogger("StaTube")
logger.setLevel(logging.DEBUG)
# Prevent duplicate handlers if logger is reloaded
if logger.handlers:
return logger, None
# Timestamped file name
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Directory: ~/Documents/StaTube/
documents_dir = get_documents_dir()
log_dir = documents_dir / "StaTube"
log_dir.mkdir(parents=True, exist_ok=True)
# Example: statube_log_20251204_162233.log
log_file_path = log_dir / f"statube_log_{timestamp}.log"
# Log format similar to your "driving_pattern" version
fmt = (
"%(asctime)s | %(levelname)-8s | "
"%(filename)s:%(lineno)d | %(module)s.%(funcName)s | "
"%(message)s"
)
formatter = EscapingFormatter(fmt)
# File handler
fh = logging.FileHandler(log_file_path, encoding="utf-8")
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
# Optional: console output
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.propagate = False
logger.debug(f"StaTube logger initialized at: {log_file_path}")
return logger, log_file_path
# Initialize logger when imported
logger, log_file_path = setup_logger()