-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
102 lines (85 loc) · 2.74 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
import os
import arrow
import traceback
LOG_FOLDER = 'log'
MS_ENABLED = False # define whether milliseconds enabled or not
BEEP_ENABLED = True # beep sound for windows
if BEEP_ENABLED:
import winsound
def init():
"""Initialize log system for use in other modules."""
global log
log = logger()
class logger:
"""Tool to work with logs. File names: 'DD_MM_YY'."""
def __init__(self):
if not os.path.exists(LOG_FOLDER):
os.mkdir(LOG_FOLDER)
def _time(self) -> str:
"""Get current time."""
if MS_ENABLED:
now = arrow.utcnow()
now_time = now.format('HH:mm:ss')
millis = round(arrow.utcnow().float_timestamp, 3)
millis -= int(millis)
return now_time + str(millis)[1:4]
return arrow.now().format('HH:mm:ss')
def _date(self) -> str:
"""Get current date."""
return arrow.now().format('DD_MM_YY')
def _join_words(self, words: tuple, sep: str) -> str:
"""Make a string from words."""
text = ''
for i in words:
text += str(i)+sep
if sep:
text = text[:-len(sep)]
return text
def write(self, *words, sep = ' ', end = '\n') -> None:
"""Write text to log."""
LOG_FILENAME = self._date() + '.log'
LOG_PATH = os.path.join(LOG_FOLDER, LOG_FILENAME)
text = self._join_words(words, sep)
if not os.path.exists(LOG_PATH):
mode = 'w'
else:
mode = 'a'
with open(LOG_PATH, mode, encoding='UTF-8') as log:
log.write(f'[{self._time()}] {text}{end}')
def error(self, e: Exception) -> None:
"""Write error to log.
Use when handling not specified errors."""
if BEEP_ENABLED: self.beep(True)
text = 'ERROR!'
exc_text = ''.join(traceback.format_exception(e))
self.write(text, exc_text, sep='\n')
def warning(self, *words, sep = ' ', end = '\n') -> None:
"""Write text to log with a warning.
Use when handling specified errors."""
if BEEP_ENABLED: self.beep(False)
words = ('WARNING!',) + words
self.write(*words, sep=sep, end=end)
def beep(self, is_error: bool) -> None:
"""Beep Boop!"""
if is_error:
for _ in range(4):
winsound.Beep( 1010, 400 )
else:
for _ in range(2):
winsound.Beep( 1010, 250 )
def _test():
"""Developer function."""
import time
l = logger()
print(l._time())
print(l._date())
l.write('abc1')
l.write('abc2')
try:
1 / 0
except Exception as e:
l.error(e)
time.sleep(1)
l.warning('this is a warning')
if __name__ == '__main__':
_test()