Create a sink
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to create a Cloud Logging Sink.
Code sample
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.
[[["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"]],[],[],[],null,["Demonstrates how to create a Cloud Logging Sink.\n\nCode sample \n\nC#\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n private void CreateSink(string sinkId, string logId)\n {\n var sinkClient = ConfigServiceV2Client.Create();\n LogSink myLogSink = new LogSink();\n myLogSink.Name = sinkId;\n\n // This creates a sink using a Google Cloud Storage bucket \n // named the same as the projectId.\n // This requires editing the bucket's permissions to add the Entity Group \n // named 'cloud-logs@google.com' with 'Owner' access for the bucket.\n // If this is being run with a Google Cloud service account,\n // that account will need to be granted 'Owner' access to the Project.\n // In Powershell, use this command:\n // PS \u003e Add-GcsBucketAcl \u003cyour-bucket-name\u003e -Role OWNER -Group cloud-logs@google.com\n myLogSink.Destination = \"storage.googleapis.com/\" + s_projectId;\n LogName logName = new LogName(s_projectId, logId);\n myLogSink.Filter = $\"logName={logName.ToString()}AND severity\u003c=ERROR\";\n ProjectName projectName = new ProjectName(s_projectId);\n sinkClient.CreateSink(projectName, myLogSink, _retryAWhile);\n Console.WriteLine($\"Created sink: {sinkId}.\");\n }\n\nGo\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"log\"\n\n \t\"cloud.google.com/go/logging/logadmin\"\n )\n\n func createSink(projectID string) (*logadmin.Sink, error) {\n \tctx := context.Background()\n \tclient, err := logadmin.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/logadmin.html#cloud_google_com_go_logging_logadmin_Client_NewClient(ctx, projectID)\n \tif err != nil {\n \t\tlog.Fatalf(\"logadmin.NewClient: %v\", err)\n \t}\n \tdefer client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/logadmin.html#cloud_google_com_go_logging_logadmin_Client_Close()\n \tsink, err := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/logging/latest/logadmin.html#cloud_google_com_go_logging_logadmin_Client_CreateSink(ctx, &logadmin.Sink{\n \t\tID: \"severe-errors-to-gcs\",\n \t\tDestination: \"storage.googleapis.com/logsinks-bucket\",\n \t\tFilter: \"severity \u003e= ERROR\",\n \t})\n \treturn sink, err\n }\n\nJava\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName));\n Sink sink = logging.create(sinkInfo);\n\nNode.js\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client libraries\n const {Logging} = require('https://cloud.google.com/nodejs/docs/reference/logging/latest/overview.html');\n const {Storage} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates clients\n const logging = new https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/logging.html();\n const storage = new Storage();\n\n /**\n * TODO(developer): Uncomment the following lines to run the code.\n */\n // const sinkName = 'Name of your sink, e.g. my-sink';\n // const bucketName = 'Desination bucket, e.g. my-bucket';\n // const filter = 'Optional log filer, e.g. severity=ERROR';\n\n // The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic,\n // or a BigQuery dataset. In this case, it is a Cloud Storage Bucket.\n // See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for\n // information on the destination format.\n const destination = storage.bucket(bucketName);\n const sink = logging.sink(sinkName);\n\n /**\n * The filter determines which logs this sink matches and will be exported\n * to the destination. For example a filter of 'severity\u003e=INFO' will send\n * all logs that have a severity of INFO or greater to the destination.\n * See https://cloud.google.com/logging/docs/view/advanced_filters for more\n * filter information.\n */\n const config = {\n destination: destination,\n filter: filter,\n };\n\n async function createSink() {\n // See https://googleapis.dev/nodejs/logging/latest/Sink.html#create\n await sink.create(config);\n console.log(`Created sink ${sinkName} to ${bucketName}`);\n }\n createSink();\n\nPHP\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Logging\\LoggingClient;\n\n /**\n * Create a log sink.\n *\n * @param string $projectId The Google project ID.\n * @param string $sinkName The name of the sink.\n * @param string $destination The destination of the sink.\n * @param string $filterString The filter for the sink.\n */\n function create_sink($projectId, $sinkName, $destination, $filterString)\n {\n $logging = new LoggingClient(['projectId' =\u003e $projectId]);\n $logging-\u003ecreateSink(\n $sinkName,\n $destination,\n ['filter' =\u003e $filterString]\n );\n printf(\"Created a sink '%s'.\" . PHP_EOL, $sinkName);\n }\n\nPython\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def create_sink(sink_name, destination_bucket, filter_):\n \"\"\"Creates a sink to export logs to the given Cloud Storage bucket.\n\n The filter determines which logs this sink matches and will be exported\n to the destination. For example a filter of 'severity\u003e=INFO' will send\n all logs that have a severity of INFO or greater to the destination.\n See https://cloud.google.com/logging/docs/view/advanced_filters for more\n filter information.\n \"\"\"\n logging_client = logging.Client()\n\n # The destination can be a Cloud Storage bucket, a Cloud Pub/Sub topic,\n # or a BigQuery dataset. In this case, it is a Cloud Storage Bucket.\n # See https://cloud.google.com/logging/docs/api/tasks/exporting-logs for\n # information on the destination format.\n destination = \"storage.googleapis.com/{bucket}\".format(bucket=destination_bucket)\n\n sink = logging_client.sink(sink_name, filter_=filter_, destination=destination)\n\n if sink.exists():\n print(\"Sink {} already exists.\".format(sink.name))\n return\n\n sink.create()\n print(\"Created sink {}\".format(sink.name))\n\nRuby\n\n\nTo learn how to install and use the client library for Logging, see\n[Logging client libraries](/logging/docs/reference/libraries).\n\n\nTo authenticate to Logging, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"google/cloud/logging\"\n\n logging = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-logging/latest/Google-Cloud-Logging.html.new\n storage = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html.new\n # bucket_name = \"name-of-my-storage-bucket\"\n bucket = storage.create_bucket bucket_name\n\n # Grant owner permission to Cloud Logging service\n email = \"cloud-logs@google.com\"\n bucket.acl.add_owner \"group-#{email}\"\n\n # sink_name = \"name-of-my-sink\"\n sink = logging.create_sink sink_name, \"storage.googleapis.com/#{bucket.id}\"\n puts \"#{sink.name}: #{sink.filter} -\u003e #{sink.destination}\"\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=logging)."]]