-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
110 lines (77 loc) · 2.77 KB
/
Copy pathlog.py
File metadata and controls
110 lines (77 loc) · 2.77 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import logging
import logging.handlers
# In production env, log file could be very large. Using TimedRotatingFileHandler to generate log file in terms of date.
class CRootLog:
"""
Root logger Class: log to one file only.
"""
fmt_string = '[%(asctime)s] ' \
'%(processName)s(%(process)d) > %(threadName)s(%(thread)d) ' \
'%(name)s <%(levelname)s> ' \
'%(message)s'
def __init__(self):
logging.basicConfig(filename="log/root.log", filemode="a", format=CRootLog.fmt_string,
datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG, )
def d(self, msg):
logging.debug(msg)
def i(self, msg):
logging.info(msg)
def w(self, msg):
logging.warning(msg)
def e(self, msg):
logging.error(msg)
def c(self, msg):
logging.critical(msg)
class CUserLog:
"""
User logger Class: log to files only. file names are the users' names
"""
def __init__(self, logger_name):
self.logger = logging.getLogger(logger_name)
self.logger.setLevel(logging.INFO)
hd_file_d_1 = logging.FileHandler(filename="log/Usr_{0}.ULOG".format(logger_name))
hd_file_d_1.setLevel(logging.INFO)
formatter = logging.Formatter(CRootLog.fmt_string)
hd_file_d_1.setFormatter(formatter)
self.logger.addHandler(hd_file_d_1)
def d(self, msg):
self.logger.debug(msg)
def i(self, msg):
self.logger.info(msg)
def w(self, msg):
self.logger.warning(msg)
def e(self, msg):
self.logger.error(msg)
def c(self, msg):
self.logger.critical(msg)
# use classmethod if i have time to do that.
class CStreamLog:
"""
Debug logger: only log msg to terminals.
"""
fmt_string = CRootLog.fmt_string
def __init__(self):
self.logger = logging.getLogger('debug_stream')
self.hd_stream_d_1 = logging.StreamHandler()
self.logger.setLevel(logging.DEBUG)
self.hd_stream_d_1.setLevel(logging.DEBUG)
self.formatter = logging.Formatter(self.fmt_string)
self.hd_stream_d_1.setFormatter(self.formatter)
self.logger.addHandler(self.hd_stream_d_1)
def d(self, msg):
self.logger.debug(msg)
def i(self, msg):
self.logger.info(msg)
def w(self, msg):
self.logger.warning(msg)
def e(self, msg):
self.logger.error(msg)
def c(self, msg):
self.logger.critical(msg)
if __name__ == "__main__":
r_logger = CUserLog('test')
r_logger.d('debug msg')
r_logger.i('info msg')
r_logger.w('warning msg')
r_logger.e('error msg')
r_logger.c('critical msg')