A sink is an object that lets you to specify a set of log entries to export to a particular destination. Cloud Logging lets you export log entries to destinations including Cloud Storage buckets (for long term log storage), Google BigQuery datasets (for log analysis), Google Pub/Sub (for streaming to other applications).
Package
@google-cloud/loggingExample
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
Constructors
(constructor)(logging, name)
constructor(logging: Logging, name: string);
Constructs a new instance of the Sink
class
Parameters | |
---|---|
Name | Description |
logging |
Logging
|
name |
string
|
Properties
formattedName_
formattedName_: string;
logging
logging: Logging;
metadata
metadata?: LogSink;
name
name: string;
Methods
create(config)
create(config: CreateSinkRequest): Promise<[Sink, LogSink]>;
Create a sink.
Parameter | |
---|---|
Name | Description |
config |
CreateSinkRequest
Config to set for the sink. |
Returns | |
---|---|
Type | Description |
Promise<[Sink, LogSink]> | {Promise
|
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
const config = {
destination: {
// ...
}
};
sink.create(config, (err, sink, apiResponse) => {
if (!err) {
// The sink was created successfully.
}
});
//-
// If the callback is omitted, we'll return a Promise.
//-
sink.create(config).then(data => {
const sink = data[0];
const apiResponse = data[1];
});
Another example:
// 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();
create(config, callback)
create(config: CreateSinkRequest, callback: CreateSinkCallback): void;
Parameters | |
---|---|
Name | Description |
config |
CreateSinkRequest
|
callback |
CreateSinkCallback
|
Returns | |
---|---|
Type | Description |
void |
delete(gaxOptions)
delete(gaxOptions?: CallOptions): Promise<DeleteResponse>;
Delete the sink.
Parameter | |
---|---|
Name | Description |
gaxOptions |
CallOptions
Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions. |
Returns | |
---|---|
Type | Description |
Promise<DeleteResponse> | {Promise
|
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
sink.delete((err, apiResponse) => {
if (!err) {
// The log was deleted.
}
});
//-
// If the callback is omitted, we'll return a Promise.
//-
sink.delete().then(data => {
const 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 sinkName = 'Name of sink to delete, e.g. my-sink';
const sink = logging.sink(sinkName);
async function deleteSink() {
// See https://googleapis.dev/nodejs/logging/latest/Sink.html#delete
await sink.delete();
console.log(`Sink ${sinkName} deleted.`);
}
deleteSink();
delete(callback)
delete(callback: DeleteCallback): void;
Parameter | |
---|---|
Name | Description |
callback |
DeleteCallback
|
Returns | |
---|---|
Type | Description |
void |
delete(gaxOptions, callback)
delete(gaxOptions: CallOptions, callback: DeleteCallback): void;
Parameters | |
---|---|
Name | Description |
gaxOptions |
CallOptions
|
callback |
DeleteCallback
|
Returns | |
---|---|
Type | Description |
void |
getMetadata(gaxOptions)
getMetadata(gaxOptions?: CallOptions): Promise<SinkMetadataResponse>;
Get the sink's metadata.
Parameter | |
---|---|
Name | Description |
gaxOptions |
CallOptions
Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions. |
Returns | |
---|---|
Type | Description |
Promise<SinkMetadataResponse> | {Promise
|
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
sink.getMetadata((err, metadata, apiResponse) => {});
//-
// If the callback is omitted, we'll return a Promise.
//-
sink.getMetadata().then(data => {
const metadata = 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 sinkName = 'Name of your sink, e.g. my-sink';
const sink = logging.sink(sinkName);
async function printSinkMetadata() {
// See https://googleapis.dev/nodejs/logging/latest/Sink.html#getMetadata
const [metadata] = await sink.getMetadata();
console.log(`Name: ${metadata.name}`);
console.log(`Destination: ${metadata.destination}`);
console.log(`Filter: ${metadata.filter}`);
}
printSinkMetadata();
getMetadata(callback)
getMetadata(callback: SinkMetadataCallback): void;
Parameter | |
---|---|
Name | Description |
callback |
SinkMetadataCallback
|
Returns | |
---|---|
Type | Description |
void |
getMetadata(gaxOptions, callback)
getMetadata(gaxOptions: CallOptions, callback: SinkMetadataCallback): void;
Parameters | |
---|---|
Name | Description |
gaxOptions |
CallOptions
|
callback |
SinkMetadataCallback
|
Returns | |
---|---|
Type | Description |
void |
setFilter(filter)
setFilter(filter: string): Promise<SinkMetadataResponse>;
Set the sink's filter.
This will override any filter that was previously set.
Parameter | |
---|---|
Name | Description |
filter |
string
The new filter. |
Returns | |
---|---|
Type | Description |
Promise<SinkMetadataResponse> | {Promise
|
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
const filter = 'metadata.severity = ALERT';
sink.setFilter(filter, (err, apiResponse) => {});
//-
// If the callback is omitted, we'll return a Promise.
//-
sink.setFilter(filter).then(data => {
const apiResponse = data[0];
});
setFilter(filter, callback)
setFilter(filter: string, callback: SinkMetadataCallback): void;
Parameters | |
---|---|
Name | Description |
filter |
string
|
callback |
SinkMetadataCallback
|
Returns | |
---|---|
Type | Description |
void |
setMetadata(metadata)
setMetadata(metadata: SetSinkMetadata): Promise<SinkMetadataResponse>;
Set the sink's metadata.
Note: If the sink was previously created or updated with uniqueWriterIdentity = true, then you must update the sink by setting uniqueWriterIdentity = true. Read more about using a unique writer identity here: https://cloud.google.com/logging/docs/api/tasks/exporting-logs#using_a_unique_writer_identity
See Sink Resource See projects.sink.update API Documentation
Parameter | |
---|---|
Name | Description |
metadata |
SetSinkMetadata
See a [Sink resource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks#LogSink). |
Returns | |
---|---|
Type | Description |
Promise<SinkMetadataResponse> | {Promise
|
const {Logging} = require('@google-cloud/logging');
const logging = new Logging();
const sink = logging.sink('my-sink');
const metadata = {
filter: 'metadata.severity = ALERT'
};
sink.setMetadata(metadata, (err, apiResponse) => {});
//-
// If the callback is omitted, we'll return a Promise.
//-
sink.setMetadata(metadata).then(data => {
const 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 lines to run the code.
*/
// const sinkName = 'Name of sink to update, e.g. my-sink';
// const filter = 'New filter for the sink, e.g. severity >= WARNING';
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 metadataInfo = {
filter: filter,
};
async function updateSink() {
// See https://googleapis.dev/nodejs/logging/latest/Sink.html#setMetadata
const [metadata] = await sink.setMetadata(metadataInfo);
console.log(`Sink ${sinkName} updated.`, metadata);
}
updateSink();
setMetadata(metadata, callback)
setMetadata(metadata: SetSinkMetadata, callback: SinkMetadataCallback): void;
Parameters | |
---|---|
Name | Description |
metadata |
SetSinkMetadata
|
callback |
SinkMetadataCallback
|
Returns | |
---|---|
Type | Description |
void |