This page includes some code samples for writing and managing logs in Cloud Logging. There are more code samples with the Client Libraries.
Listing logs entries
Here is some sample code to get a list of the log entries for a given logger.
Because there can be many logs, the results are paginated.
The log names returned are in resource format; they are URL-encoded
and the log names are prefixed by /projects/PROJECT_ID/logs/
:
C#
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Go
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Java
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Node.js
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
PHP
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Python
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Ruby
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
There are also other ways to view your log entries in Cloud Logging:
- To read log entries using the Google Cloud Console, see Using the Logs Explorer.
- To read log entries through the Logging API, see the
entries.list
method. - To read log entries using the
gcloud
command-line tool, see Reading log entries.
Writing log entries
Here is some sample code to write a single log entry to mylog
. The service,
region, labels, and other content will change depending on the entry and the
application doing the writing.
If you write many log entries, it is better to batch multiple log entries into a
single call to entries.write
. This improves throughput and helps you avoid
the quota limit on calls to entries.write
.
C#
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Go
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Java
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Node.js
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
PHP
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Python
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Ruby
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Creating logs
You create a new log by writing entries to the new log. There is no separate "create" operation.
Deleting logs
Here is some sample code to delete a log by deleting all its entries. A log with no entries does not appear in the list of project logs. Entries can be explicitly deleted, or they can expire according to the Logging retention policy. If you write new entries to the log, it will reappear.
C#
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Go
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Java
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Node.js
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
PHP
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Python
To learn how to install and use the client library for Logging, see the Logging Client Libraries.
Ruby
To learn how to install and use the client library for Logging, see the Logging Client Libraries.