Constructor

LoggingBunyan

new LoggingBunyan(options)

This module provides support for streaming your Bunyan logs to Stackdriver Logging.

Parameter

options

Optional

object

Examples

Import the client library

const LoggingBunyan = require('@google-cloud/logging-bunyan');

Create a client that uses Application Default Credentials (ADC):

const loggingBunyan = new LoggingBunyan();

Create a client with explicit credentials:

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

Full quickstart example:

const bunyan = require('bunyan');

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

// 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%');

Property

LOGGING_TRACE_KEY

static

string

Value: logging.googleapis.com/trace

Methods

stream

stream(level)

Convenience method that Builds a bunyan stream object that you can put in the bunyan streams list.

Parameter

level

(string or number)

A bunyan logging level. Log entries at or above this level will be routed to Stackdriver Logging.

Example

const logger = bunyan.createLogger({
  name: 'my-service',
  streams: [
    loggingBunyan.stream('info')
  ]
});

write

write(record, encoding, callback)

Intercept log entries as they are written so we can attempt to add the trace ID in the same continuation as the function that wrote the log, because the trace agent currently uses continuation local storage for the trace context.

By the time the Writable stream buffer gets flushed and _write gets called we may well be in a different continuation.

Parameter

record

object

encoding

string

callback

function()