9
9
from .types import CLOUD_LOGGING_KEY , SOURCE_LOCATION_KEY
10
10
11
11
12
- class CoreCloudLogging :
13
- """Initialize the Google Cloud Logging event message"""
12
+ def setup_log_severity () -> list [Processor ]:
13
+ return [structlog .processors .add_log_level , LogSeverity ()]
14
+
14
15
15
- def setup ( self ) -> list [Processor ]:
16
- return [
17
- # If some value is in bytes, decode it to a unicode str.
18
- structlog .processors .UnicodeDecoder () ,
19
- # Add a timestamp in ISO 8601 format.
20
- structlog .processors .TimeStamper ( fmt = "iso" ) ,
21
- self ,
16
+ def setup_code_location ( ) -> list [Processor ]:
17
+ call_site_processors = structlog . processors . CallsiteParameterAdder (
18
+ parameters = [
19
+ structlog .processors .CallsiteParameter . PATHNAME ,
20
+ structlog . processors . CallsiteParameter . MODULE ,
21
+ structlog .processors .CallsiteParameter . FUNC_NAME ,
22
+ structlog . processors . CallsiteParameter . LINENO ,
22
23
]
24
+ )
23
25
24
- def __call__ (
25
- self , logger : WrappedLogger , method_name : str , event_dict : EventDict
26
- ) -> EventDict :
27
- value = {
28
- "message" : event_dict .pop ("event" ),
29
- "time" : event_dict .pop ("timestamp" ),
30
- }
26
+ return [call_site_processors , code_location ]
31
27
32
- event_dict [CLOUD_LOGGING_KEY ] = value
33
- return event_dict
28
+
29
+ def init_cloud_logging (
30
+ logger : WrappedLogger , method_name : str , event_dict : EventDict
31
+ ) -> EventDict :
32
+ """Initialize the Google Cloud Logging event message"""
33
+
34
+ value = {
35
+ "message" : event_dict .pop ("event" ),
36
+ "time" : event_dict .pop ("timestamp" ),
37
+ }
38
+
39
+ event_dict [CLOUD_LOGGING_KEY ] = value
40
+ return event_dict
34
41
35
42
36
- class FormatAsCloudLogging :
43
+ def finalize_cloud_logging (
44
+ logger : WrappedLogger , method_name : str , event_dict : EventDict
45
+ ) -> EventDict :
37
46
"""Finalize the Google Cloud Logging event message and replace the logging event.
38
47
39
48
This is not exactly the format the Cloud Logging directly ingests, but
@@ -43,29 +52,28 @@ class FormatAsCloudLogging:
43
52
See: https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
44
53
"""
45
54
46
- def setup ( self ) -> list [ Processor ]:
47
- return [ self ]
55
+ # Take out the Google Cloud Logging set of fields from the event dict
56
+ gcp_event : EventDict = event_dict . pop ( CLOUD_LOGGING_KEY )
48
57
49
- def __call__ (
50
- self , logger : WrappedLogger , method_name : str , event_dict : EventDict
51
- ) -> EventDict :
52
- # Take out the Google Cloud Logging set of fields from the event dict
53
- gcp_event : EventDict = event_dict .pop (CLOUD_LOGGING_KEY )
58
+ # Override whatever is left from the event dict with the content of all
59
+ # the Google Cloud Logging-formatted fields.
60
+ event_dict .update (gcp_event )
54
61
55
- # Override whatever is left from the event dict with the content of all
56
- # the Google Cloud Logging-formatted fields.
57
- event_dict .update (gcp_event )
62
+ # Fields which are not known by Google Cloud Logging will be added to
63
+ # the `jsonPayload` field.
64
+ #
65
+ # See the `message` field documentation in:
66
+ # https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
58
67
59
- # Fields which are not known by Google Cloud Logging will be added to
60
- # the `jsonPayload` field.
61
- # See the `message` field documentation in:
62
- # https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
63
-
64
- return event_dict
68
+ return event_dict
65
69
66
70
67
71
class LogSeverity :
68
- """Set the severity using the Google Cloud Logging severities"""
72
+ """Set the severity using the Google Cloud Logging severities.
73
+
74
+
75
+ See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
76
+ """
69
77
70
78
def __init__ (self ) -> None :
71
79
self .default = "notset"
@@ -84,17 +92,10 @@ def __init__(self) -> None:
84
92
# "emergency": "EMERGENCY", # One or more systems are unusable.
85
93
}
86
94
87
- def setup (self ) -> list [Processor ]:
88
- # Add log level to event dict.
89
- return [structlog .processors .add_log_level , self ]
90
-
91
95
def __call__ (
92
96
self , logger : WrappedLogger , method_name : str , event_dict : EventDict
93
97
) -> EventDict :
94
- """Format a Python log level value as a GCP log severity.
95
-
96
- See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
97
- """
98
+ """Format a Python log level value as a GCP log severity."""
98
99
99
100
log_level = event_dict .pop ("level" )
100
101
severity = self .mapping .get (log_level , self .default )
@@ -103,30 +104,17 @@ def __call__(
103
104
return event_dict
104
105
105
106
106
- class CodeLocation :
107
+ def code_location (
108
+ logger : WrappedLogger , method_name : str , event_dict : EventDict
109
+ ) -> EventDict :
107
110
"""Inject the location of the logging message into the logs"""
108
111
109
- def setup (self ) -> list [Processor ]:
110
- # Add callsite parameters.
111
- call_site_proc = structlog .processors .CallsiteParameterAdder (
112
- parameters = [
113
- structlog .processors .CallsiteParameter .PATHNAME ,
114
- structlog .processors .CallsiteParameter .MODULE ,
115
- structlog .processors .CallsiteParameter .FUNC_NAME ,
116
- structlog .processors .CallsiteParameter .LINENO ,
117
- ]
118
- )
119
- return [call_site_proc , self ]
112
+ location = {
113
+ "file" : event_dict .pop ("pathname" ),
114
+ "line" : str (event_dict .pop ("lineno" )),
115
+ "function" : f"{ event_dict .pop ('module' )} :{ event_dict .pop ('func_name' )} " ,
116
+ }
120
117
121
- def __call__ (
122
- self , logger : WrappedLogger , method_name : str , event_dict : EventDict
123
- ) -> EventDict :
124
- location = {
125
- "file" : event_dict .pop ("pathname" ),
126
- "line" : str (event_dict .pop ("lineno" )),
127
- "function" : f"{ event_dict .pop ('module' )} :{ event_dict .pop ('func_name' )} " ,
128
- }
129
-
130
- event_dict [CLOUD_LOGGING_KEY ][SOURCE_LOCATION_KEY ] = location
118
+ event_dict [CLOUD_LOGGING_KEY ][SOURCE_LOCATION_KEY ] = location
131
119
132
- return event_dict
120
+ return event_dict
0 commit comments