-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.py
32 lines (26 loc) · 1.11 KB
/
log.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
import logging.handlers
class Logger:
def __init__(self, filename="log.log", level_string="INFO"):
if level_string == "DEBUG":
self.level = logging.DEBUG
elif level_string == "INFO":
self.level = logging.INFO
elif level_string == "WARNING":
self.level = logging.WARNING
elif level_string == "ERROR":
self.level = logging.ERROR
else:
self.level = logging.NOTSET
self.logger = self.define_logger(filename)
self.logger.info(f"Logging set to {self.level}, {level_string}")
def define_logger(self, filename):
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(self.level)
fileHandler = logging.FileHandler(filename)
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
return rootLogger