Coverage for structlog_google_cloud_logging/gcp.py: 0%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-04-01 23:00 +0200

1def format_severity(value, default="DEFAULT"): 

2 """Format a Python log level value as a GCP log severity. 

3 

4 See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity 

5 """ 

6 

7 # From Python's logging level to Google level 

8 mapping = { 

9 "notset": "DEFAULT", # The log entry has no assigned severity level. 

10 "debug": "DEBUG", # Debug or trace information. 

11 "info": "INFO", # Routine information, such as ongoing status or performance. 

12 # "notice": "NOTICE", # Normal but significant events, such as start up, shut down, or a configuration change. 

13 "warn": "WARNING", # Warning events might cause problems. 

14 "warning": "WARNING", # Warning events might cause problems. 

15 "error": "ERROR", # Error events are likely to cause problems. 

16 "critical": "CRITICAL", # Critical events cause more severe problems or outages. 

17 # "alert": "ALERT", # A person must take an action immediately. 

18 # "emergency": "EMERGENCY", # One or more systems are unusable. 

19 } 

20 

21 return mapping.get(value, default) 

22 

23 

24def add_code_location(logger, method_name, event_dict): 

25 location = {} 

26 result = {} 

27 

28 if "pathname" in event_dict: 

29 location["file"] = event_dict.pop("pathname") 

30 

31 if "module" in event_dict and "func_name" in event_dict: 

32 function = f"{event_dict.pop('module')}:{event_dict.pop('func_name')}" 

33 location["function"] = function 

34 

35 if "lineno" in event_dict: 

36 location["line"] = str(event_dict.pop("lineno")) 

37 else: 

38 if location: 

39 location["line"] = "0" # 0 == no line number available 

40 

41 if location: 

42 key = "logging.googleapis.com/sourceLocation" 

43 result = {key: location} 

44 

45 return event_dict, result