【问题标题】:Custom labels coming in text payload文本有效负载中的自定义标签
【发布时间】:2020-04-11 22:40:30
【问题描述】:

我正在尝试使用 google-cloud/logging-winston 云功能。

我想使用 prefixlabels,但是当我根据 google-cloud/logging-winston 文档配置它们时,没有任何效果。

如图所示。标签被添加到 textpayload 并且根本不使用前缀。

知道什么是错的以及如何解决这个问题......

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {

  const winston = require('winston');

  // Imports the Google Cloud client library for Winston
  const {LoggingWinston} = require('@google-cloud/logging-winston');

  const loggingWinston = new LoggingWinston({ 
    serviceContext: {
      service: 'winston-test',
      version: '1'
    },
    prefix: 'DataInflow' 
  });

  // Create a Winston logger that streams to Stackdriver Logging
  // Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
  const logger = winston.createLogger({
    level: 'info',
    transports: [
      new winston.transports.Console(),
      // Add Stackdriver Logging
      loggingWinston,
    ],
  });

  // Writes some log entries
  //logger.debug('Testing labels', { labels: { module: 'Test Winston Logging' }});
  logger.error('Testing labels', { custom_metadata: 'yes', labels: { module: 'Test Winston Logging' }});

  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Edit#1:如果我指定 keyFilename=path-to-sa-json-file 那么它可以工作。

  const loggingWinston = new LoggingWinston({ 
    serviceContext: {
      service: 'winston-test',
      version: '1'
    },
    prefix: 'DataInflow',
    keyFilename=path-to-sa-json-file.json
  });

但这很奇怪。 Cloud Function 和 logging-winston 库应使用应用程序默认凭据 (ADC)。

因此,如果我与 CF 一起使用的服务帐户具有堆栈驱动程序写入权限,那么应该没问题。

我的理解错了吗?任何有其他见解的人...

Edit#2: 似乎是 Nodejs 10 上的 Cloud Functions 的一个问题。它在 Nodejs 8 上运行良好,无需指定 keyFilename...很奇怪...

【问题讨论】:

  • 可能是因为 Nodejs10 运行时,因为它在 beta。但首先我想知道你是如何使用“前缀”参数的?我问是因为在LoggingWinston 库中没有这样的参数。
  • 查看这个官方文档,Finding credentials automatically。您是否遵循了这些步骤?如果是,请告诉我。
  • @nibrass-haider,我想我找到了问题所在。我需要在标签 winston-logs 下而不是在默认标签下找到日志...此外,如果您抛出错误,它们也应该显示在错误报告中...
  • 你解决问题了吗?如果是,您可以为其他社区成员发布答案。如果您能解决问题,请告诉我。

标签: google-cloud-platform winston google-cloud-logging


【解决方案1】:

最后我设法解决了这个问题。所有winston日志都存储在自定义标签下的stackdriver中,所以这就是我的问题。

使用下面的代码,我能够将错误记录到 stackdriver 以及错误报告。

在错误报告中,我可以在service -> winston-test 和stackdriver 下的All logs -> winston_log 下找到我的错误

 /**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {

  const winston = require('winston');

  // Imports the Google Cloud client library for Winston
  const {LoggingWinston} = require('@google-cloud/logging-winston');

  const metadata = {
    resource: {
      type: 'global',
      serviceContext: {
        service: 'winston-test',
        version: '1'
      },
      prefix: 'DataInflow' 
    }
  };


  const resource = {
    // This example targets the "global" resource for simplicity
    type: 'global',
  };


  const loggingWinston = new LoggingWinston({ resource: resource });

  // Create a Winston logger that streams to Stackdriver Logging
  // Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
  const logger = winston.createLogger({
    level: 'info',
    transports: [
      new winston.transports.Console(),
      // Add Stackdriver Logging
      loggingWinston,
    ],
  });

  // Writes some log entries
  //logger.debug('Testing labels', { labels: { module: 'Test Winston Logging' }});
  logger.error(Error('Testing labels'));

  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2019-04-01
    相关资源
    最近更新 更多