Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export previous logs in Stackdriver

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?

like image 327
xiu shi Avatar asked Aug 29 '17 17:08

xiu shi


People also ask

How do I export GCP logs?

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.

How long are Stackdriver logs retained?

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.

Where are Stackdriver logs stored?

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.


2 Answers

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.

like image 156
Nathaniel Irons Avatar answered Sep 23 '22 22:09

Nathaniel Irons


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
like image 32
user1162020 Avatar answered Sep 22 '22 22:09

user1162020