How to export previous logs in Stackdriver
Asked Answered
N

4

16

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?

Nipha answered 29/8, 2017 at 17:7 Comment(0)
N
14

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.

Nickelic answered 25/1, 2018 at 17:34 Comment(1)
It's renamed to gcloud logging read in the new version now :)Demobilize
D
6

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
Distended answered 30/1, 2020 at 8:20 Comment(0)
S
1

If you want to pull existing log data, you'll need to use the API to list entries: https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/list

You can do this directly from the API documentation page if you only need a one time solution.

Supermarket answered 15/9, 2017 at 13:51 Comment(0)
G
0

I found the best way to do it it's with the gcloud cli Example you can you use : gcloud beta logging read ‘<filter>’ --project ”<project name>” --format json --freshness=<number of days to go back>

Goren answered 5/1, 2021 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.