Associate Cloud Engineer ยท Logging and audit logs
How do you filter logs in Logs Explorer and from the CLI?
Logs Explorer, the query language (resource.type, severity, textPayload), gcloud logging read, inspecting a log entry's details.
Key points
- Write a query of field comparisons such as resource.type="gce_instance" severity>=ERROR textPayload:"timeout".
- Comparisons next to each other are joined by AND; AND, OR and NOT must be capitalized.
- The colon does a substring match.
- From the CLI, gcloud logging read "<filter>" returns entries; by default only the last day (--freshness=1d), newest first, and you can add --limit and --format=json.
Source: https://docs.cloud.google.com/logging/docs/view/logging-query-language
Practice questions
Which field do you use in a Logs Explorer query to restrict results to a kind of monitored resource, such as a VM instance?
- textPayload
- severity
- resource.type
- timestamp
Show answer
Answer: C. resource.type
resource.type (for example gce_instance) narrows entries to one monitored resource type and also speeds the query up.
Which statements about the Logging query language are correct?
- AND can be omitted between two comparisons
- textPayload:"timeout" matches entries containing that text
- AND, OR and NOT must be written in capitals
- OR has higher precedence than NOT
- Lowercase and/or work the same as AND/OR
Show answer
Answer: A. AND can be omitted between two comparisons, B. textPayload:"timeout" matches entries containing that text, C. AND, OR and NOT must be written in capitals
Adjacent comparisons are ANDed, the colon is a substring match, and logical operators must be uppercase. NOT binds tighter than OR, not the other way round, and lowercase operators are not valid.
True or false: gcloud logging read searches the entire retention period of the bucket by default.
Show answer
Answer: False
False. The default --freshness is 1d, so only entries from roughly the last day are returned unless you widen it.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
