-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_example.txt
29 lines (24 loc) · 993 Bytes
/
log_example.txt
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
# Uncomment and populate this variable in your code:
# PROJECT = 'The project ID of your Cloud Run service';
# Build structured log messages as an object.
global_log_fields = {}
# Add log correlation to nest all log messages.
# This is only relevant in HTTP-based contexts, and is ignored elsewhere.
# (In particular, non-HTTP-based Cloud Functions.)
request_is_defined = "request" in globals() or "request" in locals()
if request_is_defined and request:
trace_header = request.headers.get("X-Cloud-Trace-Context")
if trace_header and PROJECT:
trace = trace_header.split("/")
global_log_fields[
"logging.googleapis.com/trace"
] = f"projects/{PROJECT}/traces/{trace[0]}"
# Complete a structured log entry.
entry = dict(
severity="NOTICE",
message="This is the default display field.",
# Log viewer accesses 'component' as jsonPayload.component'.
component="arbitrary-property",
**global_log_fields,
)
print(json.dumps(entry))