Cloud Logging for Bunyan: Node.js Client

release levelnpm version

This module provides an easy to use, higher-level layer for working withCloud Logging,compatible withBunyan. Simply attach this as a transport to your existing Bunyan loggers.

A comprehensive list of changes in each version may be found inthe CHANGELOG.

Read more about the client libraries for Cloud APIs, including the olderGoogle APIs Client Libraries, inClient Libraries Explained.

Table of contents:

Quickstart

Before you begin

  1. Select or create a Cloud Platform project.
  2. Enable the Cloud Logging for Bunyan API.
  3. Set up authentication with a service account so you can access theAPI from your local workstation.

Installing the client library

npm install @google-cloud/logging-bunyan

Using the client library

const bunyan = require('bunyan');// Imports the Google Cloud client library for Bunyanconst {LoggingBunyan} = require('@google-cloud/logging-bunyan');// Creates a Bunyan Cloud Logging clientconst loggingBunyan = newLoggingBunyan();// Create a Bunyan logger that streams to Cloud 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 Cloud 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 Cloud Logging, logging at 'info' and above    loggingBunyan.stream('info'),  ],});// Writes some log entrieslogger.error('warp nacelles offline');logger.info('shields at 99%');

Using as an express middleware

NOTE: this feature is experimental. The API may change in a backwardsincompatible way until this is deemed stable. Please provide us feedback sothat we can better refine this express integration.

We provide a middleware that can be used in an express application. Apart frombeing easy to use, this enables some more powerful features of CloudLogging: request bundling. Any application logs emitted on behalf of a specificrequest will be shown nested inside the request log as you see in thisscreenshot:

Request Bundling Example

The middleware adds abunyan-style log function to therequest object. Youcan use this wherever you have access to therequest object (req in thesample below). All log entries that are made on behalf of a specific request areshown bundled together in the Cloud Logging UI.

const lb = require('@google-cloud/logging-bunyan');// Import express module and create an http server.const express = require('express');async function startServer() {  const {logger, mw} = await lb.express.middleware();  const app = express();  // Install the logging middleware. This ensures that a Bunyan-style `log`  // function is available on the `request` object. Attach this as one of the  // earliest middleware to make sure that log function is available in all the  // subsequent middleware and routes.  app.use(mw);  // Setup an http route and a route handler.  app.get('/', (req, res) => {    // `req.log` can be used as a bunyan style log method. All logs generated    // using `req.log` use the current request context. That is, all logs    // corresponding to a specific request will be bundled in the Cloud UI.    req.log.info('this is an info log message');    res.send('hello world');  });  // `logger` can be used as a global logger, one not correlated to any specific  // request.logger.info({port: 8080}, 'bonjour');  // Start listening on the http server.  app.listen(8080, () => {    console.log('http server listening on port 8080');  });}startServer();

Error Reporting

AnyError objects you log at severityerror or higher can automatically be picked up byCloud Error Reporting if you have specified aserviceContext.service when instantiating aLoggingBunyan instance:

