Andrea Campolonghi
Andrea Campolonghi

Reputation: 580

Read Gcloud logs programmatically

I am trying to understand what is the equivalent REST api for the cli command:

gcloud logging read ....

I need to execute a query on logs programmatically from a client library (Ruby if possible). I cannot find nothing in the rest api docs. Any suggestion?

Upvotes: 0

Views: 1989

Answers (1)

DazWilkin
DazWilkin

Reputation: 40326

Google's documentation for using client libraries with its services, is mostly very good.

Here's the documentation for Ruby for Cloud Logging.

https://cloud.google.com/logging/docs/reference/libraries#client-libraries-install-ruby

I encourage you to use Google's excellent APIs Explorer when developing using the client libraries too. APIs Explorer helps you craft REST requests and see the responses and this is very helpful when writing code and debugging:

Here's the APIs Explorer page for the Logging service's method to list log entries:

https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/list

Another very helpful facility is that you can append --log-http to any gcloud command it will show you the underlying REST calls that are being made. This is also helpful in determining how the gcloud commands work so that you may write your own code that replicates|extends the functionality:

gcloud logging read ${FILTER} \
--project=${PROJECT} \
--log-http

And, finally, the Console's Log Explorer is an excellent way to prototype the filters you'll probably need to use in your client code:

https://console.cloud.google.com/logs/query

Hint: when constructing filters, don't forget to escape " characters, for example FILTER="resource.type=\"gce_instance\"". In this case, the value gce_instance must be quoted (") in the filter's value.

Upvotes: 1

Related Questions