Search code examples
javascriptnode.jsgoogle-app-engineloggingstackdriver

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


  • google app-engine standard
  • runtime: nodejs10

I'm not sure how I'm messing this up since it seems so simple. According to the app engine standard documentation:

console.error('message');

Should have the ERROR log level in the Stackdriver Logs Viewer. However, I see the log level set to "Any log level." What does seem correct is it's logging to stderr as seen from the logName.

logName:  "projects/my-project-name/logs/stderr"  

To quote:

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.

I was originally trying to get winston to work with Stackdriver (using @google-cloud/logging-winston) to get more granular logging levels, but right now I can't even get it to log at INFO or ERROR with basic console.log() and console.error().

Hope I don't have to write a crazy custom transport just to use plain console.error().


Solution

  • tl;dr - it's not supported.

    Longer Answer

    So it seems like Google has updated their documentation since I've posted my question. They've clarified that writing to stdout and stderr is collected, but:

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

    It's great that I have an answer, which is: "No, console.log() and console.error() will not have log levels." This kind of contradicts the next paragraph. I may be misreading or misinterpreting the documentation, so please comment if that's the case.

    I took a screenshot of the documentation, pointing out what I'm referring to, in case they update it again: Google App Engine Documentation on Logging

    Solution

    Use one of the logging libraries that Google supports with a Stackdriver logging transport, like @darshan-devrai's answer. If you don't want to change all your console.log(), console.error(), etc., then just alias the console logger to your logger of choice (similar to this stackoverflow answer).