Logging
Source: index.
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).
new Logging([options])
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'
});
include:samples/quickstart.js
region_tag:logging_quickstart
Full quickstart example:
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
options |
|
Yes |
Configuration options. |
Methods
entry([resource], data) → 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.
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'
// }
// }
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
resource |
(nullable object or nullable string) |
Yes |
See a Monitored Resource. |
|
data |
(object or string) |
|
The data to use as the value for this log entry. |
- See also
- Returns
getEntriesStream([query]) → ReadableStream
List the Entry objects in your logs as a readable object stream.
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();
});
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
query |
GetEntriesRequest |
Yes |
Query object for listing entries. |
- Returns
-
ReadableStreamA readable stream that emits Entry instances.
getSinksStream([query]) → ReadableStream
Get the Sink objects associated with this project as a readable object stream.
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();
});
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
query |
GetSinksRequest |
Yes |
Query object for listing sinks. |
- Returns
-
ReadableStreamA readable stream that emits Sink instances.
log(name[, options]) → Log
Get a reference to a Stackdriver Logging log.
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const log = logging.log('my-log');
Parameters
| Name | Type | Optional | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
name |
string |
|
Name of the existing log. |
||||||||
|
options |
object |
Yes |
Configuration object. Values in
|
- See also
- Returns
request(config[, callback])
Funnel all API requests through this method, to be sure we have a project ID.
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
config |
object |
|
Configuration object. Values in
|
||||||||||||||||
|
callback |
function() |
Yes |
Callback function. |
sink(name) → Sink
Get a reference to a Stackdriver Logging sink.
Example
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
name |
string |
|
Name of the existing sink. |
- See also
- Returns