Overview and What You Will Learn
A Cloud Logging query filter is a short expression that selects log entries by resource, severity, log name, time window or field value, so you can go from "something is wrong" to one exact log entry. In this lab you will write custom log entries, build a filter step by step, search audit logs, and create a log sink that routes error entries to BigQuery for long-term retention.
- Writing test log entries with a chosen severity
- Building a filter incrementally with
AND,:and>= - Filtering by resource type and searching Admin Activity audit logs
- Creating a log sink to BigQuery with partitioned tables
- Granting the sink's writer identity access to the destination
Why This Matters in Production
Precise filters turn a vague report into a root cause in minutes instead of hours. Imagine a payments team at a company like Razorpay told only that "checkout is slow sometimes". Without filters on resource, severity and time window, an engineer scrolls through millions of raw lines. With a filter, the same question becomes one repeatable query, and a sink keeps the evidence long after the default retention period ends.
Core Principles
Cloud Logging collects entries from every GCP service and from your own applications into one searchable store. A query filter narrows that store to the entries you care about, and a log sink copies matching entries to another destination for retention or analysis.
A filter is a set of comparisons joined by AND, OR and NOT. The = operator matches exactly, : matches a substring, and >= compares severity levels such as WARNING, ERROR and CRITICAL.
Detailed Step-by-Step Practical Lab
- Create a project, link billing, enable the APIs, and start a VM so the project produces audit log activity:
gcloud projects create mumbai-logging-lab-2026 --name="Cloud Logging Lab"gcloud billing projects link mumbai-logging-lab-2026 \ --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud config set project mumbai-logging-lab-2026 gcloud services enable compute.googleapis.com logging.googleapis.com bigquery.googleapis.com gcloud compute instances create mumbai-prod-vm-1 \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud- Write two custom entries that imitate a checkout service, one healthy and one failing:
gcloud logging write checkout-service-log \ "order ORD-48214 confirmed in 210ms" --severity=INFO gcloud logging write checkout-service-log \ "payment-gateway timeout after 30s order=ORD-48213" --severity=ERROR- Start with the broadest useful query, every entry in one named log:
gcloud logging read \ 'logName="projects/mumbai-logging-lab-2026/logs/checkout-service-log"' \ --limit=10 --freshness=1h- Narrow it to errors, then add a text match on the message:
gcloud logging read \ 'logName="projects/mumbai-logging-lab-2026/logs/checkout-service-log" AND severity>=ERROR' \ --limit=10 --freshness=1h gcloud logging read \ 'logName="projects/mumbai-logging-lab-2026/logs/checkout-service-log" AND severity>=ERROR AND textPayload:"timeout"' \ --limit=10 --freshness=1h- Filter by resource type to find error entries produced by Compute Engine VMs:
gcloud logging read \ 'resource.type="gce_instance" AND severity>=ERROR' \ --freshness=24h --limit=20- Search the audit logs to find who created the VM, the usual first question in a security investigation:
gcloud logging read \ 'logName:"cloudaudit.googleapis.com" AND protoPayload.methodName:"instances.insert"' \ --limit=5 \ --format="value(timestamp,protoPayload.authenticationInfo.principalEmail)"- Create a BigQuery dataset and a sink that routes error entries to it. The
--use-partitioned-tablesflag keeps one table per log instead of one table per day:
bq mk --dataset --location=asia-south1 mumbai-logging-lab-2026:log_archive gcloud logging sinks create error-logs-to-bigquery \ bigquery.googleapis.com/projects/mumbai-logging-lab-2026/datasets/log_archive \ --log-filter='severity>=ERROR' \ --use-partitioned-tables- Grant the sink's writer identity permission to write. Each sink has its own service account, and it has no access to the destination until you grant it:
SINK_SA=$(gcloud logging sinks describe error-logs-to-bigquery \ --format="value(writerIdentity)") # A project-level grant keeps the lab short. In production, scope it to the dataset only.gcloud projects add-iam-policy-binding mumbai-logging-lab-2026 \ --member="$SINK_SA" \ --role="roles/bigquery.dataEditor"- Write a new error entry and confirm it reaches BigQuery. Tables can take a few minutes to appear:
gcloud logging write checkout-service-log \ "payment-gateway timeout after 30s order=ORD-48215" --severity=ERROR sleep 120bq ls mumbai-logging-lab-2026:log_archive bq query --use_legacy_sql=false \ 'SELECT timestamp, severity, textPayload FROM `mumbai-logging-lab-2026.log_archive.checkout_service_log` ORDER BY timestamp DESC LIMIT 5'- Clean up:
gcloud logging sinks delete error-logs-to-bigquery --quietbq rm -r -f -d mumbai-logging-lab-2026:log_archivegcloud compute instances delete mumbai-prod-vm-1 --zone=asia-south1-a --quietgcloud projects delete mumbai-logging-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeRelying on default retention for logs you must keep for compliance or history. The
_Defaultlog bucket keeps entries for 30 days unless you change it, so route those logs to BigQuery or Cloud Storage with a sink, or raise the bucket's retention.
TipBuild filters incrementally. Start broad, confirm the results look right, then add one condition at a time (resource type, severity, field value) instead of writing the final filter in one attempt.
SecurityData Access audit logs are off by default for most services, so enable them before an incident, not during one. Audit logs show who did what, which is only useful if they were being recorded.
- Remember that sinks are not retroactive. A sink only routes entries written after it is created, so historical logs are not backfilled.
- Write the sink filter as tightly as the query. A sink with an empty filter routes everything, which can create a large BigQuery bill. Use
severity>=ERRORor a specificlogName. - Grant the minimum role on the destination. Use
roles/bigquery.dataEditorfor BigQuery,roles/storage.objectCreatorfor Cloud Storage androles/pubsub.publisherfor Pub/Sub. - Separate audit logs from application logs.
logName:"cloudaudit.googleapis.com"isolates administrative and data-access entries during security work. - Prefer partitioned tables for BigQuery sinks. Date-sharded tables multiply quickly and are harder to query across days.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud logging read 'FILTER' --limit=10 |
Query log entries with a filter |
gcloud logging read 'FILTER' --freshness=1h |
Limit results to a recent time window |
gcloud logging write LOG_NAME "message" --severity=ERROR |
Write a test entry to a custom log |
gcloud logging sinks create NAME DESTINATION --log-filter='FILTER' |
Route matching entries to a destination |
gcloud logging sinks describe NAME --format="value(writerIdentity)" |
Get the sink's service account for the grant |
gcloud logging sinks list |
List sinks and confirm their filters |