Constructor

Log

new Log(logging, name, options)

Parameter

logging

Logging

Logging instance.

name

string

Name of the log.

options

Optional

object

Configuration object.

Values in options have the following properties:

Parameter

removeCircular

Optional

boolean

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

See also

Introduction to Logs

Example

const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const log = logging.log('syslog');

Property

name

string

Methods

entry

entry(metadata, data) returns Entry

Create an entry object for this log.

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

Parameter

metadata

object

See a LogEntry Resource.

Value may be null.

data

(object or string)

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

See also

LogEntry JSON representation

Returns

Entry 

Example

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

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

const entry = log.entry(metadata, {
  delegate: 'my_username'
});

entry.toJSON();
// {
//   logName: 'projects/grape-spaceship-123/logs/syslog',
//   resource: {
//     type: 'gce_instance',
//     labels: {
//       zone: 'global',
//       instance_id: '3'
//     }
//   },
//   jsonPayload: {
//     delegate: 'my_username'
//   }
// }

getEntries

getEntries(query, callback) returns Promise containing GetEntriesResponse

This method is a wrapper around {module:logging#getEntries}, but with a filter specified to only return entries from this log.

Parameter

query

Optional

GetEntriesRequest

Query object for listing entries.

callback

Optional

GetEntriesCallback

Callback function.

See also

entries.list API Documentation

Returns

Promise containing GetEntriesResponse 

Example

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

log.getEntries((err, entries) => {
  // `entries` is an array of Stackdriver Logging entry objects.
  // See the `data` property to read the data from the entry.
});

//-
// To control how many API requests are made and page through the results
// manually, set `autoPaginate` to `false`.
//-
function callback(err, entries, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    log.getEntries(nextQuery, callback);
  }
}

log.getEntries({
  autoPaginate: false
}, callback);

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

getEntriesStream

getEntriesStream(query) returns ReadableStream

This method is a wrapper around {module:logging#getEntriesStream}, but with a filter specified to only return {module:logging/entry} objects from this log.

Parameter

query

Optional

GetEntriesRequest

Query object for listing entries.

Returns

ReadableStream 

A readable stream that emits Entry instances.

Example

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

log.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.
//-
log.getEntriesStream()
  .on('data', function(entry) {
    this.end();
  });