-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogs.py
More file actions
38 lines (29 loc) · 1.05 KB
/
Copy pathlogs.py
File metadata and controls
38 lines (29 loc) · 1.05 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
"""
Logging
"""
import datetime as dt
LOG_FMT = '%Y-%m-%d %H:%M:%S'
class Logger:
"""Used log messages to a log file"""
def __init__(self, name: str, filename: str, log_fmt: str=LOG_FMT) -> None:
"""Initializes this Logger with the given name, filename, and format"""
self.name = name
self.filename = filename
self.log_fmt = log_fmt
self._last_end = '\n'
def clear(self) -> None:
"""Clears the log file"""
print('Clearing log {}...'.format(self.name))
with open(self.filename, 'w'):
pass
def log(self, message: str, end: str='\n') -> None:
"""Adds the given log message to the log file"""
if self._last_end == '\n':
now = dt.datetime.now()
prefix = now.strftime(self.log_fmt) + ' '
else:
prefix = ''
with open(self.filename, 'a+', encoding='utf-8') as fp:
fp.write(prefix + message + end)
self._last_end = end
print('Logged message "{}..." for {}'.format(message[:20], self.name))