Setting Up Stackdriver Logging for Node.js

You can write logs to Stackdriver Logging from Node.js applications by using our Bunyan or Winston plugins, or by using the Stackdriver Logging Cloud client library for Node.js directly.

The Stackdriver Logging agent does not have to be installed to use the Bunyan and Winston libraries on a Compute Engine VM instance.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Stackdriver Logging API.

    Enable the API

  5. Prepare your environment for Node.js development.

    Go to the Node.js setup guide

Using Bunyan

We provide a plugin for the Bunyan Node.js logging library. The Stackdriver Logging plugin for Bunyan provides a simple, higher-level layer for working with Stackdriver Logging.

Installing the plugin

  1. The easiest way to install the Stackdriver Logging Bunyan plugin is with npm:

    npm install --save @google-cloud/logging-bunyan

  2. Import the plugin and add it to your Bunyan configuration:

    const bunyan = require('bunyan');
    
    // Imports the Google Cloud client library for Bunyan
    const LoggingBunyan = require('@google-cloud/logging-bunyan');
    
    // Creates a Bunyan Stackdriver Logging client
    const loggingBunyan = new LoggingBunyan();
    
    // Create a Bunyan logger that streams to Stackdriver Logging
    // Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
    const logger = bunyan.createLogger({
      // The JSON payload of the log as it appears in Stackdriver Logging
      // will contain "name": "my-service"
      name: 'my-service',
      streams: [
        // Log to the console at 'info' and above
        {stream: process.stdout, level: 'info'},
        // And log to Stackdriver Logging, logging at 'info' and above
        loggingBunyan.stream('info'),
      ],
    });
    
    // Writes some log entries
    logger.error('warp nacelles offline');
    logger.info('shields at 99%');

Configuring the plugin

You can customize the behavior of the Bunyan plugin using the same configuration options supported by the Stackdriver Logging Cloud client library for Node.js. These options can be passed in the options object passed to the plugin's constructor.

Using Winston

We provide a plugin for the Winston Node.js logging library. The Stackdriver Logging plugin for Winston provides a simple, higher-level layer for working with Stackdriver Logging.

Installing the plugin

  1. The easiest way to install the Stackdriver Logging Winston plugin is with npm:

    npm install --save @google-cloud/logging-winston

  2. Import the plugin and add it to your Winston configuration:

    const winston = require('winston');
    const Logger = winston.Logger;
    const Console = winston.transports.Console;
    
    // Imports the Google Cloud client library for Winston
    const LoggingWinston = require('@google-cloud/logging-winston');
    
    // Creates a Winston Stackdriver Logging client
    const loggingWinston = new LoggingWinston();
    
    // Create a Winston logger that streams to Stackdriver Logging
    // Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
    const logger = new Logger({
      level: 'info', // log at 'info' and above
      transports: [
        // Log to the console
        new Console(),
        // And log to Stackdriver Logging
        loggingWinston,
      ],
    });
    
    // Writes some log entries
    logger.error('warp nacelles offline');
    logger.info('shields at 99%');

Configuring the plugin

You can customize the behavior of the Winston plugin using the same configuration options supported by the Stackdriver Logging Cloud client library for Node.js. These options can be passed in the options object passed to the plugin's constructor.

For more information on installation, see the documentation for the Stackdriver Logging libraries for Node.js. You can also report issues using the issue tracker.

Using the Cloud Client Library directly

For information on using the Stackdriver Logging Cloud client library for Node.js directly, see Stackdriver Logging Client Libraries.

Running on Google Cloud Platform

Using Stackdriver Logging libraries for Node.js requires the Cloud IAM Logs Writer role on Google Cloud Platform. Most Google Cloud Platform environments provide this role by default.

App Engine

Google App Engine grants the Logs Writer role by default.

The Stackdriver Logging libraries for Node.js can be used without needing to explicitly provide credentials.

Stackdriver Logging is automatically enabled for App Engine flexible environment applications. No additional setup is required.

Kubernetes Engine

On Google Kubernetes Engine, you must add the logging.write access scope when creating the cluster:

gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/logging.write

Compute Engine

When using Google Compute Engine VM instances, add the cloud-platform access scope to each instance. When creating a new instance through the Google Cloud Platform Console, you can do this in the Identity and API access section of the Create Instance panel. Use the Compute Engine default service account or another service account of your choice, and select Allow full access to all Cloud APIs in the Identity and API access section. Whichever service account you select, ensure that it has been granted the Logs Writer role in the IAM & admin section of the GCP Console.

Cloud Functions

Google Cloud Functions grants the Logs Writer role by default.

The Stackdriver Logging libraries for Node.js can be used without needing to explicity provide credentials.

Cloud Functions is configured to use Stackdriver Logging automatically.

Running locally and elsewhere

To use the Stackdriver Logging libraries for Node.js outside of Google Cloud Platform, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Logging libraries for Node.js. This applies to running the agent on your own workstation, on your data center's computers, or on the VM instances of another cloud provider. For more information see Obtaining and providing service account credentials manually.

Using Bunyan:

// Imports the Google Cloud client library for Bunyan
const LoggingBunyan = require('@google-cloud/logging-bunyan');

// Creates a client
const loggingBunyan = new LoggingBunyan({
  projectId: 'your-project-id',
  keyFilename: '/path/to/key.json',
});

Using Winston:

// Imports the Google Cloud client library for Winston
const LoggingWinston = require('@google-cloud/logging-winston');

// Creates a client
const loggingWinston = new LoggingWinston({
  projectId: 'your-project-id',
  keyFilename: '/path/to/key.json',
});

Viewing the logs

After deployment, you can view the logs in the GCP Console Logs Viewer.

Go to the Logs Viewer

For more information, see Viewing Logs.

Send feedback about...

Stackdriver Logging