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).
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constsink=logging.sink('my-sink');constconfig={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=>{constsink=data[0];constapiResponse=data[1];});
Another example:
// Imports the Google Cloud client librariesconst{Logging}=require('@google-cloud/logging');const{Storage}=require('@google-cloud/storage');// Creates clientsconstlogging=newLogging();conststorage=newStorage();/*** 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.constdestination=storage.bucket(bucketName);constsink=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.*/constconfig={destination:destination,filter:filter,};asyncfunctioncreateSink(){// See https://googleapis.dev/nodejs/logging/latest/Sink.html#createawaitsink.create(config);console.log(`Created sink${sinkName}to${bucketName}`);}createSink();
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constsink=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=>{constapiResponse=data[0];});
Another example:
// Imports the Google Cloud client libraryconst{Logging}=require('@google-cloud/logging');// Creates a clientconstlogging=newLogging();/*** TODO(developer): Uncomment the following line to run the code.*/// const sinkName = 'Name of sink to delete, e.g. my-sink';constsink=logging.sink(sinkName);asyncfunctiondeleteSink(){// See https://googleapis.dev/nodejs/logging/latest/Sink.html#deleteawaitsink.delete();console.log(`Sink${sinkName}deleted.`);}deleteSink();
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constsink=logging.sink('my-sink');sink.getMetadata((err,metadata,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-sink.getMetadata().then(data=>{constmetadata=data[0];});
Another example:
// Imports the Google Cloud client libraryconst{Logging}=require('@google-cloud/logging');// Creates a clientconstlogging=newLogging();/*** TODO(developer): Uncomment the following line to run the code.*/// const sinkName = 'Name of your sink, e.g. my-sink';constsink=logging.sink(sinkName);asyncfunctionprintSinkMetadata(){// See https://googleapis.dev/nodejs/logging/latest/Sink.html#getMetadataconst[metadata]=awaitsink.getMetadata();console.log(`Name:${metadata.name}`);console.log(`Destination:${metadata.destination}`);console.log(`Filter:${metadata.filter}`);}printSinkMetadata();
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constsink=logging.sink('my-sink');constfilter='metadata.severity = ALERT';sink.setFilter(filter,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-sink.setFilter(filter).then(data=>{constapiResponse=data[0];});
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 a [Sink resource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks#LogSink).
Returns
Type
Description
Promise<SinkMetadataResponse>
{Promise
Examples
const{Logging}=require('@google-cloud/logging');constlogging=newLogging();constsink=logging.sink('my-sink');constmetadata={filter:'metadata.severity = ALERT'};sink.setMetadata(metadata,(err,apiResponse)=>{});//-// If the callback is omitted, we'll return a Promise.//-sink.setMetadata(metadata).then(data=>{constapiResponse=data[0];});
Another example:
// Imports the Google Cloud client libraryconst{Logging}=require('@google-cloud/logging');// Creates a clientconstlogging=newLogging();/*** 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';constsink=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.*/constmetadataInfo={filter:filter,};asyncfunctionupdateSink(){// See https://googleapis.dev/nodejs/logging/latest/Sink.html#setMetadataconst[metadata]=awaitsink.setMetadata(metadataInfo);console.log(`Sink${sinkName}updated.`,metadata);}updateSink();
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,[]]