-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
85 lines (56 loc) · 1.77 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
import logging
class Logger:
""" Logger class
"""
def __init__(self, logger_name: str, file: str, level=logging.DEBUG) -> None:
""" Initialise Loger
Args:
logger_name (str): Logger name
file (str): File to save logging information to
level (int, optional): Logging level. Defaults to logging.DEBUG.
"""
self._log = logging.getLogger(logger_name)
self._log.propagate = False
self._log.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = logging.FileHandler(file)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
self._log.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
self._log.addHandler(ch)
def debug(self, message: str):
""" Debug log message
Args:
message (str): Log message
"""
self._log.debug(message)
self._exit()
def info(self, message: str):
""" Info log message
Args:
message (str): Log message
"""
self._log.info(message)
self._exit()
def warning(self, message: str):
""" Warning log message
Args:
message (str): Log message
"""
self._log.warning(message)
self._exit()
def error(self, message: str):
""" Error log message
Args:
message (str): Log message
"""
self._log.error(message)
self._exit()
def _exit(self):
""" Clear log handlers
"""
if self._log.hasHandlers():
self._log.handlers.clear()