-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
103 lines (83 loc) · 3.61 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
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
import syslog
class Logger:
EMERG = 'EMERG'
ALERT = 'ALERT'
CRIT = 'CRIT'
ERR = 'ERR'
WARNING = 'WARNING'
NOTICE = 'NOTICE'
INFO = 'INFO'
DEBUG = 'DEBUG'
def __init__(self, program, mudule, min_priority=None):
"""
Given a identity string and a min priority string create a logger
instance. The priority string must be one of ...
EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
args : ident ... loggers identity
min_priority ... min report priority EMERG, ALERT, CRIT, ERR,
WARNING, NOTICE, INFO or DEBUG
excepts :
return : none
"""
# 'min_priority' is the min priority level at which events will be sent
# to syslog, it must be one of ... EMERG, ALERT, CRIT, ERR, WARNING,
# NOTICE, INFO, DEBUG
self.case = {Logger.EMERG: syslog.LOG_EMERG,
Logger.ALERT: syslog.LOG_ALERT,
Logger.CRIT: syslog.LOG_CRIT,
Logger.ERR: syslog.LOG_ERR,
Logger.WARNING: syslog.LOG_WARNING,
Logger.NOTICE: syslog.LOG_NOTICE,
Logger.INFO: syslog.LOG_INFO,
Logger.DEBUG: syslog.LOG_DEBUG}
self.module = mudule
self.program = program
if min_priority is None:
min_priority = Logger.NOTICE
self.min_priority = min_priority
def set_prority(self, min_priority):
"""
Given the min priority string modify the classes min priority value. The
priority string must be one of EMERG, ALERT, CRIT, ERR, WARNING, NOTICE,
INFO, DEBUG
args : min_priority ... min report priority EMERG, ALERT, CRIT, ERR,
WARNING, NOTICE, INFO or DEBUG
excepts :
return : none
"""
self.min_priority = min_priority
def __call__(self, msg, priority=None):
self.log(msg, priority)
def log(self, msg, priority=None):
"""
Log an message string with a certain priority string. If that priority
is greater than the pre-defined min priority log the message to
/var/log/messages. The priority string must be one of EMERG, ALERT,
CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
args : msg ... message to be logged
priority ... priority of the msg EMERG, ALERT, CRIT, ERR,
WARNING, NOTICE, INFO or DEBUG
excepts :
return : none
"""
# 'priority' is the actual level of the event, it must be one of ...
# EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
# 'msg' will only be sent to syslog if 'priority' >= 'min_priority'
# TODO: The Python syslog module is very broken - logging priorities are
# ignored, this is a workaround ...
if priority is None:
priority = Logger.NOTICE
msg = "{0}: [{1}] {2}".format(priority, self.module, msg)
if self.case[priority] <= self.case[self.min_priority]:
syslog.openlog(self.program, syslog.LOG_PID)
syslog.syslog(msg)
syslog.closelog()
def d(self, msg):
self.log(msg, Logger.DEBUG)
def w(self, msg):
self.log(msg, Logger.WARNING)
def e(self, msg):
self.log(msg, Logger.CRIT)
if __name__ == "__main__":
log = Logger('test', 'Test', Logger.DEBUG)
log.d('test')