Constructor
new Logging(optionsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
ClientConfig |
<optional> |
Configuration options. |
Examples
Import the client library
```
const {Logging} = require('@google-cloud/logging');
```
Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:
```
const logging = new Logging();
```
Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicitcredentials</a>:
```
const logging = new Logging({ projectId:
'your-project-id', keyFilename: '/path/to/keyfile.json'
});
```
// 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'},
// See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
severity: 'INFO',
};
// 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
(async) createSink(name, config, callbackopt) → {Promise.<CreateSinkResponse>}
Create a sink.
See Sink Overview See Advanced Logs Filters See projects.sinks.create API Documentation
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string |
Name of the sink. |
|
config |
CreateSinkRequest |
Config to set for the sink. |
|
callback |
CreateSinkCallback |
<optional> |
Callback function. |
Returns:
Type | Description |
---|---|
Promise.<CreateSinkResponse> |
- See:
-
- Sink#create
Throws:
-
-
If a name is not provided.
- Type
- Error
-
-
-
if a config object is not provided.
- Type
- Error
-
Examples
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
projectId: 'grape-spaceship-123'
});
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const 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(data => {
const sink = data[0];
const apiResponse = data[1];
});
```
// Imports the Google Cloud client libraries
const {Logging} = require('@google-cloud/logging');
const {Storage} = require('@google-cloud/storage');
// Creates clients
const logging = new Logging();
const storage = new Storage();
/**
* TODO(developer): Uncomment the following lines to run the code.
*/
// const sinkName = 'Name of your sink, e.g. my-sink';
// const bucketName = 'Desination bucket, e.g. my-bucket';
// const filter = 'Optional log filer, e.g. severity=ERROR';
// The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic,
// or a BigQuery dataset. In this case, it is a Cloud Storage Bucket.
// See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for
// information on the destination format.
const destination = storage.bucket(bucketName);
const sink = logging.sink(sinkName);
/**
* The filter determines which logs this sink matches and will be exported
* to the destination. For example a filter of 'severity>=INFO' will send
* all logs that have a severity of INFO or greater to the destination.
* See https://cloud.google.com/logging/docs/view/advanced_filters for more
* filter information.
*/
const config = {
destination: destination,
filter: filter,
};
async function createSink() {
// See https://googleapis.dev/nodejs/logging/latest/Sink.html#create
await sink.create(config);
console.log(`Created sink ${sinkName} to ${bucketName}`);
}
createSink();
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. |
Returns:
Type | Description |
---|---|
Entry |
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. |
Returns:
Type | Description |
---|---|
ReadableStream |
A readable stream that emits Entry instances. |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
logging.getEntriesStream()
.on('error', console.error)
.on('data', entry => {
// `entry` is a Cloud 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. |
Returns:
Type | Description |
---|---|
ReadableStream |
A readable stream that emits Log instances. |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
logging.getLogsStream()
.on('error', console.error)
.on('data', log => {
// `log` is a Cloud 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. |
Returns:
Type | Description |
---|---|
ReadableStream |
A readable stream that emits Sink instances. |
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 Cloud Logging log.
See Log Overview
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
name |
string |
Name of the existing log. |
|||||||||
options |
object |
<optional> |
Configuration object. Properties
|
Returns:
Type | Description |
---|---|
Log |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const log = logging.log('my-log');
```
logSync(name, transport, options) → {LogSync}
Get a reference to a Cloud Logging logSync.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Name of the existing log. |
transport |
object |
An optional write stream. |
options |
LogSyncOptions |
An optional configuration object. |
Returns:
Type | Description |
---|---|
LogSync |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
// Optional: enrich logs with additional context
await logging.setProjectId();
await logging.setDetectedResource();
// Default transport writes to process.stdout
const log = logging.logSync('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
|
|||||||||||||
callback |
function |
<optional> |
Callback function. |
(async) setDetectedResource()
setResource detects and sets a detectedresource object on the Logging instance. It can be invoked once to ensure ensuing LogSync entries contain resource context.
(async) setProjectId(reqOpts)
setProjectId detects and sets a projectId string on the Logging instance. It can be invoked once to ensure ensuing LogSync entries have a projectID.
Parameters:
Name | Type | Description |
---|---|---|
reqOpts |
sink(name) → {Sink}
Get a reference to a Cloud Logging sink.
See Sink Overview
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
Name of the existing sink. |
Returns:
Type | Description |
---|---|
Sink |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
```
tailEntries(queryopt) → {DuplexStream}
Streaming read of live logs as log entries are ingested. Until the stream is terminated, it will continue reading logs.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
TailEntriesRequest |
<optional> |
Query object for tailing entries. |
Returns:
Type | Description |
---|---|
DuplexStream |
A duplex stream that emits TailEntriesResponses containing an array of Entry instances. |
Example
```
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
logging.tailEntries()
.on('error', console.error)
.on('data', resp => {
console.log(resp.entries);
console.log(resp.suppressionInfo);
})
.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();
});
```