Class LoggingBunyan (5.1.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
NameDescription
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
NameDescription
record types.BunyanLogRecord
encoding string
callback Function
Returns
TypeDescription
void

_writeCall(entries, callback)

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

A helper function to make a write call

Parameters
NameDescription
entries Entry | Entry[]

The entries to be written

callback Function

The callback supplied by Writable.write

Returns
TypeDescription
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
NameDescription
chunks Array<{ chunk: any; encoding: string; }>
callback Function
Returns
TypeDescription
void

generateCallback(callback)

generateCallback(callback: Function): ApiResponseCallback;

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

Parameter
NameDescription
callback Function

The callback function provided by Writable

Returns
TypeDescription
ApiResponseCallback

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

getNodejsLibraryVersion()

getNodejsLibraryVersion(): string;

Method used to retrieve the current logging-bunyan library version stored in NODEJS_BUNYAN_DEFAULT_LIBRARY_VERSION

Returns
TypeDescription
string

The version of this library

properLabels(labels)

static properLabels(labels: any): boolean;
Parameter
NameDescription
labels any
Returns
TypeDescription
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
NameDescription
level types.LogLevel
Returns
TypeDescription
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
NameDescription
record types.BunyanLogRecord
callback Function
Returns
TypeDescription
boolean

write(record, encoding, callback)

write(record: types.BunyanLogRecord, encoding?: string, callback?: Function): boolean;
Parameters
NameDescription
record types.BunyanLogRecord
encoding string
callback Function
Returns
TypeDescription
boolean