Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 987
ahong Avatar asked Jan 11 '19 17:01

ahong


People also ask

Does console error work in node js?

error() function from console class of Node. js is used to display an error messages on the console. It prints to stderr with newline. Parameter: This function can contains multiple parameters.

How do I log errors in node js?

The built-in console module in Node. js lets you write log messages to standard output (stdout) and standard error (stderr)using the log and error functions, respectively. This module is most definitely the simplest method for getting started, since it doesn't require importing or configuring any third-party packages.

What is the use of console log () method in node js?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

How do I enable logging in node js?

To enable this logger, you have run your application with a special environment variable, called DEBUG . Once you do that, the debug module will come to life and will start producing log events for stdout. Luckily, this module is widespread in the Node.


1 Answers

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

like image 100
Darshan Devrai Avatar answered Oct 12 '22 10:10

Darshan Devrai