Constructor

Sink

new Sink(logging, name)

A sink is an object that lets you to specify a set of log entries to export to a particular destination. Stackdriver Logging lets you export log entries to destinations including Cloud Storage buckets (for long term log storage), Google BigQuery datasets (for log analysis), Google Pub/Sub (for streaming to other applications).

Parameter

logging

Logging

Logging instance.

name

string

Name of the sink.

See also

Introduction to Sinks

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

Property

name

string

Methods

create

create(config, callback) returns Promise containing CreateSinkResponse

Create a sink.

Parameter

config

CreateSinkRequest

Config to set for the sink.

callback

Optional

CreateSinkCallback

Callback function.

See also
Logging#createSink
Throws

Error 

if a config object is not provided.

Returns

Promise containing CreateSinkResponse 

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

var config = {
  destination: {
    // ...
  }
};

sink.create(config, function(err, sink, apiResponse) {
  if (!err) {
    // The sink was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.create(config).then(function(data) {
  var sink = data[0];
  var apiResponse = data[1];
});

Another example:

// Imports the Google Cloud client libraries
const Logging = require('@google-cloud/logging');
const Storage = require('@google-cloud/storage');

// Creates clients
const logging = new Logging();
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines to run the code.
 */
// const sinkName = 'Name of your sink, e.g. my-sink';
// const bucketName = 'Desination bucket, e.g. my-bucket';
// const filter = 'Optional log filer, e.g. severity=ERROR';

// The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic,
// or a BigQuery dataset. In this case, it is a Cloud Storage Bucket.
// See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for
// information on the destination format.
const destination = storage.bucket(bucketName);
const sink = logging.sink(sinkName);

/**
 * The filter determines which logs this sink matches and will be exported
 * to the destination. For example a filter of 'severity>=INFO' will send
 * all logs that have a severity of INFO or greater to the destination.
 * See https://cloud.google.com/logging/docs/view/advanced_filters for more
 * filter information.
 */
const config = {
  destination: destination,
  filter: filter,
};

// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=create
sink
  .create(config)
  .then(() => {
    console.log(`Created sink ${sinkName} to ${bucketName}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

delete

delete(gaxOptions, callback) returns Promise containing DeleteSinkResponse

Delete the sink.

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

callback

Optional

DeleteSinkCallback

Callback function.

See also

projects.sink.delete API Documentation

Returns

Promise containing DeleteSinkResponse 

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

sink.delete(function(err, apiResponse) {
  if (!err) {
    // The log was deleted.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.delete().then(function(data) {
  var apiResponse = data[0];
});

Another example:

// Imports the Google Cloud client library
const Logging = require('@google-cloud/logging');

// Creates a client
const logging = new Logging();

/**
 * TODO(developer): Uncomment the following line to run the code.
 */
// const sinkName = 'Name of sink to delete, e.g. my-sink';

const sink = logging.sink(sinkName);

// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=delete
sink
  .delete()
  .then(() => {
    console.log(`Sink ${sinkName} deleted.`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

getMetadata

getMetadata(gaxOptions, callback) returns Promise containing GetSinkMetadataResponse

Get the sink's metadata.

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

callback

Optional

GetSinkMetadataCallback

Callback function.

See also

Sink Resource

projects.sink.get API Documentation

Returns

Promise containing GetSinkMetadataResponse 

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

sink.getMetadata(function(err, metadata, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.getMetadata().then(function(data) {
  var metadata = data[0];
  var apiResponse = data[1];
});

Another example:

// Imports the Google Cloud client library
const Logging = require('@google-cloud/logging');

// Creates a client
const logging = new Logging();

/**
 * TODO(developer): Uncomment the following line to run the code.
 */
// const sinkName = 'Name of your sink, e.g. my-sink';

const sink = logging.sink(sinkName);

// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=getMetadata
sink
  .getMetadata()
  .then(results => {
    const metadata = results[0];

    console.log(`Name: ${metadata.name}`);
    console.log(`Destination: ${metadata.destination}`);
    console.log(`Filter: ${metadata.filter}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

setFilter

setFilter(filter, callback) returns Promise containing SetSinkFilterResponse

Set the sink's filter.

This will override any filter that was previously set.

Parameter

filter

string

The new filter.

callback

Optional

SetSinkFilterCallback

Callback function.

See also

Advanced Logs Filters

Returns

Promise containing SetSinkFilterResponse 

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

var filter = 'metadata.severity = ALERT';

sink.setFilter(filter, function(err, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.setFilter(filter).then(function(data) {
  var apiResponse = data[0];
});

setMetadata

setMetadata(metadata, callback) returns Promise containing SetSinkMetadataResponse

Set the sink's metadata.

Parameter

metadata

object

See a Sink resource.

Values in metadata have the following properties:

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

callback

Optional

SetSinkMetadataCallback

Callback function.

See also

Sink Resource

projects.sink.update API Documentation

Returns

Promise containing SetSinkMetadataResponse 

Example

var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');

var metadata = {
  filter: 'metadata.severity = ALERT'
};

sink.setMetadata(metadata, function(err, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.setMetadata(metadata).then(function(data) {
  var apiResponse = data[0];
});

Another example:

// Imports the Google Cloud client library
const Logging = require('@google-cloud/logging');

// Creates a client
const logging = new Logging();

/**
 * TODO(developer): Uncomment the following lines to run the code.
 */
// const sinkName = 'Name of sink to update, e.g. my-sink';
// const filter = 'New filter for the sink, e.g. severity >= WARNING';

const sink = logging.sink(sinkName);

/**
 * The filter determines which logs this sink matches and will be exported
 * to the destination. For example a filter of 'severity>=INFO' will send
 * all logs that have a severity of INFO or greater to the destination.
 * See https://cloud.google.com/logging/docs/view/advanced_filters for more
 * filter information.
 */
const metadata = {
  filter: filter,
};

// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/sink?method=setMetadata
sink
  .setMetadata(metadata)
  .then(results => {
    const metadata = results[0];
    console.log(`Sink ${sinkName} updated.`, metadata);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });