Sink

Sink

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).

Constructor

new Sink(logging, name)

Parameters:
Name Type Description
logging Logging

Logging instance.

name string

Name of the sink.

See:
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

Members

name :string

Methods

create(config, callbackopt) → {Promise.<CreateSinkResponse>}

Create a sink.

Parameters:
Name Type Attributes Description
config CreateSinkRequest

Config to set for the sink.

callback CreateSinkCallback <optional>

Callback function.

See:
Throws:

if a config object is not provided.

Type
Error
Examples
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

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

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

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.create(config).then(data => {
  const sink = data[0];
  const 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,
  };

  async function createSink() {
    // See https://googleapis.dev/nodejs/logging/latest/Sink.html#create
    await sink.create(config);
    console.log(`Created sink ${sinkName} to ${bucketName}`);
  }
  createSink();

(async) delete(gaxOptionsopt, callbackopt) → {Promise.<DeleteSinkResponse>}

Delete the sink.

Parameters:
Name Type Attributes Description
gaxOptions object <optional>

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

callback DeleteSinkCallback <optional>

Callback function.

See:
Examples
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

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

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.delete().then(data => {
  const 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);

  async function deleteSink() {
    // See https://googleapis.dev/nodejs/logging/latest/Sink.html#delete
    await sink.delete();
    console.log(`Sink ${sinkName} deleted.`);
  }
  deleteSink();

(async) getMetadata(gaxOptionsopt, callbackopt) → {Promise.<SinkMetadataResponse>}

Get the sink's metadata.

Parameters:
Name Type Attributes Description
gaxOptions object <optional>

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

callback SinkMetadataCallback <optional>

Callback function.

See:
Examples
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

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

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.getMetadata().then(data => {
  const metadata = 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 your sink, e.g. my-sink';

  const sink = logging.sink(sinkName);

  async function printSinkMetadata() {
    // See https://googleapis.dev/nodejs/logging/latest/Sink.html#getMetadata
    const [metadata] = await sink.getMetadata();
    console.log(`Name: ${metadata.name}`);
    console.log(`Destination: ${metadata.destination}`);
    console.log(`Filter: ${metadata.filter}`);
  }
  printSinkMetadata();

setFilter(filter, callbackopt) → {Promise.<SetSinkFilterResponse>}

Set the sink's filter.

This will override any filter that was previously set.

Parameters:
Name Type Attributes Description
filter string

The new filter.

callback SetSinkFilterCallback <optional>

Callback function.

See:
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

const filter = 'metadata.severity = ALERT';

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

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

(async) setMetadata(metadata, callbackopt) → {Promise.<SetSinkMetadataResponse>}

Set the sink's metadata.

Parameters:
Name Type Attributes Description
metadata object

See a Sink resource.

Properties
Name Type Attributes Description
gaxOptions object <optional>

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

callback SetSinkMetadataCallback <optional>

Callback function.

See:
Examples
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');

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

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

//-
// If the callback is omitted, we'll return a Promise.
//-
sink.setMetadata(metadata).then(data => {
  const 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 metadataInfo = {
    filter: filter,
  };

  async function updateSink() {
    // See https://googleapis.dev/nodejs/logging/latest/Sink.html#setMetadata
    const [metadata] = await sink.setMetadata(metadataInfo);
    console.log(`Sink ${sinkName} updated.`, metadata);
  }
  updateSink();