【问题标题】:Structured Logs in Google Cloud Run not being parsed (using Winston for logging)Google Cloud Run 中的结构化日志未被解析(使用 Winston 进行日志记录)
【发布时间】:2020-01-23 02:48:38
【问题描述】:

我正在尝试以 Google Cloud 能够正确提取日志级别的方式格式化我的日志。这是在 Cloud Run 上运行的,带有 typescript。 Cloud Run 正在从容器输出中获取日志。

如果我执行以下操作,谷歌会正确解析日志行:

console.log(JSON.stringify({
   severity: 'ERROR',
   message: 'This is testing a structured log error for GCP'
}));

日志输出如下所示:

我尝试了多种使用 winston 格式化的不同方法,结果如下:

useFormat = format.combine(
      format((info, opts) => {
        info['severity'] = info.level;
        delete info.level;
        return info;
      })(),
      format.json());


    this.winston = winston.createLogger({
      level: logLevel,
      format: useFormat,
      transports: [new winston.transports.Console()]
    });

看起来它会起作用(它正确输出 json 行),我在 GCP 日志中得到了这个:

任何帮助表示赞赏。

【问题讨论】:

    标签: node.js typescript google-cloud-platform winston google-cloud-run


    【解决方案1】:

    原来我很接近,只需要 .upperCase() 日志级别(我正在映射 Verbose -> Debug,我真的不明白为什么 GCP 决定做一个与其他人完全不同的日志分级系统)。新代码:

    useFormat = 
        format.combine(
          format((info, opts) => {
            let level = info.level.toUpperCase();
              if(level === 'VERBOSE') {
                level = 'DEBUG';
              }
    
              info['severity'] = level;
              delete info.level;
              return info;
          })(),
          format.json());
    

    【讨论】:

    • 嗨@patrickWhite 为您的问题找到了答案。请接受它,以便其他 SO 用户更好地了解它。
    • Stackdriver 日志级别记录在 cloud.google.com/logging/docs/reference/v2/rest/v2/… 中,并且基于 tools.ietf.org/html/rfc5424#section-6.2.1,“verbose”一直让我印象深刻,而不是作为一个级别,而是作为对更多输出的请求......例如显示那些调试日志。
    • 我希望作为一个社区,我们刚刚使用 1 到 N 的日志级别,以便人们可以根据需要使用它,但是大多数库已经解决了调试和详细问题,并且没有关键,所以堆栈驱动器只是有点阻抗不匹配。但是,似乎正在工作,现在要弄清楚跟踪等方面的一些漂亮的花里胡哨。
    • 我只想指出,Google 为 Cloud Logging 提供了一个 winston 传输。 cloud.google.com/nodejs/docs/reference/logging-winston/latest
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2018-06-21
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多