-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
logger.py
104 lines (83 loc) · 3.31 KB
/
logger.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
import logging
import os
import re
import sys
from datetime import datetime, timedelta
from logging.handlers import RotatingFileHandler
class StreamToLogger:
logger = None
level = None
buffer = ""
pre_char = ""
pre_spaces = False
def __init__(self, logger, level):
self.logger = logger
self.level = level
def write(self, buf):
for line in buf.splitlines():
#self.logger.log(self.level, f"# ({len(line)}):'{line}'") # for debug
if line.startswith((
"<class 'usb.core.USBError'>",
"<class 'usb.core.USBTimeoutError'>",
)):
continue
if self.pre_char in [" ", "^"] and line in [" ", "^"]:
self.buffer += line
elif len(line) > 1 and line.isspace():
self.buffer += line
self.pre_spaces = True
elif line not in [" ", "^"]:
if len(self.buffer):
if self.pre_spaces:
self.logger.log(self.level, self.buffer+line)
self.pre_spaces = False
else:
self.logger.log(self.level, f" {self.buffer}") # need space
self.buffer = ""
elif len(line):
self.logger.log(self.level, line)
self.pre_char = line
def flush(self):
pass
class CustomRotatingFileHandler(RotatingFileHandler):
def doRollover(self):
datetime_format = "%Y-%m-%d_%H-%M-%S"
if self.stream:
self.stream.close()
self.stream = None
# remove file older than one month (30 days)
base_filename_no_ext, ext = os.path.splitext(self.baseFilename)
regex = rf"{base_filename_no_ext}-(.*?){ext}"
cut_out_date = datetime.now() - timedelta(days=30)
for root, dirs, files in os.walk(os.path.dirname(self.baseFilename)):
for file in files:
f = os.path.join(root, file)
match = re.match(regex, f)
if match:
try:
date = datetime.strptime(match.group(1), datetime_format)
if date < cut_out_date:
os.remove(f)
except Exception:
# not our file ? keep it
pass
# we can't get the creation date of the file easily, so use the mt_time
# e.g. last log time of the file instead
last_date = datetime.fromtimestamp(int(os.stat(self.baseFilename).st_mtime))
self.rotate(
self.baseFilename,
f"{base_filename_no_ext}-{last_date.strftime(datetime_format)}{ext}",
)
if not self.delay:
self.stream = self._open()
# never do a rollover "live"
def shouldRollover(self, record):
return False
app_logger = logging.getLogger("bike_computer")
# change level in regard to config G_DEBUG
app_logger.setLevel(level=logging.INFO)
# Add simple stream handler
sh = logging.StreamHandler()
app_logger.addHandler(sh)
sys.stdout = StreamToLogger(app_logger, logging.INFO)
sys.stderr = StreamToLogger(app_logger, logging.ERROR)