Update a sink

Demonstrates how to update a Cloud Logging Sink.

Code sample

C#

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

privatevoidUpdateSinkLog(stringsinkId,stringlogId)
{
varsinkClient=ConfigServiceV2Client.Create();
LogNamelogName=newLogName(s_projectId,logId);
LogSinkNamesinkName=newLogSinkName(s_projectId,sinkId);
varsink=sinkClient.GetSink(sinkName,_retryAWhile);
sink.Filter=$"logName={logName.ToString()}AND severity<=ERROR";
sinkClient.UpdateSink(sinkName,sink,_retryAWhile);
Console.WriteLine($"Updated {sinkId} to export logs from {logId}.");
}

Go

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import(
"context"
"log"
"cloud.google.com/go/logging/logadmin"
)
funcupdateSink(projectIDstring)(*logadmin.Sink,error){
ctx:=context.Background()
client,err:=logadmin.NewClient (ctx,projectID)
iferr!=nil{
log.Fatalf("logadmin.NewClient: %v",err)
}
deferclient.Close ()
sink,err:=client.UpdateSink (ctx,&logadmin.Sink{
ID:"severe-errors-to-gcs",
Destination:"storage.googleapis.com/logsinks-new-bucket",
Filter:"severity >= INFO",
})
returnsink,err
}

Java

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

SinkInfosinkInfo=
SinkInfo.newBuilder(sinkName,DatasetDestination.of(datasetName))
.setVersionFormat(SinkInfo.VersionFormat.V2)
.setFilter("severity>=ERROR")
.build();
Sinksink=logging.update(sinkInfo);

Node.js

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Imports the Google Cloud client library
const{Logging}=require('@google-cloud/logging');
// Creates a client
constlogging=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#setMetadata
const[metadata]=awaitsink.setMetadata (metadataInfo);
console.log(`Sink ${sinkName} updated.`,metadata);
}
updateSink();

PHP

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

use Google\Cloud\Logging\LoggingClient;
/**
 * Update a log sink.
 *
 * @param string $projectId
 * @param string $sinkName
 * @param string $filterString
 */
function update_sink($projectId, $sinkName, $filterString)
{
 $logging = new LoggingClient(['projectId' => $projectId]);
 $sink = $logging->sink($sinkName);
 $sink->update(['filter' => $filterString]);
 printf("Updated a sink '%s'." . PHP_EOL, $sinkName);
}

Python

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

defupdate_sink(sink_name, filter_):
"""Changes a sink's filter.
 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.
 """
 logging_client = logging.Client()
 sink = logging_client.sink(sink_name)
 sink.reload()
 sink.filter_ = filter_
 print("Updated sink {}".format(sink.name))
 sink.update()

Ruby

To learn how to install and use the client library for Logging, see Logging client libraries.

To authenticate to Logging, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

require"google/cloud/logging"
logging=Google::Cloud::Logging .new
storage=Google::Cloud::Storage .new
# bucket_name = "name-of-my-storage-bucket"
bucket=storage.create_bucketbucket_name
# sink_name = "name-of-my-sink"
sink=logging.sinksink_name
sink.destination="storage.googleapis.com/#{bucket.id}"
sink.save
puts"Updated sink destination for #{sink.name} to #{sink.destination}"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.