1
---
2
name: cloud-logging-cross-project-configuration
3
description: >-
4
Configure and troubleshoot Google Cloud cross-project centralized logging and read-time aggregation.
5
Use when:
6
- Setting up log routing from multiple projects/folders/organizations to a central log bucket.
7
- Creating cross-project log sinks and configuring central log buckets.
8
- Troubleshooting cross-project routing.
9
Don't use for single-project basic configurations.
10
metadata:
11
version: "1.0.2"
12
category: CloudObservabilityAndMonitoring
13
---
14
15
# Configuring Cross-Project Logging
16
17
This skill describes how to use `gcloud` commands to configure Cloud Logging so
18
that you store log data in a central location, regardless of the point of
19
origin. The skill also describes how to query log data when that data is stored
20
in multiple projects.
21
22
> [!IMPORTANT] **Sandbox Network Limitation (CRITICAL for Agent Testing):**
23
> During evaluation or in restricted sandboxed environments, network traffic to
24
> GCP APIs is blocked. Do **NOT** run network discovery commands to find
25
> resource names, project IDs, or organization IDs. Always use the exact project
26
> IDs or placeholders provided in the user prompt or instructions, for example,
27
> `{project_id}`, `{source_project_id}`, `{central_project_id}`. Assume these
28
> resources exist and proceed directly with configuration commands. Running
29
> these discovery commands will cause the execution to hang and timeout.
30
31
## Attribution
32
33
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
34
MUST read [Usage Attribution](references/attribution.md). For commands executed
35
directly in the terminal (never in generated scripts, Terraform, or user code),
36
prefix `gcloud` with
37
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:cloud-logging-cross-project-configuration)"`
38
and append `agent-skills/1.0 (skill:cloud-logging-cross-project-configuration)` to `User-Agent` headers.
39
40
## Safety and Confirmation Tiers (CRITICAL)
41
42
Before executing any commands on behalf of the user, you MUST adhere to the
43
following safety tiers based on the action requested:
44
45
1. **Tier R: Read-Only**
46
* **Description:** Commands that only read state or query logs.
47
* **Example commands:**
48
* `gcloud logging read`
49
* `gcloud logging buckets list`
50
* **Rule:** No confirmation needed. You may execute these commands
51
immediately to gather information.
52
2. **Tier M: Mutation (Non-Billing)**
53
* **Description:** Configuration modifications or free metadata creations
54
that do not incur direct storage or billing costs and do not affect
55
resource security/access policies.
56
* **Example commands:**
57
* `gcloud logging views create`
58
* `gcloud logging views update`
59
* `gcloud logging scopes create`
60
* `gcloud logging buckets create`
61
* **Rule:** No confirmation needed. You may execute these commands
62
immediately to apply configurations.
63
3. **Tier B: Billing and Security-Sensitive Mutations (High-Risk)**
64
* **Description:** Operations that create billing-inducing resources or
65
integrations, or modify security and IAM access control policies
66
(presenting a risk of privilege escalation).
67
* **Example commands:**
68
* `gcloud logging metrics create`
69
* `gcloud logging links create`
70
* `gcloud projects add-iam-policy-binding`
71
* **Rule:** **Interactive confirmation required.** These commands create
72
resources that incur billing costs or alter security access. You MUST
73
present the exact, literal command and receive user confirmation before
74
executing. NEVER execute in the same turn as asking.
75
4. **Tier D: Causes irreversible data loss**
76
* **Description:** Actions that permanently discard or delete logs, for
77
example sink exclusions.
78
* **Example commands:**
79
* `gcloud logging buckets delete`
80
* `gcloud logging sinks update --add-exclusion`
81
* **Rule:** **Explicit typed confirmation required.** These commands
82
discard or delete logs immediately and irreversibly, or they may result
83
in log data not being stored. You MUST ask for explicit typed
84
confirmation, for example, "Yes, discard logs", and halt execution until
85
the user replies.
86
87
## Decision Matrix: Centralized Storage vs. Distributed Storage with Read-Time Aggregation
88
89
Use this decision matrix to evaluate and choose between **Centralized Storage**
90
and **Read-Time Aggregation**. With centralized storage, log data is routed to
91
one log bucket, regardless of where the data originates. You write queries
92
against the centralized log bucket. With read-time aggregation, log data is
93
stored by the resource where it originates. However, a single query aggregates
94
the data by querying all resources.
95
96
After you have determined the optimal architecture for handling cross-project
97
logs, follow the corresponding configuration steps detailed below.
98
99
| Criterion | Centralized Storage | Read-Time Aggregation |
100
| :-------------------- | :----------------------- | :----------------------- |
101
| **GCP Project Scale** | Scales to thousands of | Best for < 375 projects. |
102
: : projects. : :
103
| **Log Storage** | Consolidated in a single | Resides in originating |
104
: : log bucket. : resources. :
105
| **SQL Analytics** | Easy; unified querying | Hard; requires querying |
106
: : via Observability : multiple log buckets. :
107
: : Analytics. : :
108
| **Access Control** | Scoped access via log | Requires IAM access to |
109
: : views on the centralized : all views on resources :
110
: : log bucket. : that store log data. :
111
| **Configuration** | Options vary based on | Will not interfere with |
112
: **Complexity** : Project, Folder, : bucket-based log-based :
113
: : Organization structure. : metrics. :
114
| **Cost** | Potential for duplicate | Cost-effective; no data |
115
: : storage of log buckets : replication. :
116
: : if exclusions aren't : :
117
: : set. : :
118
119
## Architecture
120
121
### Centralized Storage (Log Routing)
122
123
```mermaid
124
graph LR
125
subgraph "Source Project(s)"
126
Log[Resource Logs] --> Sink["Sink: route-to-central-project"]
127
end
128
129
subgraph "Central Project"
130
Sink --> Bucket["Bucket: central-logs-bucket (us-central1)"]
131
end
132
```
133
134
### Read-Time Aggregation (Log Scopes)
135
136
```mermaid
137
graph LR
138
subgraph "Source Project 1"
139
Log1[Resource Logs] --> Bucket1["Bucket: _Default"]
140
end
141
142
subgraph "Source Project 2"
143
Log2[Resource Logs] --> Bucket2["Bucket: _Default"]
144
end
145
146
subgraph "Scoping Project (No Log Storage)"
147
Scope["Log scope: central-query-scope"]
148
Scope -.-> View1["_AllLogs View on Bucket1"]
149
Scope -.-> View2["_AllLogs View on Bucket2"]
150
end
151
```
152
153
## Setup Steps: Centralized Storage (Log Routing)
154
155
Use these steps to route logs from one or more source projects to a central log
156
bucket in a project. Create or select the Google Cloud project that you will use
157
for storing your log data. This is the central project.
158
159
### 1. Create log bucket in central project (Tier M)
160
161
Create a custom log bucket with Log Analytics enabled.
162
163
> [Tip] Use regional log buckets, for example, set the location to
164
> `us-central1`. Don't use the `global` location. This approach ensures
165
> compatibility with Observability Analytics and SQL querying.
166
167
```bash
168
gcloud logging buckets create {bucket_id} \
169
--project={central_project_id} \
170
--location={region} \
171
--retention-days={retention_days} \
172
--enable-analytics
173
```
174
175
### 2. Create log sink in central project (Tier M)
176
177
Create a project-level sink in the central project pointing to the central log
178
bucket. This sink routes logs that land in the central project's Log Router into
179
the central log bucket.
180
181
Assuming the newly created bucket is specifically for centralizing data from
182
other projects (and the central project is not one of the source projects), the
183
central sink should exclude the central project's own logs. Include the
184
`_Required` audit log exclusions on this central sink to avoid redundancy
185
between `{bucket_id}` and the `_Required` bucket.
186
187
```bash
188
gcloud logging sinks create {sink_name} \
189
logging.googleapis.com/projects/{central_project_id}/locations/{region}/buckets/{bucket_id} \
190
--project={central_project_id} \
191
--exclusion=filter='logName:"projects/{central_project_id}/"' \
192
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/activity")' \
193
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/activity")' \
194
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/system_event")' \
195
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/system_event")' \
196
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/access_transparency")' \
197
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/access_transparency")'
198
```
199
200
### 3. Create log sink in source resource (Tier M)
201
202
To route logs to the central project, you must create a log sink in each source
203
organization, folder, or project. While you can configure a sink to route only a
204
subset of logs using the `--log-filter` argument, recommended practice is to
205
route all non-audit logs, and then restrict access or partition logs at the
206
destination using custom Log Views on the centralized log bucket.
207
208
* **For an organization-level log sink**
209
210
```bash
211
gcloud logging sinks create {sink_name} \
212
logging.googleapis.com/projects/{central_project_id} \
213
--organization={source_organization_id} \
214
--include-children \
215
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/activity")' \
216
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/activity")' \
217
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/system_event")' \
218
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/system_event")' \
219
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/access_transparency")' \
220
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/access_transparency")'
221
```
222
223
* **For project-level log sinks**
224
225
```bash
226
gcloud logging sinks create {sink_name} \
227
logging.googleapis.com/projects/{central_project_id} \
228
--project={source_project_id} \
229
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/activity")' \
230
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/activity")' \
231
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/system_event")' \
232
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/system_event")' \
233
--exclusion=filter='LOG_ID("cloudaudit.googleapis.com/access_transparency")' \
234
--exclusion=filter='LOG_ID("externalaudit.googleapis.com/access_transparency")'
235
```
236
237
### 4. Grant IAM permissions to sink writers (Tier B)
238
239
> [!IMPORTANT] **Security Action (Tier B):** Granting IAM permissions changes
240
> access control policy and must be explicitly confirmed by the user before
241
> execution.
242
243
To allow the source sinks to route logs to the central project's router, and to
244
allow the central sink to write logs to the central bucket:
245
246
1. **Grant Logs Writer permission to the source sink:** Retrieve the
247
`writerIdentity` of the source log sink and grant it
248
`roles/logging.logWriter` on the central project.
249
250
```bash
251
# Get the writer identity of the source sink
252
gcloud logging sinks describe {sink_name} \
253
--project={source_project_id} \
254
--format="value(writerIdentity)"
255
```
256
257
The output is the `{source_writer_identity}` value (for example
258
`serviceAccount:...`) for the following command:
259
260
```bash
261
# Grant Logs Writer permissions on the central project
262
gcloud projects add-iam-policy-binding {central_project_id} \
263
--member={source_writer_identity} \
264
--role=roles/logging.logWriter
265
```
266
267
2. **Grant Bucket Writer permission to the central sink:** Retrieve the
268
`writerIdentity` of the central log sink and grant it
269
`roles/logging.bucketWriter` on the central project.
270
271
```bash
272
# Get the writer identity of the central sink
273
gcloud logging sinks describe {central_sink_name} \
274
--project={central_project_id} \
275
--format="value(writerIdentity)"
276
```
277
278
The output is the `{central_writer_identity}` value for the following
279
command:
280
281
```bash
282
# Grant Bucket Writer permissions on the central project
283
gcloud projects add-iam-policy-binding {central_project_id} \
284
--member={central_writer_identity} \
285
--role=roles/logging.bucketWriter
286
```
287
288
### 5. Create custom Log Views on the central bucket (Tier M)
289
290
To partition logs or restrict access by log ID or project (since all logs were
291
routed to the same central bucket), create custom Log Views on the central
292
bucket.
293
294
* **Filter by log ID:**
295
296
```bash
297
gcloud logging views create {view_id} \
298
--bucket={bucket_id} \
299
--location={region} \
300
--project={central_project_id} \
301
--log-filter='LOG_ID("{log_id}")'
302
```
303
304
* **Filter by source project:**
305
306
```bash
307
gcloud logging views create {view_id} \
308
--bucket={bucket_id} \
309
--location={region} \
310
--project={central_project_id} \
311
--log-filter='project_id="{source_project_id}"'
312
```
313
314
### Verify Centralized Log Routing (Tier R)
315
316
To verify that logs are being routed from the source projects to the central
317
regional log bucket:
318
319
1. **Write a test log in the source project:**
320
321
```bash
322
gcloud logging write {test_log_id} "Test log entry for verification" \
323
--severity=WARNING \
324
--project={source_project_id}
325
```
326
327
2. **Read the test log from the central log bucket:** For regional log buckets,
328
you **must** specify the `--view` flag. Because the default view `_Default`
329
only contains logs matching the default filter, you should query the
330
**`_AllLogs`** view or your custom log view on the central bucket:
331
332
```bash
333
gcloud logging read 'logName:"projects/{source_project_id}/logs/{test_log_id}"' \
334
--bucket={bucket_id} \
335
--location={region} \
336
--view=_AllLogs \
337
--project={central_project_id}
338
```
339
340
--------------------------------------------------------------------------------
341
342
## Setup Steps: Read-Time Aggregation (Log Scopes)
343
344
Create or select a Google Cloud project that you will use for querying your log
345
data. This is the scoping project. Use these steps to configure a log scope. Log
346
scopes let you issue a query for log data that is stored in multiple projects.
347
348
### 1. Create a custom Log View (Optional but recommended) (Tier M)
349
350
Create a Log View in the source project to restrict which logs are accessible.
351
352
> [!IMPORTANT] **Gotcha:** Log View filters can only contain specific
353
> restrictions. Refer to
354
> https://docs.cloud.google.com/logging/docs/logs-views.md.txt#view-filter
355
356
```bash
357
gcloud logging views create {view_id} \
358
--bucket={bucket_id} \
359
--location={region} \
360
--project={source_project_id} \
361
--log-filter='LOG_ID("{log_id}")'
362
```
363
364
* `{bucket_id}`: for example, `_Default`
365
* `{region}`: for example, `global`
366
* `{view_id}`: for example, `app-logs-view`
367
* If you want to allow access to all logs in the log bucket, omit the
368
`--log-filter` flag.
369
370
### 2. Create a log scope in the scoping project (Tier M)
371
372
Create a log scope in the scoping project listing the source resources, which
373
may be projects or specific log views.
374
375
```bash
376
gcloud logging scopes create {log_scope_id} \
377
--project={scoping_project_id} \
378
--resource-names={resource_names}
379
```
380
381
* `{log_scope_id}`: for example, `central-query-scope`
382
* `{resource_names}`: Comma-separated list of log views. For example,
383
`projects/source-project-1/locations/global/buckets/_Default/views/app-logs-view,projects/source-project-2/locations/global/buckets/_Default/views/app-logs-view`.
384
You can include up to 100 views in the scope.
385
386
### 3. Update Default Observability Scope (Optional) (Tier M)
387
388
Link the log scope to the project's default observability scope so it is used by
389
default in Logs Explorer.
390
391
```bash
392
gcloud observability scopes update _Default \
393
--project={scoping_project_id} \
394
--location=global \
395
--log-scope=//logging.googleapis.com/projects/{scoping_project_id}/locations/global/logScopes/{log_scope_id}
396
```
397
398
### 4. Grant IAM permissions to users (Tier B)
399
400
Unlike centralized routing, permissions are checked at query time on all source
401
projects. Users running queries must have:
402
403
* `roles/logging.viewAccessor` granted on the specific Log View (with IAM
404
conditions) or `roles/logging.viewer` on the source projects.
405
* Access to the scoping project.
406
407
## Troubleshooting Cross-Project Log Routing and Sink Permission Failures
408
409
If logs are not appearing in a central project's bucket after configuring
410
centralized logging:
411
412
### 1. Verify Log Router Sink Filters in Source Resource
413
414
* Ensure that the log sink's filter matches the logs you expect to route.
415
416
> [!IMPORTANT] **Gotcha:** Standard filter expressions like `logName:abc` or
417
> `logName="projects/{project_id}/logs/abc"` can fail to match in log sinks.
418
> **Always** use `LOG_ID("abc")` for precise matching in log sink filters.
419
420
* Verify that you have not configured an exclusion filter that accidentally
421
discards these logs.
422
423
### 2. Verify and Grant Writer Identity Permissions (Most Common Cause)
424
425
The log sink's writer identity service account in the source project must be
426
explicitly granted the necessary permissions on the central resource.
427
428
* **Step A: Get the Writer Identity**
429
430
```bash
431
gcloud logging sinks describe {sink_name} \
432
--project={source_project_id} \
433
--format="value(writerIdentity)"
434
```
435
436
* **Step B: Grant the Permission in the Central Project (Tier B)**
437
438
> [!IMPORTANT] **Security Action (Tier B):** Granting IAM permissions
439
> changes access control policy and must be explicitly confirmed by the user
440
> before execution.
441
442
* **For Cloud Logging Buckets (standard):** Grant
443
`roles/logging.bucketWriter`:
444
445
```bash
446
gcloud projects add-iam-policy-binding {central_project_id} \
447
--member={writer_identity} \
448
--role=roles/logging.bucketWriter
449
```
450
451
* **For GCS Buckets:** Grant `roles/storage.objectCreator` on the GCS
452
bucket.
453
454
* **For Pub/Sub Topics:** Grant `roles/pubsub.publisher` on the Pub/Sub
455
topic.
456
457
* **For BigQuery Datasets:** Grant `roles/bigquery.dataEditor` on the
458
BigQuery dataset.
459
460
--------------------------------------------------------------------------------
461
462
## References and Supporting Links
463
464
* [GCP Cloud Logging - Routing and Storage Overview](https://docs.cloud.google.com/logging/docs/routing/overview.md.txt)
465
* [GCP Cloud Logging - Log Scopes](https://docs.cloud.google.com/logging/docs/log-scope/create-and-manage.md.txt)
466
* [GCP Cloud Logging - Custom Log Views](https://docs.cloud.google.com/logging/docs/logs-views.md.txt)
467