-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
34 lines (26 loc) · 909 Bytes
/
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
33
34
import logging
# create logger
def getLogger(name='logger', level='INFO', mode='w', fh=True, ch=True, prefix=""):
logger = logging.getLogger(name)
fh = logging.FileHandler(name + '.log', mode)
ch = logging.StreamHandler()
if level == "INFO":
logger.setLevel(logging.INFO)
fh.setLevel(logging.INFO)
ch.setLevel(logging.INFO)
elif level == "DEBUG":
logger.setLevel(logging.DEBUG)
fh.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
elif level == "ERROR":
logger.setLevel(logging.ERROR)
fh.setLevel(logging.ERROR)
ch.setLevel(logging.ERROR)
formatter = logging.Formatter(prefix + ' ' + '%(filename)s:%(lineno)s %(levelname)s: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
if fh:
logger.addHandler(fh)
if ch:
logger.addHandler(ch)
return logger