Constructor

LoggingWinston

new LoggingWinston(options)

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

Parameter

options

Optional

object

Values in options have the following properties:

Parameter

level

Optional

object

The default log level. Winston will filter messages with a severity lower than this.

levels

Optional

object

Custom logging levels as supported by winston. This list is used to translate your log level to the Stackdriver Logging level. Each property should have an integer value between 0 (most severe) and 7 (least severe). If you are passing a list of levels to your winston logger, you should provide the same list here.

inspectMetadata

Optional

boolean

Serialize winston-provided log metadata using util.inspect.

logName

Optional

string

The name of the log that will receive messages written to this transport.

resource

Optional

object

The monitored resource that the transport 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

Optional

object

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

serviceContext.service

Optional

string

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

serviceContext.version

Optional

string

Represents the version of the service.

projectId

Optional

string

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

Optional

string

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

Optional

string

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

credentials

Optional

object

Credentials object.

credentials.client_email

Optional

string

credentials.private_key

Optional

string

autoRetry

Optional

boolean

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

Optional

number

Maximum number of automatic retries attempted before returning the error.

promise

Optional

constructor

Custom promise module to use instead of native Promises.

Examples

Import the client library

const LoggingWinston = require('@google-cloud/logging-winston');

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

const loggingWinston = new LoggingWinston();

Create a client with explicit credentials:

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

Full quickstart example:

const winston = require('winston');
const Logger = winston.Logger;
const Console = winston.transports.Console;

// Imports the Google Cloud client library for Winston
const LoggingWinston = require('@google-cloud/logging-winston');

// Creates a Winston Stackdriver Logging client
const loggingWinston = new LoggingWinston();

// Create a Winston logger that streams to Stackdriver Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
const logger = new Logger({
  level: 'info', // log at 'info' and above
  transports: [
    // Log to the console
    new Console(),
    // And log to Stackdriver Logging
    loggingWinston,
  ],
});

// 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

Method

log

log(levelName, msg, metadata, callback)

Relay a log entry to the logging agent. This is normally called by winston.

Parameter

levelName

string

The severity level at which this entry is being logged. This should match one of the levels provided to the constructor which defaults to npm levels. This level will be translated to the appropriate Stackdriver logging severity level.

msg

string

The message to be logged.

metadata

Optional

object

Winston-provided metadata that should be attached to the log entry. If a httpRequest property is set, it will be treated as a HttpRequest request log message. If options.inspectMetadata is set, we will convert the remaining properties to strings before reporting.

callback

function()

A callback that is invoked when the logging agent either succeeds or gives up writing the log entry to the remote server.