-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.py
50 lines (39 loc) · 1.28 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
import logging
import config
from os import mkdir
class Logger():
def __init__(self, mod_name, to_file=True):
self._CreateLogDir()
self.mod_name = mod_name
self.logger = logging.getLogger(mod_name)
self.logger.setLevel(config.log_level)
self.logger.addHandler(self._GetConsoleHandler())
if to_file:
self.logger.addHandler(self._GetFileHandler())
def _CreateLogDir(self):
try:
mkdir(config.log_dir)
except FileExistsError:
pass
def _GetConsoleHandler(self):
formatter = logging.Formatter(config.console_log_format)
ch = logging.StreamHandler()
ch.setFormatter(formatter)
return ch
def _GetFileHandler(self):
formatter = logging.Formatter(config.file_log_format)
fh = logging.FileHandler("{}/spyrai-{}.log".format(config.log_dir,
self.mod_name))
fh.setFormatter(formatter)
fh.setLevel(config.log_level)
return fh
def debug(self, buf):
self.logger.debug(buf)
def info(self, buf):
self.logger.info(buf)
def warn(self, buf):
self.logger.warn(buf)
def error(self, buf):
self.logger.error(buf)
def critical(self, buf):
self.logger.critical(buf)