| name | gcp-logs |
| description | Query GCP Cloud Logging for errors, service logs, and request traces. Use when investigating GCP-hosted services. |
Query GCP Logs
Search GCP Cloud Logging for application errors, service logs, and traces.
When to Use
- Investigating errors in GCP-hosted services
- Finding request traces for debugging
- Analyzing service behavior over time
- Correlating events across services
Pre-flight Checks
Authentication and Context
gcloud auth list 2>/dev/null | grep -q ACTIVE || {
echo "Not authenticated. Run: gcloud auth login"
exit 1
}
CURRENT_PROJECT=$(gcloud config get-value project 2>/dev/null)
echo "Current GCP Project: $CURRENT_PROJECT"
Query Strategy
Follow this approach for effective log queries:
- Start simple: Begin with basic filters (severity, service, timestamp)
- Verify data exists: Check if logs are available in your time range
- Narrow time windows: Use 1-2 hour windows for better performance
- Use two-stage filtering: For complex logic, query broad → filter with jq
- Save to files: Store large result sets for analysis
- Iterate: Start broad, progressively add filters
Example Workflow
gcloud logging read 'timestamp>="2025-12-24T10:00:00Z"' --limit=10
gcloud logging read 'resource.labels.container_name="api-gateway"' --limit=50
gcloud logging read 'resource.labels.container_name="api-gateway" AND severity>=ERROR' --limit=100
gcloud logging read \
'resource.labels.container_name="api-gateway" AND severity>=ERROR AND timestamp>="2025-12-24T10:00:00Z" AND timestamp<="2025-12-24T11:00:00Z"' \
--limit=100 \
--format=json
Output Management
For large outputs (>100 entries), save to tmp file:
gcloud logging read 'severity>=ERROR' \
--limit=1000 \
--format=json > /tmp/gcp-errors-$(date +%Y%m%d-%H%M%S).json
cat /tmp/gcp-errors-*.json | jq '.[] | select(.severity=="CRITICAL")'
cat /tmp/gcp-errors-*.json | jq '.[] | .jsonPayload.message' | sort | uniq -c
Benefits:
- Preserve output for multiple operations
- Easier to analyze large result sets
- Share logs with team or attach to tickets
Common Patterns
For detailed query examples, see:
Quick Reference
gcloud logging read 'resource.labels.container_name="service-name" AND severity>=ERROR' --limit=100
gcloud logging read 'timestamp>="2025-12-24T10:00:00Z" AND timestamp<="2025-12-24T11:00:00Z"' --limit=200
gcloud logging read 'resource.labels.container_name="api" AND severity>=ERROR AND timestamp>="2025-12-24T10:00:00Z"' --format=json
Output Format
--format=json - Machine-readable JSON (recommended for piping to jq)
--format=table - Human-readable table
- Default: Log entries with timestamps
Tips
- Always single-quote filter strings to prevent shell interpretation
- Use
--limit to control result count (default: 10)
- Use
--project to override current project
- Combine filters with
AND, OR, NOT
- Use ISO 8601 timestamps with timezone (e.g., "2025-12-24T10:00:00Z")
- For deep nesting or complex conditions, use two-stage filtering with jq
- Start with narrow time windows (1-2 hours) for better performance