const loggingBunyan = new LoggingBunyan({  serviceContext: {    service: 'my-service', // required to report logged errors                           // to the Google Cloud Error Reporting                           // console    version: 'my-version'  }});

It is an error to specify aserviceContext but not specifyserviceContext.service.

Make sure to add logs to youruncaught exception andunhandled rejection handlers if you want to see those errors too.

You may also want to see the [@google-cloud/error-reporting][@google-cloud/error-reporting] module which provides direct access to the Error Reporting API.

Special Payload Fields in LogEntry

There are some fields that are considered special by Google cloud logging and will be extracted into the LogEntry structure. For example,severity,message andlabels can be extracted to LogEntry if included in the bunyan log payload. Thesespecial JSON fields will be used to set the corresponding fields in theLogEntry. Please be aware of these special fields to avoid unexpected logging behavior.

LogEntry Labels

If the bunyan log record contains a label property where all the values are strings, we automatically promote thatproperty to be theLogEntry.labels value ratherthan being one of the properties in thepayload fields. This makes it easier to filter the logs in the UI using the labels.

logger.info({labels: {someKey: 'some value'}}, 'test log message');

All the label values must be strings for this automatic promotion to work. Otherwise the labels are left in the payload.

Formatting Request Logs

To format your request logs you can provide ahttpRequest property on the bunyan metadata you provide along with the log message. We will treat this as theHttpRequest message and Cloud logging will show this as a request log. Example:

Request Log Example

logger.info({  httpRequest: {    status: res.statusCode,    requestUrl: req.url,    requestMethod: req.method,    remoteIp: req.connection.remoteAddress,    // etc.  }}, req.path);

ThehttpRequest property must be a properly formattedHttpRequest message. (Note: the linked protobuf documentation showssnake_case property names, but in JavaScript one needs to provide property names incamelCase.)

Correlating Logs with Traces

If you use [@google-cloud/trace-agent][trace-agent] module, then this module will set the Cloud Logging [LogEntry][LogEntry]trace property based on the current trace context when available. That correlation allows you to [view log entries][trace-viewing-log-entries] inline with trace spans in the Cloud Trace Viewer. Example:

Logs in Trace Example

If you wish to set the Cloud LogEntrytrace property with a custom value, then write a Bunyan log entry property for'logging.googleapis.com/trace', which is exported by this module asLOGGING_TRACE_KEY. For example:

const bunyan = require('bunyan');// Node 6+const {LoggingBunyan, LOGGING_TRACE_KEY} = require('@google-cloud/logging-bunyan');const loggingBunyan = LoggingBunyan();...logger.info({  [LOGGING_TRACE_KEY]: 'custom-trace-value'}, 'Bunyan log entry with custom trace field');

Error handling with a default callback

TheLoggingBunyan class creates an instance ofLogging which creates theLog class from@google-cloud/logging package to write log entries. TheLog class writes logs asynchronously and there are cases when log entries cannot be written when it fails or an error is returned from Logging backend.If the error is not handled, it could crash the application. One possible way to handle the error is to provide a default callbackto theLoggingBunyan constructor which will be used to initialize theLog object with that callback like in the example below:

// Imports the Google Cloud client library for Bunyanconst {LoggingBunyan} = require('@google-cloud/logging-bunyan');// Creates a clientconst loggingBunyan = newLoggingBunyan({  projectId: 'your-project-id',  keyFilename: '/path/to/key.json',  defaultCallback: err => {      if (err) {      console.log('Error occured: ' + err);      }  },});

Alternative way to ingest logs in Google Cloud managed environments

If you use this library with the Cloud Logging Agent, you can configure the handler to output logs toprocess.stdout usingthestructured logging Json format.To do this, addredirectToStdout: true parameter to theLoggingBunyan constructor as in sample below.You can use this parameter when running applications in Google Cloud managed environments such as AppEngine, Cloud Run,Cloud Function or GKE. The logger agent installed on these environments can captureprocess.stdout and ingest it into Cloud Logging.The agent can parse structured logs printed toprocess.stdout and capture additional log metadata beside the log payload.It is recommended to setredirectToStdout: true in serverless environments like Cloud Functions since it could decrease logging record loss upon execution termination - since all logs are written toprocess.stdout thosewould be picked up by the Cloud Logging Agent running in Google Cloud managed environment. Note that there is also auseMessageField option which controls if "message" field is used to store structured, non-text data insidejsonPayload field whenredirectToStdout is set. By defaultuseMessageField is alwaystrue.Set theskipParentEntryForCloudRun option to skip creating an entry for the request itself as Cloud Run already automatically createssuch log entries. This might become the default behaviour in a next major version.

// Imports the Google Cloud client library for Bunyanconst {LoggingBunyan} = require('@google-cloud/logging-bunyan');// Creates a clientconst loggingBunyan = newLoggingBunyan({  projectId: 'your-project-id',  keyFilename: '/path/to/key.json',  redirectToStdout: true,});

Samples

Samples are in thesamples/ directory. Each sample'sREADME.md has instructions for running its sample.

SampleSource CodeTry it
Expresssource codeOpen in Cloud Shell
Quickstartsource codeOpen in Cloud Shell
Explict Auth Setupsource codeOpen in Cloud Shell

TheCloud Logging for Bunyan Node.js Client API Reference documentationalso contains samples.

Supported Node.js Versions

Our client libraries follow theNode.js release schedule.Libraries are compatible with all currentactive andmaintenance versions ofNode.js.If you are using an end-of-life version of Node.js, we recommend that you updateas soon as possible to an actively supported LTS version.

Google's client libraries support legacy versions of Node.js runtimes on abest-efforts basis with the following warnings:

  • Legacy versions are not tested in continuous integration.
  • Some security patches and features cannot be backported.
  • Dependencies cannot be kept up-to-date.

Client libraries targeting some end-of-life versions of Node.js are available, andcan be installed through npmdist-tags.The dist-tags follow the naming conventionlegacy-(version).For example,npm install @google-cloud/logging-bunyan@legacy-8 installs client librariesfor versions compatible with Node.js 8.

Versioning

This library followsSemantic Versioning.

This library is considered to bestable. The code surface will not change in backwards-incompatible waysunless absolutely necessary (e.g. because of critical security issues) or withan extensive deprecation period. Issues and requests againststable librariesare addressed with the highest priority.

More Information:Google Cloud Platform Launch Stages

Contributing

Contributions welcome! See theContributing Guide.

Please note that thisREADME.md, thesamples/README.md,and a variety of configuration files in this repository (including.nycrc andtsconfig.json)are generated from a central template. To edit one of these files, make an editto its templates indirectory.

License

Apache Version 2.0

SeeLICENSE

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.