-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_handler.py
146 lines (136 loc) · 3.33 KB
/
sqlite_handler.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import logging
from logging import LogRecord
import sqlite3
CREATE_SQL = """
CREATE TABLE IF NOT EXISTS LOGS(
name text,
msg text,
args text,
levelname text,
levelno integer,
pathname text,
filename text,
module text,
exc_info text,
exc_test text,
stack_info text,
lineno integer,
funcName text,
created real,
msecs real,
relativeCreated real,
thread integer,
threadName text,
processName text,
process integer,
message text,
asctime text
)
"""
INSERT_SQL = """
INSERT INTO LOGS(
name,
msg,
args,
levelname,
levelno,
pathname,
filename,
module,
exc_info,
exc_test,
stack_info,
lineno,
funcName,
created,
msecs,
relativeCreated,
thread,
threadName,
processName,
process,
message,
asctime
) VALUES(
:name,
:msg,
:args,
:levelname,
:levelno,
:pathname,
:filename,
:module,
:exc_info,
:exc_test,
:stack_info,
:lineno,
:funcName,
:created,
:msecs,
:relativeCreated,
:thread,
:threadName,
:processName,
:process,
:message,
:asctime
)
"""
class SQLiteHandler(logging.Handler):
"""
custom handler that writes logs to SQLite DB
"""
def __init__(self, db):
logging.Handler.__init__(self)
self.db = db
with sqlite3.connect(self.db) as con:
con.execute(CREATE_SQL)
con.commit()
def emit(self, record: LogRecord):
with sqlite3.connect(self.db) as con:
con.execute(
INSERT_SQL,
{
'name': record.name,
'msg': record.msg,
'args': str(record.args),
'levelname': record.levelname,
'levelno': record.levelno,
'pathname': record.pathname,
'filename': record.filename,
'module': record.module,
'exc_info': record.exc_info,
'exc_test': record.exc_text,
'stack_info': record.stack_info,
'lineno': record.lineno,
'funcName': record.funcName,
'created': record.created,
'msecs': record.msecs,
'relativeCreated': record.relativeCreated,
'thread': record.thread,
'threadName': record.threadName,
'processName': record.processName,
'process': record.process,
'message': record.message,
'asctime': record.asctime
}
)
con.commit()
if __name__ == '__main__':
logger = logging.getLogger('my-logger')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - [%(levelname)s] - %(name)s - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
# console handler
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
# sqlite handler
sql_handler = SQLiteHandler('logs.sqlite')
sql_handler.setLevel(logging.DEBUG)
logger.addHandler(sql_handler)
logger.info("hello from logger")
logger.error("this is an error message")