-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyslogger.py
More file actions
39 lines (30 loc) · 1.02 KB
/
syslogger.py
File metadata and controls
39 lines (30 loc) · 1.02 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
"""
This class enables proper syslog level logging to a journald enabled system
by prefixing all log lines with the syslog level.
See `man systemd.journal-fields` PRIORITY field for a brief description.
"""
import logging
SYSLOG_LEVELS = {
logging.DEBUG: 7,
logging.INFO: 6,
logging.WARNING: 4,
logging.ERROR: 3,
logging.CRITICAL: 2,
logging.FATAL: 1
}
class SyslogFilter(logging.Filter):
"""
Prepends the syslog level corresponding to the python logging log level
to all lines
"""
def filter(self, record):
record.sysloglevel = SYSLOG_LEVELS[record.levelno]
return True
class Syslogger(logging.Logger):
def __init__(self, name=__name__, stream=None, **kwargs):
super().__init__(name, **kwargs)
formatter = logging.Formatter('<%(sysloglevel)d> %(message)s')
handler = logging.StreamHandler(stream=stream) # Allow mocking the output stream
handler.setFormatter(formatter)
self.addHandler(handler)
self.addFilter(SyslogFilter())