PROJECT_ID は Google Cloud プロジェクト ID、TRACE_ID はトレース ID です。
Cloud Logging API の使用時にログエントリをスパンに関連付けるには、LogEntry オブジェクトの spanId フィールドをスパン ID の 16 文字 16 進数エンコードに設定します。たとえば、ログエントリを ID 74 というスパンに関連付けるには、スパン ID を 000000000000004a に設定します。
トレースのログエントリは、ウォーターフォール グラフの横またはログ エクスプローラに表示できます。ログ エクスプローラを使用した場合は、自動的にトレースのタイムスタンプ範囲に制限されます。表示するログエントリがない場合は、ログ エクスプローラに「No entries found matching current filter」というメッセージが表示されます。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2024-12-21 UTC。"],[],[],null,["# Link log entries with traces\n\nThis document describes how you can link a log entry with a trace. You perform\nthe linkage by setting a field in log entry.\nBy using the trace fields in the [`LogEntry`](/logging/docs/reference/v2/rest/v2/LogEntry)\nstructure---`spanId`, `trace`, and `traceSampled`--- you can\ncorrelate logs and traces to enable better distributed troubleshooting.\n\nManually associate a log entry with a trace or span\n---------------------------------------------------\n\nIf you use the Cloud Logging API to write log data, or if you write\n[structured log data](/logging/docs/structured-logging),\nthen you can associate a log entry with a trace or with a span. The log entry\ncan also record whether the trace was sampled. For information about how the\nOps Agent converts structured log data to\nfields in a `LogEntry`, see [Special fields in structured payloads](/logging/docs/agent/configuration#special-fields).\n\nTo associate a log entry with a trace by setting the `trace` field\nin the [`LogEntry` object](/logging/docs/reference/v2/rest/v2/LogEntry).\nThe `trace` field must be set to a string with the following format: \n\n projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/traces/\u003cvar translate=\"no\"\u003eTRACE_ID\u003c/var\u003e\n\nwhere \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e is your Google Cloud project ID and\n\u003cvar translate=\"no\"\u003eTRACE_ID\u003c/var\u003e is the trace identifier\n\nTo associate a log entry with a span when using the Cloud Logging API,\nset the `spanId` field in the [`LogEntry` object](/logging/docs/reference/v2/rest/v2/LogEntry)\nto the 16-character hexadecimal encoding of the span's ID.\nFor example, to associate a log entry with a span that has an ID of `74`,\nset the span ID to `000000000000004a`.\n\nTo indicate that you are using trace sampling and that the trace was sampled\nfor storage when the log entry was written, set the `traceSampled` field in\nthe [`LogEntry` object](/logging/docs/reference/v2/rest/v2/LogEntry). When you use trace sampling,\nit's possible that a log entry is created when the trace itself isn't captured.\n\nAutomatically associate a log entry with a trace or span\n--------------------------------------------------------\n\nWhen using [Cloud Logging client\nlibraries](/logging/docs/reference/libraries), in some cases the trace fields\nin a log entry can be set automatically. Values set [manually](#associating)\ntake precedence over values set automatically.\n\nFor example, if you are using [OpenTelemetry](https://opentelemetry.io) and are\nlogging from an active OpenTelemetry span, then the trace fields in the log entry\nare populated from the [OpenTelemetry\nContext](https://opentelemetry.io/docs/concepts/context-propagation).\n\nAlternatively, in some cases, when an HTTP request is present,\nthe trace fields can be set from the\n[W3C `traceparent`](https://www.w3.org/TR/trace-context) field or the\n[`X-Cloud-Trace-Context`](/trace/docs/trace-context#legacy-http-header) value in the HTTP request.\n\nFor more information about automatic population of trace fields in log\nentries, see the following language-specific client-library documentation:\n\n- [Go](/go/docs/reference/cloud.google.com/go/logging/latest#hdr-Automatic_Trace_Span_ID_Extraction)\n- [Java](https://github.com/googleapis/java-logging?tab=readme-ov-file#population-of-tracespan-id-fields-in-a-logentry)\n- [NodeJS](/nodejs/docs/reference/logging/latest#automatic-tracespan-id-extraction)\n- [Python](/python/docs/reference/logging/latest/auto-trace-span-extraction)\n\nViewing logs\n------------\n\nYou can view the log entry for a trace alongside the waterfall graph or\nin the Logs Explorer. When you use the Logs Explorer, it's automatically restricted\nto the timestamp range of the trace. If there are no log entries to display,\nthe Logs Explorer displays the message\n`No entries found matching current filter`.\n\nFrom the **Trace details** pane, to view the log entry for the trace,\ndo one of the following:\n\n- To display the trace log entries alongside the waterfall graph, go to\n the waterfall graph and click **Show logs** .\n When **Show logs** isn't displayed, no log entries are available.\n\n- To view the log entry in the Logs Explorer, click **View** next to the\n label **Log** in the **Details** section.\n Note that when you have a Cloud Load Balancing trace, click **View** next\n to the label **VM Log**.\n\nFor more information about viewing log entries in Cloud Logging,\nsee [Using the Logs Explorer](/logging/docs/view/logs-explorer-interface).\n\n### Log viewing permissions\n\nTo view any log entries, you must have the `logging.logEntries.list` permission\nin your project. This permission is provided by the **Logs Viewer** and\n**Project Viewer** Identity and Access Management (IAM) roles.\n\nTo view VM instance logs, you must have the `compute.instances.get` permission\nin your project. This permission is provided by the **Compute Engine Network\nViewer** and **Project Viewer** IAM roles."]]