-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.py
51 lines (42 loc) · 1.73 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
import logging
class Log:
def __init__(self,file):
self.file = file
self.logger = logging.getLogger(__file__)
self.logger.setLevel(logging.INFO)
self.file_handler = logging.FileHandler(self.file,encoding='utf-8')
self.formatter = logging.Formatter('%(levelname)s: %(name)s:\n\t%(message)s')
self.file_handler.setFormatter(self.formatter)
self.logger.addHandler(self.file_handler)
self.handlers = []
def setLevel(self,level=int):
if level == 0:
self.formatter = logging.Formatter('%(levelname)s: %(name)s:\n%(message)s')
elif level == 1:
self.formatter = logging.Formatter('\tl1 %(message)s')
elif level == 2:
self.formatter = logging.Formatter('\t\tl2 %(message)s')
elif level == 3:
self.formatter = logging.Formatter('\t\t\tl3 %(message)s')
elif level == 4:
self.formatter = logging.Formatter('\t\t\tl4 %(message)s')
self.file_handler.setFormatter(self.formatter)
self.logger.addHandler(self.file_handler)
def print(self,message):
self.logger.info(message)
def debug(self):
self.logger.debug('debug')
def clearFile(self):
open(self.file,'w').close()
def addHandler(self,file):
self.file_handler = logging.FileHandler(file , encoding='utf-8')
self.logger.addHandler(self.file_handler)
self.handlers.append(self.file_handler)
self.file = file
def removeHandler(self):
self.file_handler.close()
self.logger.removeHandler(self.file_handler)
def removeAllHandlers(self):
for handler in self.handlers:
handler.close()
self.logger.removeHandler(handler)