演示如何创建 Cloud Logging 接收器。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
private void CreateSink(string sinkId, string logId)
{
var sinkClient = ConfigServiceV2Client.Create();
LogSink myLogSink = new LogSink();
myLogSink.Name = sinkId;
// This creates a sink using a Google Cloud Storage bucket
// named the same as the projectId.
// This requires editing the bucket's permissions to add the Entity Group
// named 'cloud-logs@google.com' with 'Owner' access for the bucket.
// If this is being run with a Google Cloud service account,
// that account will need to be granted 'Owner' access to the Project.
// In Powershell, use this command:
// PS > Add-GcsBucketAcl <your-bucket-name> -Role OWNER -Group cloud-logs@google.com
myLogSink.Destination = "storage.googleapis.com/" + s_projectId;
LogName logName = new LogName(s_projectId, logId);
myLogSink.Filter = $"logName={logName.ToString()}AND severity<=ERROR";
ProjectName projectName = new ProjectName(s_projectId);
sinkClient.CreateSink(projectName, myLogSink, _retryAWhile);
Console.WriteLine($"Created sink: {sinkId}.");
}
Go
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
ctx := context.Background()
_, err := client.CreateSink(ctx, &logadmin.Sink{
ID: "severe-errors-to-gcs",
Destination: "storage.googleapis.com/logsinks-bucket",
Filter: "severity >= ERROR",
})
Java
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName));
Sink sink = logging.create(sinkInfo);
Node.js
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
// 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();
PHP
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
/** Create a log sink.
*
* @param string $projectId The Google project ID.
* @param string $sinkName The name of the sink.
* @param string $destination The destination of the sink.
* @param string $filterString The filter for the sink.
*/
function create_sink($projectId, $sinkName, $destination, $filterString)
{
$logging = new LoggingClient(['projectId' => $projectId]);
$logging->createSink(
$sinkName,
$destination,
['filter' => $filterString]
);
printf("Created a sink '%s'." . PHP_EOL, $sinkName);
}
Python
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
def create_sink(sink_name, destination_bucket, filter_):
"""Creates a sink to export logs to the given Cloud Storage bucket.
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()
# 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.
destination = "storage.googleapis.com/{bucket}".format(bucket=destination_bucket)
sink = logging_client.sink(sink_name, filter_=filter_, destination=destination)
if sink.exists():
print("Sink {} already exists.".format(sink.name))
return
sink.create()
print("Created sink {}".format(sink.name))
Ruby
如需了解如何安装和使用 Logging 客户端库,请参阅 Logging 客户端库。
require "google/cloud/logging"
logging = Google::Cloud::Logging.new
storage = Google::Cloud::Storage.new
# bucket_name = "name-of-my-storage-bucket"
bucket = storage.create_bucket bucket_name
# Grant owner permission to Cloud Logging service
email = "cloud-logs@google.com"
bucket.acl.add_owner "group-#{email}"
# sink_name = "name-of-my-sink"
sink = logging.create_sink sink_name, "storage.googleapis.com/#{bucket.id}"
puts "#{sink.name}: #{sink.filter} -> #{sink.destination}"
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。