Logging
Constructor
Logging
new Logging(options)
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).
Parameter |
|
---|---|
options |
Optional Configuration options. |
- See also
Logging to Stackdriver from Bunyan">www.npmjs.com/package/@google-cloud/logging-bunyan}
Logging to Stackdriver from Winston">www.npmjs.com/package/@google-cloud/logging-winston}
Examples
Import the client library
const Logging = require('@google-cloud/logging');
Create a client that uses Application Default Credentials (ADC):
const logging = new Logging();
Create a client with explicit credentials:
const logging = new Logging({
projectId: 'your-project-id',
keyFilename: '/path/to/keyfile.json'
});
Full quickstart example:
Properties
Entry
Constructor
Entry class.
- See also
- Entry
Log
Constructor
Log class.
- See also
- Log
Logging
Constructor
Logging class.
- See also
- Logging
Sink
Constructor
Sink class.
- See also
- Sink
Methods
createSink
createSink(name, config, callback) returns Promise containing CreateSinkResponse
Create a sink.
Parameter |
|
---|---|
name |
string Name of the sink. |
config |
Config to set for the sink. |
callback |
Optional Callback function. |
- See also
- Sink#create
- Throws
-
Error
If a name is not provided.
-
Error
if a config object is not provided.
- Returns
-
Promise containing CreateSinkResponse
Example
var Storage = require('@google-cloud/storage');
var storage = new Storage({
projectId: 'grape-spaceship-123'
});
var Logging = require('@google-cloud/logging');
var logging = new Logging();
var config = {
destination: storage.bucket('logging-bucket'),
filter: 'severity = ALERT'
};
function callback(err, sink, apiResponse) {
// `sink` is a Sink object.
}
logging.createSink('new-sink-name', config, callback);
//-
// If the callback is omitted, we'll return a Promise.
//-
logging.createSink('new-sink-name', config).then(function(data) {
var sink = data[0];
var apiResponse = data[1];
});
Another example:
entry
entry(resource, data) returns Entry
Create an entry object.
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 |
|
---|---|
resource |
Optional (nullable object or nullable string) See a Monitored Resource. |
data |
(object or string) The data to use as the value for this log entry. |
- See also
- Returns
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
var resource = {
type: 'gce_instance',
labels: {
zone: 'global',
instance_id: '3'
}
};
var entry = logging.entry(resource, {
delegate: 'my_username'
});
entry.toJSON();
// {
// resource: {
// type: 'gce_instance',
// labels: {
// zone: 'global',
// instance_id: '3'
// }
// },
// jsonPayload: {
// delegate: 'my_username'
// }
// }
getEntries
getEntries(query, callback) returns Promise containing GetEntriesResponse
List the entries in your logs.
Parameter |
|
---|---|
query |
Optional Query object for listing entries. |
callback |
Optional Callback function. |
- See also
- Returns
-
Promise containing GetEntriesResponse
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
logging.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.
logging.getEntries(nextQuery, callback);
}
}
logging.getEntries({
autoPaginate: false
}, callback);
//-
// If the callback is omitted, we'll return a Promise.
//-
logging.getEntries().then(function(data) {
var entries = data[0];
});
Another example:
Another example:
getEntriesStream
getEntriesStream(query) returns ReadableStream
List the Entry objects in your logs as a readable object stream.
Parameter |
|
---|---|
query |
Optional 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();
logging.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.
//-
logging.getEntriesStream()
.on('data', function(entry) {
this.end();
});
getSinks
getSinks(query, callback) returns Promise containing GetSinksResponse
Get the sinks associated with this project.
Parameter |
|
---|---|
query |
Optional Query object for listing sinks. |
callback |
Optional Callback function. |
- See also
- Returns
-
Promise containing GetSinksResponse
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
logging.getSinks(function(err, sinks) {
// sinks is an array of Sink objects.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
logging.getSinks().then(function(data) {
var sinks = data[0];
});
Another example:
getSinksStream
getSinksStream(query) returns ReadableStream
Get the Sink objects associated with this project as a readable object stream.
Parameter |
|
---|---|
query |
Optional Query object for listing sinks. |
- Returns
-
ReadableStream
A readable stream that emits Sink instances.
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
logging.getSinksStream()
.on('error', console.error)
.on('data', function(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
log(name, options) returns Log
Get a reference to a Stackdriver Logging log.
Parameter |
|||||
---|---|---|---|---|---|
name |
string Name of the existing log. |
||||
options |
Optional object Configuration object. Values in
|
- See also
- Returns
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
var log = logging.log('my-log');
request
request(config, callback)
Funnel all API requests through this method, to be sure we have a project ID.
Parameter |
|||||||||
---|---|---|---|---|---|---|---|---|---|
config |
object Configuration object. Values in
|
||||||||
callback |
Optional function() Callback function. |
sink
sink(name) returns Sink
Get a reference to a Stackdriver Logging sink.
Parameter |
|
---|---|
name |
string Name of the existing sink. |
- See also
- Returns
Example
var Logging = require('@google-cloud/logging');
var logging = new Logging();
var sink = logging.sink('my-sink');