Coverage for mflog/syslog.py: 46%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-11-25 11:58 +0000

1from logging.handlers import SysLogHandler 

2from logging import LogRecord 

3import json 

4 

5 

6class SyslogLoggerMsgOnlyFormatter(object): 

7 

8 def format(self, record): 

9 return record.msg['event'] 

10 

11 

12class SyslogLoggerJSONFormatter(object): 

13 

14 def format(self, record): 

15 return json.dumps(record.msg) 

16 

17 

18class SyslogLogger(object): 

19 

20 __syslog_handler = None 

21 

22 def __init__(self, address, frmt=None): 

23 self.__syslog_handler = SysLogHandler(address) 

24 if frmt is None or frmt == "msg_only": 

25 self.__syslog_handler.formatter = SyslogLoggerMsgOnlyFormatter() 

26 else: 

27 self.__syslog_handler.formatter = SyslogLoggerJSONFormatter() 

28 

29 def close(self): 

30 self.__syslog_handler.close() 

31 

32 def msg(self, event_dict): 

33 record = LogRecord(event_dict.get("name", "unknown"), 

34 event_dict.get("level", "WARNING"), 

35 "/not_used/not_used.py", 1, 

36 event_dict, [], None) 

37 self.__syslog_handler.acquire() 

38 try: 

39 self.__syslog_handler.emit(record) 

40 finally: 

41 self.__syslog_handler.release()