Send Google Cloud Logging to your own storage through Pub/Sub
Route Cloud Logging entries to a Pub/Sub topic with a log sink. A push subscription sends each LogEntry to Send2Bucket’s Pub/Sub drain, which stores the original entry in your bucket or table.
How it works
Cloud Logging ──log sink─▶ Pub/Sub topic ──push subscription─▶ /v1/events/pubsub?api-key=… ─▶ your destination
- A push subscription cannot send custom headers, so the API key and stream go in the push endpoint's query string.
- Each message carries one
LogEntry. Send2Bucket stores it as sent; Delivery format has the details. - Pub/Sub redelivers a message until it gets a
2xx, so a message Send2Bucket refuses is retried until the subscription's retention ends, unless you set a dead-letter topic.
1. Create a stream and a key
Create an ingestion stream in the dashboard, add a destination, and copy the stream's API key and ID. Use a key for this stream only: it sits in the subscription's configuration.
2. Route logs to a topic
Create a topic and a log sink that publishes to it. The filter decides which entries leave Cloud Logging; this one keeps warnings and worse:
gcloud pubsub topics create send2bucket-logs
gcloud logging sinks create send2bucket \
pubsub.googleapis.com/projects/<project_id>/topics/send2bucket-logs \
--log-filter='severity>=WARNING'
gcloud pubsub topics add-iam-policy-binding send2bucket-logs \
--member="$(gcloud logging sinks describe send2bucket --format='value(writerIdentity)')" \
--role=roles/pubsub.publisher
The last command lets the sink's writer identity publish to the topic.
3. Push to Send2Bucket
gcloud pubsub subscriptions create send2bucket-push \
--topic=send2bucket-logs \
--push-endpoint='https://api.send2bucket.com/v1/events/pubsub?api-key=<key>&stream=<stream_id>' \
--ack-deadline=60
- Both push formats work: the default wrapper, and
--push-no-wrapper, which sends the entry alone. - Authenticated push, which adds a Google token in
Authorization, is optional: theapi-keyin the URL takes precedence, and Send2Bucket does not check the token. - Add
--dead-letter-topicand--max-delivery-attemptsto stop retrying a message that keeps failing.
4. Query the logs
DuckDB reads the objects in place; the view reads this stream's folder only. For Cloud Storage, create the secret as in the DuckDB guide and use a gs:// path:
INSTALL httpfs; LOAD httpfs;
CREATE SECRET s2b (TYPE s3, PROVIDER credential_chain, REGION '<region>');
CREATE VIEW gcp_logs AS
SELECT * FROM read_json(
's3://<bucket>/<prefix>/s2b/stream=<stream_id>/date=*/hour=*/*.ndjson.gz',
format = 'newline_delimited',
hive_partitioning = true,
columns = {v: 'INTEGER', delivery_batch_id: 'VARCHAR', batch_id: 'VARCHAR',
received_at: 'TIMESTAMPTZ', signal: 'VARCHAR', data: 'JSON'})
WHERE signal = 'log';
Entries by resource type and severity:
SELECT data->>'$.resource.type' AS resource, data->>'$.severity' AS severity, count(*) AS entries
FROM gcp_logs
GROUP BY ALL
ORDER BY entries DESC;
The latest errors with their text. A LogEntry carries textPayload or a structured jsonPayload:
SELECT data->>'$.timestamp' AS logged_at,
data->>'$.resource.labels.service_name' AS service,
coalesce(data->>'$.textPayload', data->>'$.jsonPayload.message') AS message
FROM gcp_logs
WHERE data->>'$.severity' IN ('ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY')
ORDER BY logged_at DESC
LIMIT 20;
Notes
- Requests: Pub/Sub pushes one message per request, so a busy sink makes many small requests. Narrow the sink's filter to what you keep.
- Duplicates: Pub/Sub delivers at least once. Each
LogEntryhas aninsertId; deduplicate on it when it matters. - Billing: usage is the entries as stored, not the base64 push body. Pub/Sub bills its own throughput separately.