LoggingBunyan

LoggingBunyan

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

Constructor

new LoggingBunyan(optionsopt)

Parameters:
Name Type Attributes Description
options object <optional>
Properties
Name Type Attributes Default Description
logName string <optional>

The name of the log that will receive messages written to this bunyan stream. Default: bunyan_Log.

resource object <optional>

The monitored resource that the log stream corresponds to. On Google Cloud Platform, this is detected automatically, but you may optionally specify a specific monitored resource. For more information, see the official documentation

serviceContext object <optional>

For logged errors, we provide this as the service context. For more information see this guide and the official documentation.

Properties
Name Type Attributes Description
service string <optional>

An identifier of the service, such as the name of the executable, job, or Google App Engine service name.

version string <optional>

Represents the version of the service.

projectId string <optional>

The project ID from the Google Cloud Console, e.g. 'grape-spaceship-123'. We will also check the environment variable GCLOUD_PROJECT for your project ID. If your app is running in an environment which supports Application Default Credentials, your project ID will be detected automatically.

keyFilename string <optional>

Full path to the a .json, .pem, or .p12 key downloaded from the Google Cloud Console. If you provide a path to a JSON file, the projectId option above is not necessary. NOTE: .pem and .p12 require you to specify the email option as well.

email string <optional>

Account email address. Required when using a .pem or .p12 keyFilename.

credentials object <optional>

Credentials object.

Properties
Name Type Attributes Description
client_email string <optional>
private_key string <optional>
autoRetry boolean <optional>
true

Automatically retry requests if the response is related to rate limits or certain intermittent server errors. We will exponentially backoff subsequent requests by default.

maxRetries number <optional>
3

Maximum number of automatic retries attempted before returning the error.

promise constructor <optional>

Custom promise module to use instead of native Promises.

promise constructor <optional>

Custom promise module to use instead of native Promises.

maxEntrySize number <optional>

Max size limit of a log entry.

jsonFieldsToTruncate Array.<string> <optional>

A list of JSON properties at the given full path to be truncated.

Examples
Import the client library
```
const {LoggingBunyan} = require('@google-cloud/logging-bunyan');

```
Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:
```
const loggingBunyan = new
LoggingBunyan();

```
Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit credentials</a>:
```
  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%');

Members

LOGGING_SAMPLED_KEY

Value: logging.googleapis.com/trace_sampled

LOGGING_SPAN_KEY

Value: logging.googleapis.com/spanId

LOGGING_TRACE_KEY

Value: logging.googleapis.com/trace

Methods

_write()

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

_writeCall(entries, callback)

A helper function to make a write call

Parameters:
Name Type Description
entries

The entries to be written

callback

The callback supplied by Writable.write

_writev()

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

formatEntry_()

Format a bunyan record into a Stackdriver log entry.

generateCallback(callback)

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

Parameters:
Name Type Description
callback

The callback function provided by Writable

Returns:
Type Description

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

getNodejsLibraryVersion()

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

Returns:
Type Description

The version of this library

stream()

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