Logging app events with Node.js

This part of the Bookshelf tutorial shows how an app can incorporate detailed logging to help you detect, debug, monitor, and solve potential issues, both during development and after your app is in production.

This page is part of a multipage tutorial. To start from the beginning and read the setup instructions, go to Node.js Bookshelf app.

Configuring settings

Copy your config.json file from the authenticating users part of this tutorial to the nodejs-getting-started/5-logging directory.

Installing dependencies

Install dependencies in the nodejs-getting-started/5-logging directory by using npm:

npm install

Running the app on your local machine

  1. Start a local web server using npm:

    npm start
  2. In your web browser, enter the following address:


Deploying the app to the App Engine standard environment

  1. Deploy the sample app from the nodejs-getting-started/5-logging directory:

    gcloud app deploy
  2. In your web browser, enter this address:


    Replace [YOUR_PROJECT_ID] with your GCP project ID.

If you update your app, you deploy the updated version by entering the same command that you used to deploy the app. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain. By default the App Engine standard environment scales to 0 instances when there is no incoming traffic to a version. Thus, unused versions shouldn't cost anything. However, all of these app versions are still billable resources.

See the Cleaning up section in the final step of this tutorial for more information on cleaning up billable resources, including non-default app versions.

Understanding the code

After your app is deployed and running, it's important to know how well it's working. Google Cloud Platform (GCP) has tracing, logging, debugging, and monitoring tools built into the Google Cloud Platform Console, where you can view detailed activity in your app and identify critical issues or trends.

The Bookshelf sample app uses Stackdriver Trace to trace remote procedure calls (RPCs) in the app. You can view the traces in the GCP Console. The sample app uses Stackdriver Debugger along with Cloud Source Repositories to help you debug the app once it's deployed. You can debug the deployed sample app by using the GCP Console.

// Activate Google Cloud Trace and Debug when in production
if (process.env.NODE_ENV === 'production') {

The sample app uses Winston, which is a logging library for Node.js, along with express-winston middleware to manage request and error logging. The code below sets up logging of all requests made to the app to the GCP Console.

const requestLogger = expressWinston.logger({
  transports: [
    new StackdriverTransport(),
    new winston.transports.Console({
      json: false,
      colorize: colorize
  expressFormat: true,
  meta: false

The next code block sets up logging of important errors that occur in the app.

const errorLogger = expressWinston.errorLogger({
  transports: [
    new StackdriverTransport(),
    new winston.transports.Console({
      json: true,
      colorize: colorize

Now that logging is set up, the app can use it. The following code adds the request-logging middleware from logging.requestLogger to the app.


To handle errors, the following code installs the error-logging middleware from logging.errorLogger to the app. It's important that this middleware is added after all other middleware, but before any custom error handlers, so that it can accurately report errors.


// Basic 404 handler
app.use((req, res) => {
  res.status(404).send('Not Found');

// Basic error handler
app.use((err, req, res, next) => {
  /* jshint unused:false */
  // If our routes specified a specific response, then send that. Otherwise,
  // send a generic message so as not to leak anything.
  res.status(500).send(err.response || 'Something broke!');

The app can also log important events from anywhere by using the following code:

logging.info("Something happened");
logging.error("Something bad happened");

To log an object, use the following code:


Logging configuration for the App Engine standard environment

When the sample app runs in the App Engine standard environment, anything logged to stderr and stdout is automatically collected by Stackdriver Logging and is available for viewing, searching, and exporting in the logs viewer in the GCP Console.

The Winston configuration ensures that all logs are written to stderr or stdout.

Viewing logs

After the sample app is deployed and running, Logging starts collecting logging data. You can analyze this logging data with the log monitoring tools built into the GCP Console. The following image shows logged events in the GCP Console.

Logging in the GCP Console

If you want more detailed analysis, you can stream or import the app's logs into BigQuery or export them to a Cloud Storage bucket. You can do both of these logging tasks from the GCP Console.

क्या यह पेज उपयोगी था? हमारे काम के बारे में अपनी राय दें:

निम्न के बारे में फ़ीडबैक भेजें...