-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_config.py
79 lines (71 loc) · 2.48 KB
/
logger_config.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
import sqlite3
import threading
import traceback
class SQLiteHandler(logging.Handler):
def __init__(self, db='app_logs.db'):
super().__init__()
self.db = db
self.lock = threading.Lock()
self._initialize_database()
def _initialize_database(self):
# Initialize the database schema
conn = sqlite3.connect(self.db)
conn.execute('''
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created REAL,
level TEXT,
module TEXT,
funcName TEXT,
lineno INTEGER,
message TEXT,
args TEXT,
exc_info TEXT,
processName TEXT,
threadName TEXT
)
''')
conn.commit()
conn.close()
def formatException(self, exc_info):
if exc_info:
return ''.join(traceback.format_exception(*exc_info))
return ''
def emit(self, record):
try:
# Format the record to ensure message is ready
self.format(record)
log_entry = (
record.created,
record.levelname,
record.module,
record.funcName,
record.lineno,
record.getMessage(),
str(record.args),
self.formatException(record.exc_info) if record.exc_info else None,
record.processName,
record.threadName,
)
insert_sql = '''
INSERT INTO logs (
created, level, module, funcName, lineno, message, args, exc_info, processName, threadName
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
# Create a new connection for this thread
conn = sqlite3.connect(self.db)
with conn:
conn.execute(insert_sql, log_entry)
except Exception:
self.handleError(record)
def setup_logger(name=None, level=logging.DEBUG):
logger = logging.getLogger(name)
logger.setLevel(level)
# Prevent adding multiple handlers if the logger is already configured
if not logger.handlers:
sqlite_handler = SQLiteHandler()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(message)s')
sqlite_handler.setFormatter(formatter)
logger.addHandler(sqlite_handler)
return logger