Write request logs

An example of how to log HTTP request logs.

Code sample

Go

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


// Writes an advanced log entry to Cloud Logging.
packagemain
import(
"context"
"log"
"net/http"
"os"
"cloud.google.com/go/logging"
)
funcmain(){
ctx:=context.Background()
// Creates a client.
projectID:=os.Getenv("GOOGLE_CLOUD_PROJECT")
client,err:=logging.NewClient(ctx,projectID)
iferr!=nil{
log.Fatalf("Failed to create client: %v",err)
}
deferclient.Close()
// Sets the name of the log to write to.
logger:=client.Logger("my-log")
// Logs a basic entry.
logger.Log (logging.Entry {Payload:"hello world"})
// TODO(developer): replace with your request value.
r,err:=http.NewRequest("GET","http://example.com",nil)
// Logs an HTTPRequest type entry.
// Some request metadata will be autopopulated in the log entry.
httpEntry:=logging.Entry {
Payload:"optional message",
HTTPRequest:&logging.HTTPRequest {
Request:r,
},
}
logger.Log (httpEntry)
}

Java

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

importcom.google.cloud.MonitoredResource ;
importcom.google.cloud.logging.HttpRequest ;
importcom.google.cloud.logging.LogEntry ;
importcom.google.cloud.logging.Logging ;
importcom.google.cloud.logging.LoggingOptions ;
importcom.google.cloud.logging.Payload ;
importcom.google.cloud.logging.Severity ;
importjava.util.Collections;
/** Write LogEntry with HTTP request using the Cloud Logging API. */
publicclass LogEntryWriteHttpRequest{
publicstaticvoidmain(String []args)throwsException{
// TODO(developer): Replace these variables before running the sample.
String logName="log-name";// i.e "my-log"
String payLoad="payload";// i.e "Hello world!"
HttpRequest httpRequest=
HttpRequest .newBuilder()
.setRequestUrl("www.example.com")
.setRequestMethod(HttpRequest .RequestMethod.GET)// Supported method GET,POST,PUT,HEAD
.setStatus(200)
.build();
createLogEntryRequest(logName,payLoad,httpRequest);
}
publicstaticvoidcreateLogEntryRequest(String logName,String payLoad,HttpRequest httpRequest)
throwsException{
// Instantiates a logging client
try(Logging logging=LoggingOptions .getDefaultInstance().getService()){
// create an instance of LogEntry with HTTP request
LogEntry logEntry=
LogEntry .newBuilder(Payload .StringPayload.of(payLoad))
.setSeverity(Severity .ERROR)
.setLogName(logName)
.setHttpRequest(httpRequest)
.setResource(MonitoredResource .newBuilder("global").build())
.build();
// Writes the log entry asynchronously
logging.write(Collections.singleton(logEntry));
System.out.printf("Logged: %s",payLoad);
}
}
}

Node.js

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/*
const projectId = 'YOUR_PROJECT_ID'; // Your Google Cloud Platform project ID
const logName = 'my-log'; // The name of the log to write to
const requestMethod = 'GET'; // GET, POST, PUT, etc.
const requestUrl = 'http://www.google.com';
const status = 200;
const userAgent = `my-user-agent/1.0.0`;
const latencySeconds = 3;
const responseSize = 256; // response size in bytes.
*/
// Imports the Google Cloud client library
const{Logging}=require('@google-cloud/logging');
// Creates a client
constlogging=newLogging ({projectId});
// Selects the log to write to
constlog=logging.log(logName);
// The data to write to the log
consttext='Hello, world!';
// The metadata associated with the entry
constmetadata={
resource:{type:'global'},
httpRequest:{
requestMethod,
requestUrl,
status,
userAgent,
latency:{
seconds:latencySeconds,
},
responseSize,
},
};
// Prepares a log entry
constentry=log.entry(metadata,text);
// Writes the log entry
asyncfunctionwriteLog(){
awaitlog.write(entry);
console.log(`Logged: ${text}`);
}
writeLog();

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

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.