1
---
2
name: cloud-logging-configuration-basics
3
description: >-
4
Configure single-project Cloud Logging on Google Cloud: regional log buckets, log sinks, log views, restricting or hiding sensitive logs in the default view (_Default) filter,
5
IAM permissions for views (Logs View Accessor, IAM conditions), logs-based metrics, log exclusions, and sampling.
6
Don't use for cross-project logging or multi-project setups.
7
metadata:
8
version: "1.0.3"
9
category: CloudObservabilityAndMonitoring
10
---
11
12
# Configuring Cloud Logging
13
14
Use this skill to configure Cloud Logging resources such as log buckets, log
15
views, or log sinks.
16
17
> [!IMPORTANT] **Sandbox Network Limitation (CRITICAL for Agent Testing):**
18
> During evaluation or in restricted sandboxed environments, network traffic to
19
> Google Cloud APIs is blocked. Do **NOT** run network discovery commands to
20
> find resource names, project IDs, or organization IDs. Always use the exact
21
> project IDs or placeholders provided in the user prompt or instructions for
22
> example, `{project_id}`. Assume these resources exist and proceed directly
23
> with configuration commands. Running these discovery commands will cause the
24
> execution to hang and timeout.
25
26
## Attribution
27
28
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
29
MUST read [Usage Attribution](references/attribution.md). For commands executed
30
directly in the terminal (never in generated scripts, Terraform, or user code),
31
prefix `gcloud` with
32
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:cloud-logging-configuration-basics)"`
33
and append `agent-skills/1.0 (skill:cloud-logging-configuration-basics)` to `User-Agent` headers.
34
35
## Safety and Confirmation Tiers (CRITICAL)
36
37
Before executing any commands on behalf of the user, you MUST adhere to the
38
following safety tiers based on the action requested:
39
40
1. **Tier R: Read-Only**
41
* **Description:** Commands that only read state or query logs.
42
* **Example commands:**
43
* `gcloud logging read`
44
* `gcloud logging buckets list`
45
* **Rule:** No confirmation needed. You may execute these commands
46
immediately to gather information.
47
2. **Tier M: Mutation (Non-Billing)**
48
* **Description:** Configuration modifications or free metadata creations
49
that do not incur direct storage or billing costs and do not affect
50
resource security/access policies.
51
* **Example commands:**
52
* `gcloud logging views create`
53
* `gcloud logging views update`
54
* `gcloud logging scopes create`
55
* `gcloud logging buckets create`
56
* **Rule:** No confirmation needed. You may execute these commands
57
immediately to apply configurations.
58
3. **Tier B: Billing and Security-Sensitive Mutations (High-Risk)**
59
* **Description:** Operations that create billing-inducing resources or
60
integrations, or modify security and IAM access control policies
61
(presenting a risk of privilege escalation).
62
* **Example commands:**
63
* `gcloud logging metrics create`
64
* `gcloud logging links create`
65
* `gcloud projects add-iam-policy-binding`
66
* **Rule:** **Interactive confirmation required.** These commands create
67
resources that incur billing costs or alter security access. You MUST
68
present the exact, literal command and receive user confirmation before
69
executing. NEVER execute in the same turn as asking.
70
4. **Tier D: Causes irreversible data loss**
71
* **Description:** Actions that permanently discard or delete logs, for
72
example sink exclusions.
73
* **Example commands:**
74
* `gcloud logging buckets delete`
75
* `gcloud logging sinks update --add-exclusion`
76
* **Rule:** **Explicit typed confirmation required.** These commands
77
discard or delete logs immediately and irreversibly, or they may result
78
in log data not being stored. You MUST ask for explicit typed
79
confirmation, for example, "Yes, discard logs", and halt execution until
80
the user replies.
81
82
## Getting Started
83
84
If the `gcloud` executable is missing, refer to the
85
[Google Cloud CLI Installation Guide](https://docs.cloud.google.com/sdk/docs/install-sdk.md.txt)
86
to install it.
87
88
## Creating Log Buckets (Compliance and Analytics) (Tier M)
89
90
To create a regional log bucket with a specific retention policy for regulatory
91
compliance, and with Observability Analytics enabled:
92
93
> [!WARNING] **Mandatory Observability Analytics Downgrade Warning:** Whenever
94
> providing guidance, writing a guide, or drafting commands on Cloud Logging
95
> cost optimization or exclusions, you **must** explicitly include the following
96
> warning in your final text response and any generated guides: "After a log
97
> bucket has been upgraded to use Observability Analytics, it **cannot be
98
> downgraded** to remove the analytics capability."
99
100
```bash
101
gcloud logging buckets create {bucket_id} \
102
--project={project_id} \
103
--location={region} \
104
--retention-days={retention_days} \
105
--enable-analytics
106
```
107
108
* `{bucket_id}`: for example, `my-custom-bucket`
109
* `{region}`: for example, `global` or `us-central1`. Observability Analytics
110
is supported in both global and regional log buckets.
111
* `{retention_days}`: for example, `365`
112
113
A log bucket incurs no storage or ingestion charges until logs are routed to it
114
with a log sink.
115
116
### Verify the Log Bucket (Tier R)
117
118
Check the log bucket's configuration to verify its compliance:
119
120
```bash
121
gcloud logging buckets describe {bucket_id} \
122
--location={region} \
123
--project={project_id}
124
```
125
126
### Route logs to the Log Bucket (Tier B)
127
128
> [!IMPORTANT] **Billing Action (Tier B):** Routing log entries to a bucket
129
> incurs ongoing charges based on the volume of data stored. You MUST get
130
> interactive user confirmation before running this command.
131
132
Log entries are stored in the log bucket only if a log sink filter matches the
133
entries and targets that bucket.
134
135
To route log entries to the log bucket:
136
137
```bash
138
gcloud logging sinks create {sink_id} \
139
projects/{project_id}/locations/{region}/buckets/{bucket_id} \
140
--log-filter='{filter_expression}' \
141
--project={project_id}
142
```
143
144
--------------------------------------------------------------------------------
145
146
## Logs-Based Metrics
147
148
Logs-based metrics count the number of log entries that match a filter, allowing
149
you to track error rates and set up alerting policies.
150
151
### 1. Create a logs-based counter metric (Tier B)
152
153
> [!IMPORTANT] **Billing Action (Tier B):** Creating logs-based metrics incurs
154
> ongoing charges based on the volume of data points reported. You MUST get
155
> interactive user confirmation before running this command.
156
157
To count the occurrences of a specific log pattern, for example, "OutOfMemory"
158
errors:
159
160
```bash
161
gcloud logging metrics create {metric_name} \
162
--log-filter='{filter_expression}' \
163
--description='{description}' \
164
--project={project_id}
165
```
166
167
* `{metric_name}`: for example, `oom_error_count`
168
* `{filter_expression}`: for example, `textPayload:"OutOfMemory"`
169
* `{description}`: for example, "Count of log entries about OOMs"
170
171
Refer to
172
[REST Resource: projects.metric](https://docs.cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics.md.txt#LogMetric)
173
for restrictions on the metric fields.
174
175
### 2. Verify the logs-based metric (Tier R)
176
177
To verify that the metric exists and inspect its configuration, use the
178
`describe` command:
179
180
```bash
181
gcloud logging metrics describe {metric_name} \
182
--project={project_id}
183
```
184
185
--------------------------------------------------------------------------------
186
187
## Restricting Access to Sensitive Logs (Security)
188
189
Anyone with `roles/logging.viewer` on that project can see logs in a project's
190
`_Default` log bucket via `_Default` log view. To restrict visibility of the
191
logs:
192
193
> [!IMPORTANT] **Ambiguity Handling (Guidance for Agents):** If the user asks to
194
> "exclude", "hide", or "remove" sensitive logs without explicitly specifying
195
> whether they want to stop storing them, you **MUST** default to **excluding
196
> them from the default view (Step 1)**. This is a safe, non-destructive Tier M
197
> action. Only configure a storage exclusion (under the "Discarding Sensitive
198
> Logs from Storage" section) if the user explicitly uses destructive terms like
199
> *"stop storing"*, *"permanently discard"*, or *"sink exclusion"*.
200
201
### 1. Exclude sensitive logs from default view (Tier M)
202
203
To explicitly exclude sensitive logs from general access, update the filter for
204
the `_Default` log view:
205
206
```bash
207
gcloud logging views update _Default \
208
--bucket=_Default \
209
--location=global \
210
--project={project_id} \
211
--log-filter='NOT LOG_ID("cloudaudit.googleapis.com/data_access") AND NOT LOG_ID("externalaudit.googleapis.com/data_access") AND NOT LOG_ID("{sensitive_log_id}")'
212
```
213
214
### 2. Create a log view (Tier M)
215
216
Create a new log view that includes the sensitive logs in the project's
217
`_Default` log bucket. For example, a "security-logs-view" with access to the
218
`{sensitive_log_id}`
219
220
```bash
221
gcloud logging views create security-logs-view \
222
--bucket=_Default \
223
--location=global \
224
--project={project_id} \
225
--log-filter='LOG_ID("{sensitive_log_id}")' \
226
--description="Sensitive logs"
227
```
228
229
### 3. Grant access to log view using IAM conditions (Tier B)
230
231
> [!IMPORTANT] **Security Action (Tier B):** Granting IAM permissions changes
232
> access control policy and must be explicitly confirmed by the user before
233
> execution.
234
235
To restrict access to log view use IAM. When granting the Logs Viewer Accessor
236
role, always attach an IAM condition that restricts the grant to a specific log
237
view. For example, to grant `{security_group_email}` access ONLY to the
238
`security-logs-view` in the `_Default` bucket:
239
240
```bash
241
gcloud projects add-iam-policy-binding {project_id} \
242
--member='group:{security_group_email}' \
243
--role='roles/logging.viewAccessor' \
244
--condition="expression=resource.name=='projects/{project_id}/locations/global/buckets/_Default/views/security-logs-view',title=Restricted to Specific Log View,description=Only allows access to the specified log view"
245
```
246
247
Replace `{location}` with the location of the log bucket, for example `global`
248
or a regional location like `us-central1`.
249
250
### 4. Verify Sensitive Log Restrictions (Tier R)
251
252
To verify that your Log View for sensitive logs is configured correctly:
253
254
```bash
255
gcloud logging views describe {view_id} \
256
--bucket={bucket_id} \
257
--location={region} \
258
--project={project_id}
259
```
260
261
Ensure that the `filter` block contains the appropriate restriction expression.
262
263
--------------------------------------------------------------------------------
264
265
## Discarding Sensitive Logs from Storage (Tier D)
266
267
If your organization's compliance policies prohibit storing sensitive logs at
268
all, you can configure an exclusion to discard them before they are written to
269
disk.
270
271
> [!CAUTION] **Destructive Action (Tier D):** Excluding logs from all log sinks
272
> deletes the log entries immediately and irreversibly.
273
>
274
> **Safety Rule:** You MUST ask the user for explicit typed confirmation, for
275
> example, "I confirm I want to exclude `{sensitive_log_id}` logs from storage",
276
> before running this command. **Same-Turn Restriction:** Do NOT execute the
277
> `gcloud logging sinks update` command in the same turn as asking for
278
> confirmation. Stop tool execution immediately and wait for the user to reply.
279
280
**Exclude sensitive logs from storage using sink exclusions**
281
282
```bash
283
gcloud logging sinks update _Default \
284
--project={project_id} \
285
--add-exclusion=name=exclude-sensitive,filter='LOG_ID("{sensitive_log_id}")'
286
```
287
288
--------------------------------------------------------------------------------
289
290
## Cost Optimization (Reducing Logging Costs)
291
292
Cloud Logging costs are based on the volume of data ingested and stored. You can
293
reduce costs by excluding high-volume, low-value logs or by sampling them. Each
294
log sink that routes logs to a distinct log bucket contributes to cost and is a
295
candidate for optimization.
296
297
> [!CAUTION] **Destructive Actions (Tier D):** Exclusions in this section may
298
> immediately halt storage of log entries.
299
>
300
> **Safety Rule:** You MUST ask for explicit typed confirmation (for example, "I
301
> confirm I want to exclude load balancer logs") before executing exclusions or
302
> sampling updates.
303
304
### Exclude all high-volume logs (Tier D)
305
306
To completely stop ingesting a specific type of log into a log bucket, add an
307
exclusion to the log sinks that route logs into that bucket.
308
309
```bash
310
gcloud logging sinks update {sink_id} \
311
--project={project_id} \
312
--add-exclusion=name={exclusion_name},filter={exclusion_filter}
313
```
314
315
* `{sink_id}`: for example '_Default'
316
* `{exclusion_name}`: for example 'exclude-lb-logs'
317
* `{exclusion_filter}`: for example 'resource.type="http_load_balancer"'
318
319
### Sample high-volume logs (Tier D)
320
321
If you need some logs for analysis but want to reduce volume, use the `sample()`
322
function in the exclusion filter.
323
324
> [!IMPORTANT] The `sample(field, fraction)` function matches a `fraction` of
325
> logs. When used in an **exclusion filter**, the matched logs are
326
> **discarded**. If you exclude 90% of log entries, then only 10% are retained.
327
> To exclude 90%, use `sample(insertId, 0.9)` in the exclusion filter.
328
329
To exclude 90% of `DEBUG` severity logs:
330
331
```bash
332
gcloud logging sinks update _Default \
333
--project={project_id} \
334
--add-exclusion=name=sample-debug-logs,filter='severity=DEBUG AND sample(insertId, 0.9)'
335
```
336
337
### Verify Log Exclusions and Cost Optimization (Tier R)
338
339
To verify that log exclusions are correct, list the details of the sink and
340
check the `exclusions` to ensure your filter is present. For example, for the
341
`_Default` sink:
342
343
```bash
344
gcloud logging sinks describe _Default --project={project_id}
345
```
346
347
--------------------------------------------------------------------------------
348
349
## References and Supporting Links
350
351
* [Cloud Logging - Counter Metrics](https://docs.cloud.google.com/logging/docs/logs-based-metrics/counter-metrics.md.txt)
352
* [Cloud Logging - Custom Log Views](https://docs.cloud.google.com/logging/docs/logs-views.md.txt)
353
* [Cloud Logging - Exclusions](https://docs.cloud.google.com/logging/docs/routing/overview.md.txt)
354