Constructor

Log

new Log(logging, name, options)

A log is a named collection of entries, each entry representing a timestamped event. Logs can be produced by Google Cloud Platform services, by third-party services, or by your applications. For example, the log apache-access is produced by the Apache Web Server, but the log compute.googleapis.com/activity_log is produced by Google Compute Engine.

Parameter

logging

Logging

Logging instance.

name

string

Name of the log.

options

Optional

object

Configuration object.

See also

Introduction to Logs

Example

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

Property

name

string

Methods

alert

alert(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "ALERT".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.alert(entry, function(err, apiResponse) {});

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

critical

critical(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "CRITICAL".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.critical(entry, function(err, apiResponse) {});

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

debug

debug(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "DEBUG".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.debug(entry, function(err, apiResponse) {});

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

delete

delete(gaxOptions, callback) returns Promise containing DeleteLogResponse

Delete the log.

Parameter

gaxOptions

Optional

object

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

callback

Optional

DeleteLogCallback

Callback function.

See also

projects.logs.delete API Documentation

Returns

Promise containing DeleteLogResponse 

Example

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

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

//-
// If the callback is omitted, we'll return a Promise.
//-
log.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 logName = 'Name of the log to delete, e.g. my-log';

const log = logging.log(logName);

// Deletes a logger and all its entries.
// Note that a deletion can take several minutes to take effect.
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=delete
log
  .delete()
  .then(() => {
    console.log(`Deleted log: ${logName}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

emergency

emergency(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "EMERGENCY".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.emergency(entry, function(err, apiResponse) {});

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

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

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

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

var 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'
//   }
// }

error

error(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "ERROR".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.error(entry, function(err, apiResponse) {});

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

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

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

log.getEntries(function(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(function(data) {
  var 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

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

log.getEntriesStream()
  .on('error', console.error)
  .on('data', function(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();
  });

info

info(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "INFO".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.info(entry, function(err, apiResponse) {});

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

notice

notice(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "NOTICE".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.notice(entry, function(err, apiResponse) {});

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

warning

warning(entry, options, callback) returns Promise containing LogWriteResponse

Write a log entry with a severity of "WARNING".

This is a simple wrapper around Log#write. All arguments are the same as documented there.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

Returns

Promise containing LogWriteResponse 

Example

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

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.warning(entry, function(err, apiResponse) {});

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

write

write(entry, options, callback) returns Promise containing LogWriteResponse

Write log entries to Stackdriver Logging.

Parameter

entry

(Entry or Array of Entry)

A log entry, or array of entries, to write.

options

Optional

WriteOptions

Write options

Value may be null.

callback

Optional

LogWriteCallback

Callback function.

See also

entries.write API Documentation

Returns

Promise containing LogWriteResponse 

Example

var entry = log.entry('gce_instance', {
  instance: 'my_instance'
});

log.write(entry, function(err, apiResponse) {
  if (!err) {
    // The log entry was written.
  }
});

//-
// You may also pass multiple log entries to write.
//-
var secondEntry = log.entry('compute.googleapis.com', {
  user: 'my_username'
});

log.write([
  entry,
  secondEntry
], function(err, apiResponse) {
  if (!err) {
    // The log entries were written.
  }
});

//-
// To save some steps, you can also pass in plain values as your entries.
// Note, however, that you must provide a configuration object to specify the
// resource.
//-
var entries = [
  {
    user: 'my_username'
  },
  {
    home: process.env.HOME
  }
];

var options = {
  resource: 'compute.googleapis.com'
};

log.write(entries, options, function(err, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
log.write(entries).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 logName = 'Name of the log to write to, e.g. my-log';

const log = logging.log(logName);

// Modify this resource to match a resource in your project
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
const resource = {
  // This example targets the "global" resource for simplicity
  type: 'global',
};

// A text log entry
const entry = log.entry({resource: resource}, 'Hello, world!');

// A structured log entry
const secondEntry = log.entry(
  {resource: resource},
  {
    name: 'King Arthur',
    quest: 'Find the Holy Grail',
    favorite_color: 'Blue',
  }
);

// Save the two log entries. You can write entries one at a time, but it is
// best to write multiple entires together in a batch.
log
  .write([entry, secondEntry])
  .then(() => {
    console.log(`Wrote to ${logName}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

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 logName = 'Name of the log to write to, e.g. my-log';
// const options = {
//   resource: {...}, // TODO(developer): Fill this in
//   entry: 'Hello, world!'
// };

const log = logging.log(logName);

// Prepare the entry
const entry = log.entry({resource: options.resource}, options.entry);

// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
log
  .write(entry)
  .then(() => {
    console.log(`Wrote to ${logName}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });