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 VM instance.

Using Bunyan

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

Installing the plugin

  1. Before installing the plugin, make sure you've prepared your environment for Node.js development.

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

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

  3. 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');
    
    // Instantiates a Bunyan Stackdriver Logging client
    const loggingBunyan = 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',
      // log at 'info' and above
      level: 'info',
      streams: [
        // Log to the console
        { stream: process.stdout },
        // And log to Stackdriver Logging
        loggingBunyan.stream()
      ]
    });
    
    // Writes some log entries
    logger.error('warp nacelles offline');
    logger.info('shields at 99%');

For more information or to report issues with the plugin, see the plugin's source code repository.

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 an easy to use, higher-level layer for working with Stackdriver Logging.

Installing the plugin

  1. Before installing the plugin, make sure you've prepared your environment for Node.js development.

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

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

  3. 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');
    
    // Instantiates a Winston Stackdriver Logging client
    const loggingWinston = 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%');

For more information or to report issues with the plugin, see the plugin's source code repository.

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.

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

The Stackdriver Logging libraries for Node.js should work without the need to manually provide authentication credentials for instances running on Google Cloud Platform, as long as the Stackdriver Logging API access scope is enabled on that instance.

Cloud Functions

On Google Cloud Functions, the Stackdriver Logging API access scope is enabled by default, and the Stackdriver Logging libraries for Node.js can be used without needing to provide credentials or a project ID.

However, Cloud Functions is configured to use Stackdriver Logging automatically. Logs written to stdout and stderr are sent to Stackdriver Logging for you.

App Engine

On Google App Engine, the Stackdriver Logging API access scope is enabled by default, and the Stackdriver Logging libraries for Node.js can be used without needing to provide credentials or a project ID.

However, App Engine is configured to use Stackdriver Logging automatically. Logs written to stdout and stderr are sent to Stackdriver Logging for you.

Container Engine

On Google Container Engine, you need to explicitly add the logging.write OAuth scope when creating the cluster:

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

Compute Engine

For Google Compute Engine instances, you need to explicitly enable the logging.write Stackdriver Logging API access scope for each instance. When creating a new instance through the Google Cloud Platform Console, you can do this under Identity and API access: Use the Compute Engine default service account and select "Allow full access to all Cloud APIs" under Access scopes.

To use something other than the Compute Engine default service account see the docs for Creating and Enabling Service Accounts for Instances and the Running elsewhere section below. The important thing is that the service account you use has the Logs Writer role.

Running locally and elsewhere

If your application is running outside of Google Cloud Platform, such as on AWS, locally, on-premise, or on another cloud provider, you can still use Stackdriver Logging, provided that you supply a project ID and credentials to the Stackdriver Logging libraries for Node.js, as described in the following sections.

Project ID

Provide a project ID to the Stackdriver Logging libraries for Node.js by setting the projectId value in the options object passed to the libraries' constructors, or set the GCLOUD_PROJECT environment variable:

Linux or Mac OS X

export GCLOUD_PROJECT=your-project-id

Windows

set GCLOUD_PROJECT=your-project-id

Powershell:

$env:GCLOUD_PROJECT="your-project-id"

Credentials

The Stackdriver Logging libraries for Node.js use a strategy called Application Default Credentials (ADC) to find your application's credentials. You can provide credentials to the Stackdriver Logging libraries for Node.js by setting the keyFilename value in the options object passed to the libraries' constructors, or you can set the GOOGLE_APPLICATION_CREDENTIALS environment variable. For more information, see Providing credentials to your application.

Examples

With Bunyan:

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

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

With Winston:

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

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

Viewing the logs

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

Go to the Logs Viewer page

Send feedback about...

Stackdriver Logging