A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://cloud.google.com/error-reporting/docs/formatting-error-messages below:

Format errors in logs | Error Reporting

Skip to main content Format errors in logs

Stay organized with collections Save and categorize content based on your preferences.

This document explains how to format the log entry when you want to use Cloud Logging to report error events.

You can report error events to your Google Cloud project by executing the Cloud Logging API method write or the Error Reporting API method report. When you report error events by using the Cloud Logging API, then request body contains a LogEntry object that must include a stack trace, or a ReportedErrorEvent object.

Before you begin LogEntry format requirements

This section describes how to format a LogEntry so that Error Reporting captures the error event that is contained in the log entry.

Log a stack trace

To log an error event that is a stack trace, write the error event as one of these types:

Log a text message

To log an error event that is a text message, use the following format for the jsonPayload:

    "jsonPayload": {
      "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
      "message": "Text message"
    },

When you set the @type field to the specified value, Error Reporting always evaluates the log entry as though all required fields are present. As a result, Error Reporting captures the error event.

If you set the @type field to a different value or leave it unset, then Cloud Logging searches for field labeled serviceContext to determine if the payload is a ReportedErrorEvent object.

You don't need to set the @type field when the message, stack_trace, or exception fields of the jsonPayload contain a stack trace. In those cases, Error Reporting automatically captures the error event.

Supported monitored resources

Set the resource field of the LogEntry object to one of the following supported monitored resource types:

1 textPayload not supported

Examples

This section shows how you can ensure that Error Reporting processes a log entry when that log entry contains a text message or a stack trace.

Log an error event that is a text message

The following example shows how to format a LogEntry object when you want to log an error event that is a text message, use the following JSON structure for the jsonPayload field of the LogEntry:

{...
  {
    "jsonPayload": {
      "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
      "message": "A simple text message"
    },
    "logName": "projects/test-project/logs/reported-error",
    "resource": {
      "labels": {
        "project_id": "test-project"
      },
      "type": "global"
    },
    "severity": "ERROR",
    "timestamp": "2019-06-27T13:43:26.375834551Z"
  }
}

As the example shows, you must set the @type field to the value that forces Error Reporting to group the log entry: For more information, see Log a text message.

When the message field contains a stack trace, the log entry is automatically grouped so you don't need to specify the @type field.

Log an error that is formatted as a ReportedErrorEvent object

When your error event is stored in a ReportedErrorEvent object, use the following JSON structure for the jsonPayload field of the LogEntry:

{
  "eventTime": string,
  "serviceContext": {
    "service": string,     // Required.
    "version": string
  },
  "message": string,       // Required. This field contains the main error content to report.
  "@type": string          // Optional. For information about this field, see Log a text message.
  "context": {
    "httpRequest": {
      "method": string,
      "url": string,
      "userAgent": string,
      "referrer": string,
      "responseStatusCode": number,
      "remoteIp": string
    },
    "user": string,
    "reportLocation": {    // Required if no stack trace is provided.
      "filePath": string,
      "lineNumber": number,
      "functionName": string
    }
  }
}

Ensure that you populate the message field with the error information. To learn how to store a stack trace in the message field of a ReportedErrorEvent object, see the reference page for the report method.

The following example illustrates how to set the jsonPayload field of the LogEntry to be formatted as an ReportedErrorEvent object. Because the message field contains a stack trace, the error event is grouped by Error Reporting:

{...
   "jsonPayload": {
      "serviceContext": {
        "service": "frontend",
        "version": "bf6b5b09b9d3da92c7bf964ab1664fe751104517"
      },
      "message": "com.example.shop.Template$CartDiv retrieveCart: Error\njava.lang.IndexOutOfBoundsException: Index: 4, Size: 4\n\tat java.util.ArrayList.rangeCheck(ArrayList.java:635)\n\tat java.util.ArrayList.get(ArrayList.java:411)\n\tat com.example.shop.Cart.retrieve(Cart.java:76)\n\tat com.example.shop.Cart.generate(Cart.java:55)\n\tat com.example.shop.Template$CartDiv.retrieveCart(Template.java:113)\n\tat com.example.shop.Template.generate(Template.java:22)\n\tat com.example.shop.CartServlet.doGet(CartServlet.java:115)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:717)\n",
      "context":
        "httpRequest": {
          "method": "GET",
          "url": "http://example.com/shop/cart",
          "responseStatusCode": 500
        },
        "user": "9f32f587135aa6774e78ed30fbaabcce3ec5528f"
      }
   },
   "logName": "projects/test-project/logs/reported-error",
   "resource": {
      "labels": {
        "project_id": "test-project"
      },
      "type": "global"
   },
   "severity": "ERROR",
   "timestamp": "2019-06-27T13:43:26.375834551Z"
}
Log an error event by using the textPayload field

You can record an error event by using the textPayload field of a LogEntry to store the error data. For example, the following Google Cloud CLI command results in a log entry whose severity level is ERROR and whose textPayload field contains an error event:

gcloud logging write test-log --severity=ERROR --payload-type=text 'RuntimeException: Oops! Something bad happened.
at com.example.MyClass.method(MyClass.java:123)
at com.example.OtherClass.doStuff(Unknown Source)
at com.example.Sys.create(Native Method)'

The result of the previous command is a log entry that is grouped by Error Reporting:

{...
    logName: "projects/PROJECT_ID/logs/test-log"
    severity: "ERROR"
    textPayload: "RuntimeException: Oops! Something bad happened.
                  at com.example.MyClass.method(MyClass.java:123)
                  at com.example.OtherClass.doStuff(Unknown Source)
                  at com.example.Sys.create(Native Method)"
    ...
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-11 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-11 UTC."],[[["Cloud Logging API's `write` method and Error Reporting API's `report` method can be used to report error events to your Google Cloud project, with the `LogEntry` object needing to contain a stack trace or a `ReportedErrorEvent` object when using the Cloud Logging API."],["To log error events with stack traces via `LogEntry`, utilize multi-line `textPayload` or `jsonPayload` fields containing `message`, `stack_trace`, or `exception`, where `stack_trace` takes precedence in evaluation."],["To log error events as text messages, utilize the `jsonPayload` field with `\"@type\": \"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent\"`, which ensures Error Reporting captures the event."],["The `resource` field in the `LogEntry` must be set to one of the supported monitored resource types for Error Reporting, such as `app_script_function`, `gce_instance`, or `global`, among others."],["If using the Error Reporting API's `report` method, properly formatted log entries are automatically generated and sent to Cloud Logging, but may incur ingestion costs that can be controlled through Exclusion filters."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4