在 Cloud Functions 中编写结构化日志记录
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
此示例演示了如何使用 Google Cloud 日志记录客户端在 Cloud Functions 中写入结构化日志。结构化日志可让您更详细、更有条理地记录应用相关信息,从而更轻松地排查和分析问题。
代码示例
Node.js
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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"]],[],[[["\u003cp\u003eThis sample demonstrates how to write structured logs in Cloud Functions using the Google Cloud logging client in both Node.js and Python.\u003c/p\u003e\n"],["\u003cp\u003eStructured logs offer a detailed and organized method for recording application information, aiding in issue troubleshooting and analysis.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples showcase initializing the logging client, creating log entries with metadata like severity and components, and writing them to the log.\u003c/p\u003e\n"],["\u003cp\u003eIn both samples, setting up Application Default Credentials is required for authenticating to Cloud Run functions.\u003c/p\u003e\n"],["\u003cp\u003eThe HTTP request data is attached automatically for request-log correlation.\u003c/p\u003e\n"]]],[],null,["# Write structured Logging in Cloud Functions\n\nThis sample demonstrates how to write structured logs in Cloud Functions using the Google Cloud logging client. Structured logs provide a more detailed and organized way to record information about your application, making it easier to troubleshoot and analyze issues.\n\nCode sample\n-----------\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, 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 const {Logging} = require('https://cloud.google.com/nodejs/docs/reference/logging/latest/overview.html');\n const functions = require('@google-cloud/functions-framework');\n const pkg = require('./package.json');\n\n functions.http('structuredLogging', async (req, res) =\u003e {\n // Initialize the logging client\n const logging = new https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html();\n // Required to capture your project id\n await logging.https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html();\n // Create a LogSync transport, defaulting to process.stdout\n const log = logging.https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html(pkg.name);\n const text = 'Hello, world!';\n // Create a structured log entry with severity,\n // additional component fields, and HTTP request.\n // Appending the httpRequest enables log correlation.\n const metadata = {\n severity: 'https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/protos.google.logging.type.logseverity.html',\n component: 'arbitrary-property',\n httpRequest: req,\n };\n // Prepares a log entry\n const entry = log.entry(metadata, text);\n log.write(entry);\n res.status(200).send('Success: A log message was written');\n });\n\n### Python\n\n\nTo authenticate to Cloud Run functions, 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 logging\n\n import functions_framework\n from google.cloud.logging import https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html\n\n\n @functions_framework.http\n def structured_logging(request):\n # Initialize the Google Cloud logging client\n cloud_logging_client = Client()\n # Set up a Log Handler that exports logs in JSON format to stdout\n # when running in a serverless environment.\n # To manually set up a Structured Log Handler, see\n # https://googleapis.dev/python/logging/latest/handlers-structured-log.html\n cloud_logging_client.https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html#google_cloud_logging_v2_client_Client_setup_logging()\n\n # Construct log message and additional metadata\n # https://cloud.google.com/run/docs/logging#using-json\n msg = \"Hello, world!\"\n metadata = {\"component\": \"arbitrary-property\"}\n\n # Write structured log with additional component fields\n # HTTP request data is attached automatically for request-log correlation\n logging.info(msg, extra={\"json_fields\": metadata})\n\n return \"Success: A log message was written\"\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=functions)."]]