Write request logs
Stay organized with collections
Save and categorize content based on your preferences.
An example of how to log HTTP request logs.
Code sample
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.
[[["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"]],[],[],[],null,["# Write request logs\n\nAn example of how to log HTTP request logs.\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Writes an advanced log entry to Cloud Logging.\n package main\n\n import (\n \t\"context\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n\n \t\"cloud.google.com/go/logging\"\n )\n\n func main() {\n \tctx := context.Background()\n\n \t// Creates a client.\n \tprojectID := os.Getenv(\"GOOGLE_CLOUD_PROJECT\")\n \tclient, err := logging.NewClient(ctx, projectID)\n \tif err != nil {\n \t\tlog.Fatalf(\"Failed to create client: %v\", err)\n \t}\n \tdefer client.Close()\n\n \t// Sets the name of the log to write to.\n \tlogger := client.Logger(\"my-log\")\n\n \t// Logs a basic entry.\n \tlogger.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/index.html#cloud_google_com_go_logging_Logger_Log(logging.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/index.html#cloud_google_com_go_logging_Entry{Payload: \"hello world\"})\n\n \t// TODO(developer): replace with your request value.\n \tr, err := http.NewRequest(\"GET\", \"http://example.com\", nil)\n\n \t// Logs an HTTPRequest type entry.\n \t// Some request metadata will be autopopulated in the log entry.\n \thttpEntry := logging.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/index.html#cloud_google_com_go_logging_Entry{\n \t\tPayload: \"optional message\",\n \t\tHTTPRequest: &logging.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/index.html#cloud_google_com_go_logging_HTTPRequest{\n \t\t\tRequest: r,\n \t\t},\n \t}\n \tlogger.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/index.html#cloud_google_com_go_logging_Logger_Log(httpEntry)\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.MonitoredResource.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.HttpRequest.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntry.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.Payload.html;\n import com.google.cloud.logging.https://cloud.google.com/java/docs/reference/google-cloud-apihub/latest/com.google.cloud.apihub.v1.Severity.html;\n import java.util.Collections;\n\n /** Write LogEntry with HTTP request using the Cloud Logging API. */\n public class LogEntryWriteHttpRequest {\n\n public static void main(https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.common.Type.String.html[] args) throws Exception {\n // TODO(developer): Replace these variables before running the sample.\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.common.Type.String.html logName = \"log-name\"; // i.e \"my-log\"\n https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.common.Type.String.html payLoad = \"payload\"; // i.e \"Hello world!\"\n https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.HttpRequest.html httpRequest =\n https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.HttpRequest.html.newBuilder()\n .setRequestUrl(\"www.example.com\")\n .setRequestMethod(https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.HttpRequest.html.RequestMethod.GET) // Supported method GET,POST,PUT,HEAD\n .setStatus(200)\n .build();\n createLogEntryRequest(logName, payLoad, httpRequest);\n }\n\n public static void createLogEntryRequest(https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.common.Type.String.html logName, https://cloud.google.com/java/docs/reference/google-cloud-bigtable/latest/com.google.cloud.bigtable.common.Type.String.html payLoad, https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.HttpRequest.html httpRequest)\n throws Exception {\n // Instantiates a logging client\n try (https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.Logging.html logging = https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LoggingOptions.html.getDefaultInstance().getService()) {\n // create an instance of LogEntry with HTTP request\n https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntry.html logEntry =\n https://cloud.google.com/java/docs/reference/google-cloud-logging/latest/com.google.cloud.logging.LogEntry.html.newBuilder(https://cloud.google.com/java/docs/reference/google-cloud-apigee-connect/latest/com.google.cloud.apigeeconnect.v1.Payload.html.StringPayload.of(payLoad))\n .setSeverity(https://cloud.google.com/java/docs/reference/google-cloud-apihub/latest/com.google.cloud.apihub.v1.Severity.html.ERROR)\n .setLogName(logName)\n .setHttpRequest(httpRequest)\n .setResource(https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.MonitoredResource.html.newBuilder(\"global\").build())\n .build();\n\n // Writes the log entry asynchronously\n logging.write(Collections.singleton(logEntry));\n System.out.printf(\"Logged: %s\", payLoad);\n }\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /*\n const projectId = 'YOUR_PROJECT_ID'; // Your Google Cloud Platform project ID\n const logName = 'my-log'; // The name of the log to write to\n const requestMethod = 'GET'; // GET, POST, PUT, etc.\n const requestUrl = 'http://www.google.com';\n const status = 200;\n const userAgent = `my-user-agent/1.0.0`;\n const latencySeconds = 3;\n const responseSize = 256; // response size in bytes.\n */\n\n // Imports the Google Cloud client library\n const {Logging} = require('https://cloud.google.com/nodejs/docs/reference/logging/latest/overview.html');\n\n // Creates a client\n const logging = new https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html({projectId});\n\n // Selects the log to write to\n const log = logging.log(logName);\n\n // The data to write to the log\n const text = 'Hello, world!';\n\n // The metadata associated with the entry\n const metadata = {\n resource: {type: 'global'},\n httpRequest: {\n requestMethod,\n requestUrl,\n status,\n userAgent,\n latency: {\n seconds: latencySeconds,\n },\n responseSize,\n },\n };\n\n // Prepares a log entry\n const entry = log.entry(metadata, text);\n\n // Writes the log entry\n async function writeLog() {\n await log.write(entry);\n console.log(`Logged: ${text}`);\n }\n writeLog();\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=logging)."]]