Class LoggingBunyan (3.3.0)

This module provides support for streaming your Bunyan logs to [Stackdriver Logging](https://cloud.google.com/logging).

Inheritance

Writable > LoggingBunyan

Package

@google-cloud/logging-bunyan

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

// Creates a Bunyan Cloud Logging client
const loggingBunyan = new LoggingBunyan();

// 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 entries
logger.error('warp nacelles offline');
logger.info('shields at 99%');

Constructors

(constructor)(options)

constructor(options?: types.Options);

Constructs a new instance of the LoggingBunyan class

Parameter
Name Description
options types.Options

Properties

cloudLog

cloudLog: LogSeverityFunctions;

redirectToStdout

redirectToStdout: boolean;

Methods

_write(record, encoding, callback)

_write(record: types.BunyanLogRecord, encoding: string, callback: Function): void;

Relay a log entry to the logging agent. This is called by bunyan through Writable#write.

Parameters
Name Description
record types.BunyanLogRecord
encoding string
callback Function
Returns
Type Description
void

_writeCall(entries, callback)

_writeCall(entries: Entry | Entry[], callback: Function): void;

A helper function to make a write call

Parameters
Name Description
entries Entry | Entry[]

The entries to be written

callback Function

The callback supplied by Writable.write

Returns
Type Description
void

_writev(chunks, callback)

_writev(chunks: Array<{
        chunk: any;
        encoding: string;
    }>, callback: Function): void;

Relay an array of log entries to the logging agent. This is called by bunyan through Writable#write.

Parameters
Name Description
chunks Array<{ chunk: any; encoding: string; }>
callback Function
Returns
Type Description
void

generateCallback(callback)

generateCallback(callback: Function): ApiResponseCallback;

Creates a combined callback which calls the this.defaultCallback and the Writable.write supplied callback

Parameter
Name Description
callback Function

The callback function provided by Writable

Returns
Type Description
ApiResponseCallback

Combined callback which executes both, this.defaultCallback and one supplied by Writable.write

properLabels(labels)

static properLabels(labels: any): boolean;
Parameter
Name Description
labels any
Returns
Type Description
boolean

stream(level)

stream(level: types.LogLevel): types.StreamResponse;

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

Parameter
Name Description
level types.LogLevel
Returns
Type Description
types.StreamResponse

write(record, callback)

write(record: types.BunyanLogRecord, callback?: Function): boolean;

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.

Parameters
Name Description
record types.BunyanLogRecord
callback Function
Returns
Type Description
boolean

write(record, encoding, callback)

write(record: types.BunyanLogRecord, encoding?: string, callback?: Function): boolean;
Parameters
Name Description
record types.BunyanLogRecord
encoding string
callback Function
Returns
Type Description
boolean