-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.py
More file actions
79 lines (66 loc) · 2.42 KB
/
Copy pathlog.py
File metadata and controls
79 lines (66 loc) · 2.42 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import logging
import logging.config
import os
from colorlog import ColoredFormatter
def create_console_handler(verbose_level):
"""
Set up the console logging for a transaction processor.
Args:
verbose_level (int): The log level that the console should print out
"""
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s.%(msecs)03d "
"%(levelname)-8s %(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
if verbose_level == 0:
clog.setLevel(logging.WARN)
elif verbose_level == 1:
clog.setLevel(logging.INFO)
else:
clog.setLevel(logging.DEBUG)
return clog
def init_console_logging(verbose_level=2):
"""
Set up the console logging for a transaction processor.
Args:
verbose_level (int): The log level that the console should print out
"""
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(create_console_handler(verbose_level))
def log_configuration(log_config=None, log_dir=None, name=None):
"""
Sets up the loggers for a transaction processor.
Args:
log_config (dict): A dictinary of log config options
log_dir (string): The log directory's path
name (string): The name of the expected logging file
"""
if log_config is not None:
logging.config.dictConfig(log_config)
else:
log_filename = os.path.join(log_dir, name)
debug_handler = logging.FileHandler(log_filename + "-debug.log")
debug_handler.setFormatter(logging.Formatter(
'[%(asctime)s.%(msecs)03d [%(threadName)s] %(module)s'
' %(levelname)s] %(message)s', "%H:%M:%S"))
debug_handler.setLevel(logging.DEBUG)
error_handler = logging.FileHandler(log_filename + "-error.log")
error_handler.setFormatter(logging.Formatter(
'[%(asctime)s.%(msecs)03d [%(threadName)s] %(module)s'
' %(levelname)s] %(message)s', "%H:%M:%S"))
error_handler.setLevel(logging.ERROR)
logging.getLogger().addHandler(error_handler)
logging.getLogger().addHandler(debug_handler)