Overview and What You Will Learn
In this lab, you will generate log entries from a VM, write increasingly specific query filters to narrow down to exactly the relevant entries, and configure a log sink routing specific logs to BigQuery for long-term, queryable retention.
Why This Matters in Production
An engineer is told "checkout is slow sometimes," with no more specific information than that. Without the ability to actually filter logs - by resource, by severity, by a specific time window - finding the actual root cause becomes a matter of scrolling through a huge volume of raw text manually. Cloud Logging's filter syntax turns that into a precise, repeatable question asked directly against the data.
Core Principles
Cloud Logging collects log entries from every GCP service and your own applications into a searchable, filterable store - and its query filter syntax is the tool that turns "something is wrong" into "here is the exact log entry explaining why."
+------------------------------------------+| Resources emit log entries continuously |+------------------------------------------+ | v+------------------------------------------+| Cloud Logging stores and indexes them |+------------------------------------------+ | v+------------------------------------------+| Query filters narrow down to exactly the || relevant entries by resource, severity, || time range, or specific field values |+------------------------------------------+ | v+------------------------------------------+| Log Sinks can route matching entries to || BigQuery, Cloud Storage, or Pub/Sub for || long-term retention or further processing |+------------------------------------------+Detailed Step-by-Step Practical Lab
- Create a project and a VM that will generate some log activity:
gcloud projects create gcp-logging-lab-2026 --name="Cloud Logging Lab"gcloud config set project gcp-logging-lab-2026gcloud services enable compute.googleapis.com logging.googleapis.com gcloud compute instances create vm-logging-test \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud- Start with the simplest possible query - view recent logs for this specific VM:
gcloud logging read \ 'resource.type="gce_instance" AND resource.labels.instance_id="'$(gcloud compute instances describe vm-logging-test --zone=asia-south1-a --format="value(id)")'"' \ --limit=10- Narrow the query to only error-severity entries from the last hour:
gcloud logging read \ 'severity>=ERROR' \ --freshness=1h- Combine multiple conditions - error-severity entries specifically for Compute Engine resources:
gcloud logging read \ 'resource.type="gce_instance" AND severity>=ERROR' \ --freshness=24h \ --limit=20- Create a BigQuery dataset to serve as a log sink destination for long-term retention:
bq mk --dataset --location=asia-south1 gcp-logging-lab-2026:log_archive- Create a Log Sink routing error-severity logs to that BigQuery dataset:
gcloud logging sinks create error-logs-to-bigquery \ bigquery.googleapis.com/projects/gcp-logging-lab-2026/datasets/log_archive \ --log-filter='severity>=ERROR'- Grant the sink's service account permission to write to the BigQuery dataset:
SINK_SA=$(gcloud logging sinks describe error-logs-to-bigquery --format="value(writerIdentity)") bq add-iam-policy-binding \ --member="$SINK_SA" \ --role="roles/bigquery.dataEditor" \ gcp-logging-lab-2026:log_archiveNoteEvery Log Sink has its own dedicated service account (the
writerIdentity) used specifically to write matching logs to the destination - this identity must be explicitly granted write permission on the destination, since the sink itself doesn't automatically have access just by existing.
- Clean up:
gcloud logging sinks delete error-logs-to-bigquery --quietbq rm -r -f -d gcp-logging-lab-2026:log_archivegcloud compute instances delete vm-logging-test --zone=asia-south1-a --quietgcloud projects delete gcp-logging-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeRelying solely on Cloud Logging's default retention window for logs that genuinely need long-term retention for compliance or historical analysis. Cloud Logging's own storage has a default retention period - a Log Sink routing relevant logs to BigQuery or Cloud Storage is necessary for retention beyond that default window.
TipBuild query filters incrementally - start broad, confirm results look reasonable, then add conditions one at a time (resource type, severity, specific field values) rather than trying to write the complete, precise filter in one attempt.
- Log Sinks only route logs matching their filter going forward from creation, not retroactively. Historical logs that existed before the sink was created are not backfilled into the new destination automatically.
- Audit Logs are a specific category within Cloud Logging worth filtering for separately during a security investigation -
logName:"cloudaudit.googleapis.com"narrows a query specifically to administrative and data-access audit entries, distinct from regular application or system logs.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud logging read |
Query and filter log entries |
gcloud logging sinks create |
Route matching logs to an external destination |
gcloud logging sinks describe --format="value(writerIdentity)" |
Get a sink's service account for permission grants |
gcloud logging read --freshness= |
Limit query results to a recent time window |