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

3

9
  • 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().

Gelt answered 11/1, 2019 at 17:47 Comment(3)
I am facing the same issue. Have you got it working?Bolling
@DarshanDevrai sorry, I haven't. I've put this issue on the back burner, but hope to revisit it. If I find a solution or if google gives some support I'll happily answer my own question.Gelt
I revisited the documentation and it seems like this might not be possible. Not sure if I missed this the first time or they added it recently (Last updated September 17, 2019). It now says "Write your application logs using stdout for output and stderr for errors. These files are automatically collected and can be viewed in the Logs Viewer. Note that this does not provide log levels that you can use for filtering in the Logs Viewer; however, the Logs Viewer does provide other filtering, such as text, timestamp, etc." Emphasis mine. Doc kind of contradicts itself.Gelt
G
3

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).

Gelt answered 29/10, 2019 at 16:55 Comment(0)
B
3

After trying for hours, I got it working. I used Winston as per the docs https://cloud.google.com/logging/docs/setup/nodejs#using_winston.

Then in stackdriver logs viewer, I selected the winston_log from the dropdown like this - enter image description here

And its showing error logs now.

This works with Bunyan too. You have to select bunyan_log for the Bunyan.

Hope this helps someone:)

Bolling answered 10/10, 2019 at 16:49 Comment(4)
I'll give it a try! does stderr or plain console.error not log at error level?Gelt
I know stderr or plain console.error should log at error level as per the docs but In my case only Winston and Bunyan do.Bolling
see my comment on my own question. Looks like they edited the docs since I last read them and it kind of contradicts itself now.Gelt
Darshan thanks for your answer. I upvoted it, but decided to answer the question more directly.Gelt
G
3

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).

Gelt answered 29/10, 2019 at 16:55 Comment(0)
C
1

The docs here and here seem to be conflicting in terms of how console.error is logged.

In my case printing a simple object worked fine:

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

This will be displayed as: Error on log dashboard

Coelho answered 16/2, 2021 at 9:13 Comment(1)
Thanks for contributing. First the links you posted are about Cloud Functions, which is a different PaaS than App Engine. Second your first link is about "Reporting Errors" which is a different service than Logging, so they do not conflict.Gelt

© 2022 - 2024 — McMap. All rights reserved.