GAE node.js console.error() 未记录为 ERROR 日志级别

GAE node.js console.error() not logging as ERROR log level

我不确定我是怎么把它搞砸的,因为它看起来很简单。根据 app engine standard documentation:

console.error('message');

Stackdriver 日志查看器中的日志级别应为 ERROR。但是,我看到日志级别设置为 "Any log level." 从 logName.

中可以看出,它正在记录到 stderr,这似乎是正确的
logName:  "projects/my-project-name/logs/stderr"  

引用:

To emit a log item from your Node.js app, you can use the console.log() or console.error() functions, which have the following log levels:

  • Items emitted with console.log() have the INFO log level.
  • Items emitted with console.error() have the ERROR log level.
  • Internal system messages have the DEBUG log level.

我最初试图让 winston 与 Stackdriver 一起工作(使用 @google-cloud/logging-winston)以获得更精细的日志记录级别,但现在我什至无法让它在 INFO or ERROR with basic console.log() and console.error().

希望我不必为了使用普通 console.error() 而写

经过几个小时的尝试,我成功了。 我根据文档 https://cloud.google.com/logging/docs/setup/nodejs#using_winston.

使用了 Winston

然后在 stackdriver 日志查看器中,我 select 像这样从下拉列表中编辑 winston_log -

现在显示错误日志。

这也适用于 Bunyan。你必须 select bunyan_log 为 Bunyan。

希望这对某人有所帮助:)

tl;dr - 不支持。

更长的答案

看来 Google 在我发布问题后更新了他们的文档。他们已经澄清,写入 stdoutstderr 是收集的,但是:

does not provide log levels that you can use for filtering in the Logs Viewer

很高兴我有一个答案,它是:"No, console.log() and console.error() will not have log levels."这与下一段矛盾。我可能误读或误解了文档,所以如果是这样的话请发表评论。

我截取了文档的屏幕截图,指出了我所指的内容,以防他们再次更新它:

解决方案

使用 Google 支持的日志记录库之一与 Stackdriver 日志记录传输,如 @darshan-devrai 的回答。如果您不想更改所有 console.log()console.error() 等,则只需将控制台记录器别名为您选择的记录器(类似于此 )。

文档 here and hereconsole.error 的记录方式方面似乎存在冲突。

在我的案例中,打印一个简单的对象效果很好:

let gcloudError = {
        severity: 'ERROR',
        message: 'Error in country ' + country + ' ' + JSON.stringify(err.message)
      }
console.error(JSON.stringify(gcloudError));

这将显示为: