寫入要求記錄檔
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如何記錄 HTTP 要求記錄的範例。
程式碼範例
Go
如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章。
如要向 Logging 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章。
如要向 Logging 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章。
如要向 Logging 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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)."]]