Gravar registros de solicitações
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Um exemplo de como registrar registros de solicitação HTTP.
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],[],[],[],null,["An example of how to log HTTP request logs.\n\nCode sample \n\nGo\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\nJava\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\nNode.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\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=logging)."]]