-
Notifications
You must be signed in to change notification settings - Fork 15
/
log.py
53 lines (45 loc) · 1.73 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging, sys
from logging.handlers import RotatingFileHandler
def filter_status(record):
""""
Only displays log messages about status info
or ERROR level
"""
if ("Status:" in str(record.msg)) or (record.levelname == 'ERROR'):
return 1
else:
return 0
def log(logFile, level_input="WARNING", terminal_output=False):
if level_input == "NOTSET":
level = logging.NOTSET
if level_input == "DEBUG":
level = logging.DEBUG
if level_input == "INFO":
level = logging.INFO
if level_input == "WARNING":
level = logging.WARNING
if level_input == "ERROR":
level = logging.ERROR
if level_input == "CRITICAL":
level = logging.CRITICAL
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=5 * 1024 * 1024, backupCount=2, encoding="utf-8", delay=0)
my_handler.setFormatter(log_formatter)
my_handler.setLevel(level)
app_log = logging.getLogger('root')
app_log.setLevel(level)
app_log.addHandler(my_handler)
# This part is what goes on console.
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(level)
# TODO: We could have 2 level in the config, one for screen and one for files.
print ("Logging level: {} ({})".format(level_input,level))
if terminal_output != True:
ch.addFilter(filter_status)
# No need for complete func and line info here.
formatter = logging.Formatter('%(asctime)s %(message)s')
else:
formatter = logging.Formatter('%(asctime)s %(funcName)s(%(lineno)d) %(message)s')
ch.setFormatter(formatter)
app_log.addHandler(ch)
return app_log