I have a log in Stackdriver that logs every request goes into my api and failed, and I want to write a script to count on the number of times each error message appears. The problem is, the export feature in Stackdriver V2 only allow me to sink upcoming error messages, but I only care about the logs entries that already lives in the log. Is there a way to download the complete log from Stackdriver?
You'll obtain the HTTPS URL for the source. Create a topic in Google Pub/Sub and subscribe the GCP source URL to that topic. Create an export of GCP logs from Google Stackdriver Logging. Exporting involves writing a filter that selects the log entries you want to export, and choosing a Pub/Sub as the destination.
Stackdriver Logging does not retain logs forever. All logs are subject to a standard log retention period, after which they are deleted from Stackdriver. The retention period varies based on the type of log, with admin activity audit logs being retained for 400 days, and all other logs being retained for 30 days.
All logs generated in the project are stored in the _Required and _Default logs buckets, which live in the project that the logs are generated in: _Required: This bucket holds Admin Activity audit logs, System Event audit logs, and Access Transparency logs, and retains them for 400 days.
You can now do this from the gcloud CLI tool, with gcloud logging read
: https://cloud.google.com/logging/docs/reference/tools/gcloud-logging#reading_log_entries
Though in the scenario described, creating a log sink is the only way to capture events that are older than the stackdriver logging window, which is only 30 days even in the paid version.
Note that the filter is same as when you choose advanced filter in the Logging gui.
gcloud beta logging read "resource.type=\"gae_app\" resource.labels.module_id=\"api\" \"Ronald Reagan\"" --format=json --freshness=6d > x.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With