Logging

Logging

Stackdriver Logging allows you to store, search, analyze, monitor, and alert on log data and events from Google Cloud Platform and Amazon Web Services (AWS).

Constructor

new Logging(optionsopt)

Parameters:
Name Type Attributes Description
options ClientConfig <optional>

Configuration options.

Source:
See:
Examples

Import the client library

const {Logging} = require('@google-cloud/logging');
<caption>Create a client that uses <a
href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
Default Credentials (ADC)</a>:</caption> const logging = new Logging();
<caption>Create a client with <a
href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
credentials</a>:</caption> const logging = new Logging({ projectId:
'your-project-id', keyFilename: '/path/to/keyfile.json'
});

Full quickstart example:

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

async function quickstart(
  projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
  logName = 'my-log' // The name of the log to write to
) {
  // Creates a client
  const logging = new Logging({projectId});

  // Selects the log to write to
  const log = logging.log(logName);

  // The data to write to the log
  const text = 'Hello, world!';

  // The metadata associated with the entry
  const metadata = {
    resource: {type: 'global'},
  };

  // Prepares a log entry
  const entry = log.entry(metadata, text);

  async function writeLog() {
    // Writes the log entry
    await log.write(entry);
    console.log(`Logged: ${text}`);
  }
  writeLog();
}

Methods

entry(resourceopt, data) → {Entry}

Create an entry object.

Using this method will not itself make any API requests. You will use the object returned in other API calls, such as Log#write.

Note, Cloud Logging Quotas and limits dictates that the maximum log entry size, including all LogEntry Resource properties, cannot exceed approximately 256 KB.

Parameters:
Name Type Attributes Description
resource object | string <optional>

See a Monitored Resource.

data object | string

The data to use as the value for this log entry.

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

const resource = {
  type: 'gce_instance',
  labels: {
    zone: 'global',
    instance_id: '3'
  }
};

const entry = logging.entry(resource, {
  delegate: 'my_username'
});

entry.toJSON();
// {
//   resource: {
//     type: 'gce_instance',
//     labels: {
//       zone: 'global',
//       instance_id: '3'
//     }
//   },
//   jsonPayload: {
//     delegate: 'my_username'
//   }
// }

getEntriesStream(queryopt) → {ReadableStream}

List the Entry objects in your logs as a readable object stream.

Parameters:
Name Type Attributes Description
query GetEntriesRequest <optional>

Query object for listing entries.

Source:
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();

logging.getEntriesStream()
  .on('error', console.error)
  .on('data', entry => {
    // `entry` is a Stackdriver Logging entry object.
    // See the `data` property to read the data from the entry.
  })
  .on('end', function() {
    // All entries retrieved.
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
logging.getEntriesStream()
  .on('data', function(entry) {
    this.end();
  });

getLogsStream(queryopt) → {ReadableStream}

List the Log objects in your project as a readable object stream.

Parameters:
Name Type Attributes Description
query GetLogsRequest <optional>

Query object for listing entries.

Source:
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();

logging.getLogsStream()
  .on('error', console.error)
  .on('data', log => {
    // `log` is a Stackdriver Logging log object.
  })
  .on('end', function() {
    // All logs retrieved.
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
logging.getLogsStream()
  .on('data', log => {
    this.end();
  });

getSinksStream(queryopt) → {ReadableStream}

Get the Sink objects associated with this project as a readable object stream.

Parameters:
Name Type Attributes Description
query GetSinksRequest <optional>

Query object for listing sinks.

Source:
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();

logging.getSinksStream()
  .on('error', console.error)
  .on('data', sink => {
    // `sink` is a Sink object.
  })
  .on('end', function() {
    // All sinks retrieved.
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
logging.getSinksStream()
  .on('data', function(sink) {
    this.end();
  });

log(name, optionsopt) → {Log}

Get a reference to a Stackdriver Logging log.

Parameters:
Name Type Attributes Description
name string

Name of the existing log.

options object <optional>

Configuration object.

Properties
Name Type Attributes Description
removeCircular boolean <optional>

Replace circular references in logged objects with a string value, [Circular]. (Default: false)

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

request(config, callbackopt)

Funnel all API requests through this method, to be sure we have a project ID.

Parameters:
Name Type Attributes Description
config object

Configuration object.

Properties
Name Type Description
gaxOpts object

GAX options.

method function

The gax method to call.

reqOpts object

Request options.

callback function <optional>

Callback function.

Source:

sink(name) → {Sink}

Get a reference to a Stackdriver Logging sink.

Parameters:
Name Type Description
name string

Name of the existing sink.

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