Google App Engine NodeJS log severity

507 views
Skip to first unread message

Marco Galassi

unread,
Mar 31, 2017, 11:03:20 AM3/31/17
to Google App Engine

I am using nodejs on App Engine and I would like to have the ability to log my application in an easy manner.
Possibily, I would like to be able to log as easy as in python standard environment:

import logging
import webapp2
class MainPage(webapp2.RequestHandler):
  def get(self):
    logging.debug('This is a debug message')
    logging.info('This is an info message')
    logging.warning('This is a warning message')
    logging.error('This is an error message')
    logging.critical('This is a critical message')
    try:
      raise ValueError('This is a sample value error.')
    except ValueError:
      logging.exception('A example exception log.')
    self.response.out.write('Logging example.')
 
 
app = webapp2.WSGIApplication([ 
    ('/', MainPage)
], debug=True)

This is very useful since it shows the logs with the corresponding logging severity in the Stackdriver Logging console.
Unfortunately, it doesn't look that something like this is possible on NodeJS.

I have tried to follow what the documentation says on logging on NodeJS, which says that I can use winston-gae, but it doesn't work at all.

Any help is very appreciated.

Adam (Cloud Platform Support)

unread,
Mar 31, 2017, 6:25:40 PM3/31/17
to Google App Engine
I've also been unable to get winston-gae working. It hasn't been updated in over 2 years and is not an official Google library, so for now it seems it's in need of a maintainer.

The standard winston logger does work, but only outputs to stdout and as such you don't get proper severity in the Logs Viewer.

Marco Galassi

unread,
Apr 3, 2017, 3:00:19 AM4/3/17
to Google App Engine
Yes, but that basically is just the same as doing 
console.log('stdout');
console
.error('stderr');

It would be nice to open an issue to ask google to implement it. Where could I do this?

Marco

Adam (Cloud Platform Support)

unread,
Apr 4, 2017, 3:56:55 PM4/4/17
to Google App Engine
I see you'd already opened an issue but it was closed. The reason being is that Google does not maintain the winston-gae project, which is an open source project. We do provide access to the Cloud Logging APIs via google-cloud-node though which you can use to implement your own logger or use directly in your code (or which can be referenced to update winston-gae, or implement a different solution).
Message has been deleted

Marco Galassi

unread,
Apr 5, 2017, 3:32:00 AM4/5/17
to Google App Engine
Hi Adam,
thank you for your answer.
I am probably just dumb, but I can't make the Cloud Logging APIs work.
I have installed @google-cloud/logging through
npm install --save @google-cloud/logging
as pointed out in the documentation, put a random 
logging.log('this is a log')
in the / root handler, before sending back a response to the client, and still, when I access the page,
I only get an internal server error. The corresponding log in Stackdriver is simply: "TypeError: logging.log is not a function at app.get"
My code currently looks as simple as this:
const express = require('express');
var logging = require('@google-cloud/logging');
const app = express();

app.get('/', (reqres=> {
  logging.log('this is a super cool log'); // Shouldn't this log?
  res.status(200).send('Hello, world!');
});

if (module === require.main) {
  const server = app.listen(process.env.PORT || 8080, () => {
    const port = server.address().port;
    console.log(`App listening on port ${port}`);
  })
}
module.exports = app;


I can't understand very well how to use it nor I can find a complete example.

Any help would be really appreciated.

Adam (Cloud Platform Support)

unread,
Apr 8, 2017, 3:08:55 PM4/8/17
to Google App Engine
Actually, when browsing the sources for google-cloud-node I discovered there is now a new winston transport being developed - @google-cloud/logging-winston. Usage is greatly simplified:

var winston = require('winston');
var transport = require('@google-cloud/logging-winston');

winston.add(transport);

winston.error('warp nacelles offline');
winston.verbose('sheilds at 99%');
Reply all
Reply to author
Forward
0 new messages