Coverage for mflog/processors.py: 96%

37 statements  

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

1# -*- coding: utf-8 -*- 

2 

3import os 

4import structlog 

5from mflog.utils import level_name_to_level_no, get_level_no_from_logger_name 

6from mflog.utils import get_extra_context 

7 

8 

9def fltr(logger, method_name, event_dict): 

10 """Filter log messages.""" 

11 method_level_no = level_name_to_level_no(method_name) 

12 logger_level_no = \ 

13 get_level_no_from_logger_name(event_dict.get('name', '')) 

14 if method_level_no < logger_level_no: 

15 raise structlog.DropEvent 

16 return event_dict 

17 

18 

19def add_level(logger, method_name, event_dict): 

20 """Add the severity in the event_dict.""" 

21 event_dict['level'] = method_name.lower() 

22 return event_dict 

23 

24 

25def add_pid(logger, method_name, event_dict): 

26 """Add the current pid in the event dict.""" 

27 event_dict['pid'] = os.getpid() 

28 return event_dict 

29 

30 

31def add_extra_context(logger, method_name, event_dict): 

32 """Add extra context in the event dict.""" 

33 extra_context = get_extra_context() 

34 event_dict.update(extra_context) 

35 return event_dict 

36 

37 

38def add_exception_info(logger, method_name, event_dict): 

39 exc_info = event_dict.pop("exc_info", None) 

40 if exc_info: 

41 e = structlog.processors._figure_out_exc_info(exc_info) 

42 if e[0] is not None: 42 ↛ 53line 42 didn't jump to line 53, because the condition on line 42 was never false

43 event_dict["exception"] = structlog._frames._format_exception(e) 

44 event_dict["exception_type"] = e[0].__name__ 

45 event_dict["exception_file"] = e[-1].tb_frame.f_code.co_filename 

46 event = event_dict.get("event", "") 

47 if isinstance(event, Exception): 

48 # see issue #3 

49 # => we convert the exception object in string to avoid 

50 # json serializing issues 

51 event_dict["event"] = str(event) 

52 else: 

53 event_dict["exception"] = None 

54 return event_dict 

55 return event_dict 

56 

57 

58def kv_renderer(logger, method_name, event_dict): 

59 ordered_items = sorted(event_dict.items()) 

60 return " ".join(["%s=%s" % (k, v) for k, v in ordered_items])