Quickstart

This page shows you how to read and write log entries in Stackdriver Logging.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Install and initialize the Cloud SDK.
  5. Install the gcloud beta commands component:
    gcloud components install beta

Write log entries using the Cloud SDK

  1. Use the following gcloud command to write a simple text log entry directly to Stackdriver Logging:

    gcloud beta logging write my-test-log "A simple entry"
    
  2. Use gcloud again to write a structured log entry to Stackdriver Logging:

    gcloud beta logging write --payload-type=json my-test-log '{ "message": "My second entry", "weather": "partly cloudy"}'
    

View logs in the Logs Viewer

  1. Go to the Logs Viewer page in the Google Cloud Platform Console.

    Go to the Logs Viewer page

    This page allows you to view and filter all your logs. You'll see a screen like the following:

    Logs Viewer

    If your project uses Google Cloud Platform services, you might also see log entries from those services.

  2. In the drop-down menu above the log entries, choose Custom Logs to see the log entries you wrote with gcloud.

    If you don't see Custom Logs or if you don't see your log entries, wait a few minutes for your log entries to be received by Stackdriver Logging, and refresh the Stackdriver Logging page.

  3. Expand the two log entries by selecting the ▸ icons to the left of the log entries. This shows the fields inside each log entry:

    Custom log entries

    The first entry, A simple entry, has its text stored in the field textPayload. The second entry, My second entry, is a structured log entry contained in the field jsonPayload. The structured payload contains the fields message and weather. When a message field is present, Stackdriver Logging uses it as the summary of the log entry when the entry is not expanded.

For more information on log entry data formats, see Datatypes common to all logs.

Filter log entries

  1. In the filter box above the logs selector, type simple and press ENTER. You should see only the log entry, A simple entry.

  2. Remove the filter string you added and select the refresh icon (Show newest logs) to the right of the logs selector. Both log entries reappear.

  3. At the right end of the filter box, select the ▾ icon (Convert to advanced filter). After the filter text that already appears in the advanced filter, add the following line

    jsonPayload.weather:partly
    
  4. Select Submit filter. You should see only the log entry, My second entry:

    Advanced filter for log entries

For more information on filters, see Searching and filtering logs.

List log entries using the Logging API

  1. Go to the API reference page for the entries.list API method:

    Go to entries.list API page

    At the bottom of the page is a Try It! section that lets you run the API method.

  2. In the Try It! section, fill in values for projectIds and filter as shown in the following screenshot. You should use your own project ID. Use the same filter value shown in the screenshot, because resource.type=global corresponds to Custom Logs written by gcloud:

    List entries API

  3. Select Authorize and Execute. You should see a response similar to the following.

    Response:
    {
     "entries": [
      {
       "textPayload": "A simple entry",
       "insertId": "2016-03-18|06:33:11.477796-07|10.95.75.135|-1308388662",
       "resource": {
        "type": "global"
       },
       "timestamp": "2016-03-18T13:33:11.456328306Z",
       "logName": "projects/my-gcp-project-id/logs/my-test-log"
      },
      {
       "insertId": "2016-03-18|06:34:05.690112-07|10.95.160.201|-670393401",
       "jsonPayload": {
        "weather": "partly cloudy",
        "message": "My second entry"
       },
       "resource": {
        "type": "global"
       },
       "timestamp": "2016-03-18T13:34:05.676109435Z",
       "logName": "projects/my-gcp-project-id/logs/my-test-log"
      }
     ]
    }
    

List log entries using the Cloud SDK

  1. Run the following gcloud command:

    gcloud beta logging read "resource.type=global"
    

    You should see output similar to the following.

    ---
    insertId: 2016-03-18|06:34:05.690112-07|10.95.160.201|-670393401
    jsonPayload:
      message: My second entry
      weather: partly cloudy
    logName: projects/my-gcp-project-id/logs/my-test-log
    projectNumber: '649517127304'
    resource:
      type: global
    timestamp: '2016-03-18T13:34:05.676109435Z'
    ---
    insertId: 2016-03-18|06:33:11.477796-07|10.95.75.135|-1308388662
    logName: projects/my-gcp-project-id/logs/my-test-log
    projectNumber: '649517127304'
    resource:
      type: global
    textPayload: A simple entry
    timestamp: '2016-03-18T13:33:11.456328306Z'
    

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. (Optional) To delete the log entries you created, run the following gcloud command:

    gcloud beta logging logs delete my-test-log
    

    If you do not delete your log entries, they will expire and be removed in about a month. See Quota Policy. If logs such as my-test-log have no entries, then the Logs Viewer does not display them.

What's next

  • See Logs Viewer for a more detailed discussion of the Logs Viewer.
  • See Exporting logs to learn how to export your log entries to Google Cloud Storage, Google Bigquery, and Google Cloud Pub/Sub.
  • See Logging Agent to learn how to collect log entries from your virtual machine instances in Stackdriver Logging.
  • See Audit Logs for your auditing and compliance needs.
  • See Stackdriver Logging API to learn how to read, write, and configure logs from your applications.

Send feedback about...

Stackdriver Logging