NodeJS Google Cloud Run -自定义Cloud Logging属性

41zrol4v  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(87)

是否有方法为Cloud Run服务自动生成的日志添加自定义属性?

const app = express();
app.use((req: any, res, next) => {
  const myCustomHeader = req.get('x-my-custom-header');
  // TODO: Add my custom header to Cloud Run automatic logs
  next();
});

我想将myCustomHeader添加到Cloud Logging输出(例如:httpRequest对象:

{
 httpRequest: {
   myCustomHeader: "my custom value here",
   … // Default attributes set by Cloud Run
 },
 insertId:  "5c82b3d1000ece0000000000",
 labels: {
  instanceId:  "someid",
  mylabel: "mylabelvalue",
  mysecondlabel: "mysecondlabelvalue",
 },
 logName:  "projects/my-project/logs/run.googleapis.com%2Frequests",
 receiveTimestamp:  "2019-03-08T18:26:25.981686167Z",
 resource: {
  labels: {
   configuration_name:  "myservice",
   location:  "us-central1",
   project_id:  "my-project",
   revision_name:  "myservice-00002",
   service_name:  "myservice",
  }
  type:  "cloud_run_revision",
 },
 severity:  "INFO",
 timestamp:  "2019-03-08T18:26:25.970397Z",
}
vcirk6k6

vcirk6k61#

根据本官方文件
当您从服务或作业写入日志时,只要日志被写入以下任何位置,Cloud Logging就会自动拾取日志:

  • 标准输出(stdout)或标准错误(stderr)流
  • /var/log目录下的任何文件
  • 系统日志(/dev/log)
  • 使用Cloud Logging客户端库编写的日志,这些库可用于许多流行语言

大多数开发人员都希望使用标准输出和标准错误来编写日志。
因此,如果您想向日志添加一些自定义属性,请按照生成日志的文档中给出的步骤进行操作。

相关问题