This chapter follows Protecting services with Cloud Armor. Cloud Logging centralizes logs from Google Cloud services and applications. Cloud Run automatically collects stdout and stderr from your container; the load balancer emits access logs when enabled.
Structured logs from Cloud Run
Logging agents parse JSON written to stdout when you include a severity field:
export function logInfo(message: string, extra: Record<string, unknown> = {}) { console.log(JSON.stringify({ severity: "INFO", message, ...extra }));}
export function logError(message: string, err: unknown) { console.error( JSON.stringify({ severity: "ERROR", message, error: err instanceof Error ? err.message : String(err), }), );}Use DEBUG, INFO, WARNING, ERROR, CRITICAL so Logs Explorer filters by severity.
When you propagate X-Cloud-Trace-Context, add logging.googleapis.com/trace in JSON to link logs to Cloud Trace (optional for the series).
Logs Explorer and the query language
Open Logging → Logs Explorer. Scope to your project and filter Cloud Run:
resource.type="cloud_run_revision"resource.labels.service_name="file-api"severity>=ERRORSearch message text:
jsonPayload.message:"upload"Save useful queries as log-based metrics or pin them for on-call.
Buckets, retention, and views
Logs land in _Default and _Required buckets with default retention. Log buckets can be created with custom retention (for example 400 days for compliance).
Log views restrict which entries IAM principals can read inside a bucket—useful for separating security logs from application logs.
Sinks and exclusions
Log sinks export matching entries to BigQuery, Cloud Storage, or Pub/Sub for analysis or archival:
gcloud logging sinks create file-api-errors bigquery.googleapis.com/projects/YOUR_PROJECT_ID/datasets/logs \ --log-filter='resource.type="cloud_run_revision" AND severity>=ERROR'The sink’s writer identity needs permission to write the destination.
Exclusions drop noisy debug logs from storage to save cost—apply carefully so you do not hide incidents.
Audit logs
Admin Activity audit logs record configuration changes (IAM, firewall, Run deploy). Data Access logs record reads/writes to user data when enabled—higher volume and cost.
Review audit logs when investigating who changed Cloud Armor policies or IAM bindings.
Next: Cloud Monitoring turns metrics and SLOs into dashboards and alerts for the same service.
Check your understanding
Question 1 of 5
0 of 5 checked