本页面介绍如何使用 Service Infrastructure 启用托管式服务,以向服务提供方项目和服务使用方项目发送日志。
Service Control API 接受结构化或非结构化日志数据,然后将其批量处理并转发到 Cloud Logging。您和您的托管服务用户都可以使用Google Cloud Cloud 控制台和 Google API 控制台查看日志数据,也可以使用 Cloud Logging API 以编程方式访问数据。
Logging 单独可允许托管式服务生成日志,以供作为托管式服务开发者的您使用,而 Service Control API 亦允许您生成供用户使用的日志。这使您的用户可以了解和诊断托管式服务的使用情况。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-08。"],[],[],null,["# Reporting Logs\n\nThis page describes how to use the Service Infrastructure to enable\n[managed services](/service-infrastructure/docs/glossary#managed) to send logs to both\n[service producer](/service-infrastructure/docs/glossary#producer) and\n[service consumer](/service-infrastructure/docs/glossary#consumer) projects.\n\nThe Service Control API accepts structured or unstructured log data,\nwhich it batches and forwards to [Cloud Logging](/logging).\nBoth you and users of your managed services can use\nGoogle Cloud console and Google API Console to view the log data, or use the\nCloud Logging API to access the data programmatically.\n\nWhereas Logging alone allows a managed service to generate logs\nfor use by you as the developer of the managed service, the\nService Control API also lets you generate logs that are intended for\nyour users. This makes it possible for your users to understand and diagnose\ntheir use of your managed service.\n\nConfiguring logs\n----------------\n\nBefore you send logs to the Service Control API, you need to specify\nthree different bits of information in your\n[service configuration](/service-infrastructure/docs/service-management/reference/rpc/google.api#google.api.Service):\n\n- **Log names**: You can have one or more distinct logs. Each log is identified\n by a unique name. Individual logs make it possible for you to isolate\n distinct types of data if it makes the data easier to visualize or use.\n\n- **Monitored resources** :\n A [monitored resource](/logging/docs/api/v2#monitored-resources)\n represents a cloud entity that produces some logging data.\n To define a monitored resource in the service configuration, see\n [monitored_resource.proto](https://github.com/googleapis/googleapis/blob/master/google/api/monitored_resource.proto)\n for the technical specification.\n We strongly recommend you to use the same monitored resource for both\n [monitoring](/service-infrastructure/docs/reporting-metrics) and logging.\n\n- **Log configuration** : Lets you specify the monitored resource associated\n with any log stream and the service producer or service consumer project where\n each log should be sent. To configure the log association, see\n [logging.proto](https://github.com/googleapis/googleapis/blob/master/google/api/logging.proto)\n for the technical specification.\n\nIn the following example of [service configuration](/service-infrastructure/docs/glossary#config),\nthe log `activity_log` is configured to be sent to the service consumer. \n\n # The definition of the monitored resource to be used.\n monitored_resources:\n - type: library.appspot.com/Branch\n description: A library branch\n display_name: Library Branch\n launch_stage: ALPHA\n labels:\n - key: resource_container\n description: The Google Cloud resource container (ie. project id) for\n the branch.\n - key: location\n description: The Google Cloud region the branch is located.\n - key: branch_id\n description: The ID of the branch.\n\n # The log name to be used.\n logs:\n - name: activity_log\n\n # The logging configuration.\n logging:\n consumer_destinations:\n - monitored_resource: library.appspot.com/Branch\n logs:\n - activity_log\n\nAfter you have authored such configuration, you will need to follow\n[Managing Service Configurations](/service-infrastructure/docs/manage-config)\nand\n[Managing Service Rollouts](/service-infrastructure/docs/rollout) to push\nthe service configuration to the Service Management API, which will configure\nthe monitored resource in [Cloud Logging](/logging).\n\nReporting logs\n--------------\n\nAfter the service configuration push finishes, you can call the\nService Control API to report log entries. The following example uses the\n`gcurl` command to demonstrate the call. To learn how to set this up, see\n[Getting Started with the Service Control API](/service-infrastructure/docs/service-control/getting-started). \n\n```\ngcurl -d \"{\n 'operations': [ {\n 'operationId': 'e8bf36ef-f9b5-4274-b4f9-079a3731e6e5',\n 'operationName': 'New book arrived',\n 'consumerId': 'projects/library-consumer',\n 'startTime': '`date +%FT%T%:z`',\n 'endTime': '`date +%FT%T%:z`',\n 'labels': {\n 'location': 'us-east1',\n 'branch_id': 'my-test-library-branch'\n },\n 'logEntries': [ {\n 'severity': 'INFO',\n 'textPayload': 'new book arrived',\n 'name': 'activity_log'\n } ]\n } ]\n}\" https://servicecontrol.googleapis.com/v1/services/endpointsapis.appspot.com:report\n```"]]