Logs and Errors
Where Cloud Run logs go, how to find the error that matters in seconds, what a good log line contains, and what must never be logged.
Where the logs are
Everything the server writes to standard output and standard error on Cloud Run goes to Cloud Logging, tagged with the service, the revision and the time. Cloud Run also logs every request: address, status code, latency. Builds log there too. You do not install anything to get this.
Finding errors fast
The console's Logs Explorer is good for browsing. For a quick answer, the command line is faster:
gcloud logging read 'resource.type="cloud_run_revision" AND resource.labels.service_name="iamravi-site" AND severity>=ERROR' \
--project=PROJECT_ID --freshness=1d --limit=20
Useful variations:
| To find | Add to the filter |
|---|---|
| Server errors seen by visitors | httpRequest.status>=500 |
| Slow requests | httpRequest.latency>"2s" |
| One deploy's logs | resource.labels.revision_name="REVISION" |
| A word in a message | textPayload:"payment" |
Read logs backwards
When something breaks, start at the first error after the last good moment, not the last one on the screen. Later errors are often consequences of the first. A missing setting at start-up becomes a hundred failed requests a minute later. Fix the first, and the rest usually vanish.
What a good log line contains
- What happened, in words:
could not fetch order - asking the provider to retry. - Which thing: an order ID, a page path, a section, never personal data.
- What happens next: retrying, ignored, refused.
- A short prefix for the area, such as
[pay]or[ravi-os], so one area's messages can be filtered.
This site's payment code, for example, logs [pay] webhook: amount mismatch for <payment> ... price <amount> when it refuses a payment. Someone reading that line at midnight knows what happened and that nothing was granted.
What must never be logged
- secrets, keys, tokens, passwords, session cookies;
- full request headers or bodies from sign-in and payments;
- personal details beyond what is needed (an email address in a log is personal data that now has to be protected too).
Log the name of what is missing, never its value.
Errors visitors should not see
A visitor should get a calm error page, not a stack trace. A dependency failing should cost one feature, not the page: if analytics or the database are unreachable, the dashboard says so in a sentence and the rest still loads. Write the detailed reason to the log, where you can read it, and show the visitor only what helps them.
Keep logs useful
- Do not log on every successful request from your own code; Cloud Run already does that.
- Keep noisy debugging lines out of production, or behind a setting.
- Tests that feed in bad input on purpose should not fill the test output with stack traces, or real errors hide among them.
Try it yourself
Run the error query above for your own service over the last week. Group what you find by message. The most frequent error you did not know about is your next fix.