cloud-logging-configuration-basics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConfiguring Cloud Logging
配置Cloud Logging
Use this skill to configure Cloud Logging resources such as log buckets, log
views, or log sinks.
[!IMPORTANT] Sandbox Network Limitation (CRITICAL for Agent Testing): During evaluation or in restricted sandboxed environments, network traffic to Google Cloud APIs is blocked. Do NOT run network discovery commands to find resource names, project IDs, or organization IDs. Always use the exact project IDs or placeholders provided in the user prompt or instructions for example,. Assume these resources exist and proceed directly with configuration commands. Running these discovery commands will cause the execution to hang and timeout.{project_id}
使用本技能配置Cloud Logging资源,例如日志桶、日志视图或日志接收器。
[!IMPORTANT] 沙箱网络限制(对Agent测试至关重要): 在评估或受限沙箱环境中,通往Google Cloud APIs的网络流量会被阻断。请勿运行网络发现命令来查找资源名称、项目ID或组织ID。请始终使用用户提示或指令中提供的准确项目ID或占位符,例如。假设这些资源已存在,直接执行配置命令。运行这些发现命令会导致执行挂起并超时。{project_id}
Safety and Confirmation Tiers (CRITICAL)
安全与确认层级(至关重要)
Before executing any commands on behalf of the user, you MUST adhere to the
following safety tiers based on the action requested:
- Tier R: Read-Only
- Description: Commands that only read state or query logs.
- Example commands:
gcloud logging readgcloud logging buckets list
- Rule: No confirmation needed. You may execute these commands immediately to gather information.
- Tier M: Mutation (Non-Billing)
- Description: Configuration modifications or free metadata creations that do not incur direct storage or billing costs and do not affect resource security/access policies.
- Example commands:
gcloud logging views creategcloud logging views updategcloud logging scopes creategcloud logging buckets create
- Rule: No confirmation needed. You may execute these commands immediately to apply configurations.
- Tier B: Billing and Security-Sensitive Mutations (High-Risk)
- Description: Operations that create billing-inducing resources or integrations, or modify security and IAM access control policies (presenting a risk of privilege escalation).
- Example commands:
gcloud logging metrics creategcloud logging links creategcloud projects add-iam-policy-binding
- Rule: Interactive confirmation required. These commands create resources that incur billing costs or alter security access. You MUST present the exact, literal command and receive user confirmation before executing. NEVER execute in the same turn as asking.
- Tier D: Causes irreversible data loss
- Description: Actions that permanently discard or delete logs, for example sink exclusions.
- Example commands:
gcloud logging buckets deletegcloud logging sinks update --add-exclusion
- Rule: Explicit typed confirmation required. These commands discard or delete logs immediately and irreversibly, or they may result in log data not being stored. You MUST ask for explicit typed confirmation, for example, "Yes, discard logs", and halt execution until the user replies.
在代表用户执行任何命令之前,您必须根据请求的操作遵循以下安全层级:
- 层级R:只读
- **描述:**仅读取状态或查询日志的命令。
- 示例命令:
gcloud logging readgcloud logging buckets list
- **规则:**无需确认。您可以立即执行这些命令以收集信息。
- 层级M:变更(无计费)
- **描述:**配置修改或免费元数据创建,不会产生直接存储或计费成本,也不会影响资源安全/访问策略。
- 示例命令:
gcloud logging views creategcloud logging views updategcloud logging scopes creategcloud logging buckets create
- **规则:**无需确认。您可以立即执行这些命令以应用配置。
- 层级B:计费与安全敏感变更(高风险)
- **描述:**会产生计费资源或集成的操作,或修改安全和IAM访问控制策略(存在权限提升风险)。
- 示例命令:
gcloud logging metrics creategcloud logging links creategcloud projects add-iam-policy-binding
- **规则:**需要交互式确认。这些命令会创建产生计费成本的资源或更改安全访问权限。您必须展示确切的命令文本并在执行前获得用户确认。绝不能在询问的同一轮次执行。
- 层级D:导致不可逆数据丢失
- **描述:**永久丢弃或删除日志的操作,例如接收器排除规则。
- 示例命令:
gcloud logging buckets deletegcloud logging sinks update --add-exclusion
- **规则:**需要明确的键入确认。这些命令会立即且不可逆地丢弃或删除日志,或可能导致日志数据无法存储。您必须请求明确的键入确认,例如“是的,丢弃日志”,并在用户回复前停止执行。
Getting Started
入门指南
If the executable is missing, refer to the
Google Cloud CLI Installation Guide
to install it.
gcloud如果缺少可执行文件,请参考Google Cloud CLI安装指南进行安装。
gcloudCreating Log Buckets (Compliance and Analytics) (Tier M)
创建日志桶(合规与分析)(层级M)
To create a regional log bucket with a specific retention policy for regulatory
compliance, and with Observability Analytics enabled:
[!WARNING] Mandatory Observability Analytics Downgrade Warning: Whenever providing guidance, writing a guide, or drafting commands on Cloud Logging cost optimization or exclusions, you must explicitly include the following warning in your final text response and any generated guides: "After a log bucket has been upgraded to use Observability Analytics, it cannot be downgraded to remove the analytics capability."
bash
gcloud logging buckets create {bucket_id} \
--project={project_id} \
--location={region} \
--retention-days={retention_days} \
--enable-analytics- : for example,
{bucket_id}my-custom-bucket - : for example,
{region}. You must use a regional log bucket to also use Observability Analytics.us-central1 - : for example,
{retention_days}365
A log bucket incurs no storage or ingestion charges until logs are routed to it
with a log sink.
创建具有特定保留策略以满足法规合规要求,并启用Observability Analytics的区域日志桶:
[!WARNING] 强制Observability Analytics降级警告:每当提供指导、编写指南或起草关于Cloud Logging成本优化或排除规则的命令时,您必须在最终文本回复和任何生成的指南中明确包含以下警告:“日志桶升级为使用Observability Analytics后,无法降级以移除分析功能。”
bash
gcloud logging buckets create {bucket_id} \
--project={project_id} \
--location={region} \
--retention-days={retention_days} \
--enable-analytics- :例如
{bucket_id}my-custom-bucket - :例如
{region}。您必须使用区域日志桶才能同时使用Observability Analytics。us-central1 - :例如
{retention_days}365
在通过日志接收器将日志路由到日志桶之前,日志桶不会产生存储或摄入费用。
Verify the Log Bucket (Tier R)
验证日志桶(层级R)
Check the log bucket's configuration to verify its compliance:
bash
gcloud logging buckets describe {bucket_id} \
--location={region} \
--project={project_id}检查日志桶的配置以验证其合规性:
bash
gcloud logging buckets describe {bucket_id} \
--location={region} \
--project={project_id}Route logs to the Log Bucket (Tier B)
将日志路由到日志桶(层级B)
[!IMPORTANT] Billing Action (Tier B): Routing log entries to a bucket incurs ongoing charges based on the volume of data stored. You MUST get interactive user confirmation before running this command.
Log entries are stored in the log bucket only if a log sink filter matches the
entries and targets that bucket.
To route log entries to the log bucket:
bash
gcloud logging sinks create {sink_id} \
projects/{project_id}/locations/{region}/buckets/{bucket_id} \
--log-filter='{filter_expression}' \
--project={project_id}[!IMPORTANT] **计费操作(层级B):**将日志条目路由到桶会根据存储的数据量产生持续费用。您必须在运行此命令前获得用户交互式确认。
只有当日志接收器过滤器匹配条目并指向该桶时,日志条目才会存储在日志桶中。
将日志条目路由到日志桶:
bash
gcloud logging sinks create {sink_id} \
projects/{project_id}/locations/{region}/buckets/{bucket_id} \
--log-filter='{filter_expression}' \
--project={project_id}Logs-Based Metrics
基于日志的指标
Logs-based metrics count the number of log entries that match a filter, allowing
you to track error rates and set up alerting policies.
基于日志的指标会统计匹配过滤器的日志条目数量,使您能够跟踪错误率并设置警报策略。
1. Create a logs-based counter metric (Tier B)
1. 创建基于日志的计数器指标(层级B)
[!IMPORTANT] Billing Action (Tier B): Creating logs-based metrics incurs ongoing charges based on the volume of data points reported. You MUST get interactive user confirmation before running this command.
To count the occurrences of a specific log pattern, for example, "OutOfMemory"
errors:
bash
gcloud logging metrics create {metric_name} \
--log-filter='{filter_expression}' \
--description='{description}' \
--project={project_id}- : for example,
{metric_name}oom_error_count - : for example,
{filter_expression}textPayload:"OutOfMemory" - : for example, "Count of log entries about OOMs"
{description}
Refer to
REST Resource: projects.metric
for restrictions on the metric fields.
[!IMPORTANT] **计费操作(层级B):**创建基于日志的指标会根据报告的数据点数量产生持续费用。您必须在运行此命令前获得用户交互式确认。
统计特定日志模式的出现次数,例如“OutOfMemory”错误:
bash
gcloud logging metrics create {metric_name} \
--log-filter='{filter_expression}' \
--description='{description}' \
--project={project_id}- :例如
{metric_name}oom_error_count - :例如
{filter_expression}textPayload:"OutOfMemory" - :例如“关于OOM的日志条目计数”
{description}
有关指标字段的限制,请参考
REST资源:projects.metric
2. Verify the logs-based metric (Tier R)
2. 验证基于日志的指标(层级R)
To verify that the metric exists and inspect its configuration, use the
command:
describebash
gcloud logging metrics describe {metric_name} \
--project={project_id}要验证指标是否存在并检查其配置,请使用命令:
describebash
gcloud logging metrics describe {metric_name} \
--project={project_id}Restricting Access to Sensitive Logs (Security)
限制敏感日志的访问权限(安全)
Anyone with on that project can see logs in a project's
log bucket via log view. To restrict visibility of the
logs:
roles/logging.viewer_Default_Default[!IMPORTANT] Ambiguity Handling (Guidance for Agents): If the user asks to "exclude", "hide", or "remove" sensitive logs without explicitly specifying whether they want to stop storing them, you MUST default to excluding them from the default view (Step 1). This is a safe, non-destructive Tier M action. Only configure a storage exclusion (under the "Discarding Sensitive Logs from Storage" section) if the user explicitly uses destructive terms like "stop storing", "permanently discard", or "sink exclusion".
拥有项目权限的任何人都可以通过日志视图查看项目日志桶中的日志。要限制日志的可见性:
roles/logging.viewer_Default_Default[!IMPORTANT] 歧义处理(Agent指南):如果用户要求“排除”、“隐藏”或“移除”敏感日志,但未明确指定是否要停止存储它们,您必须默认选择从默认视图中排除(步骤1)。这是一种安全、非破坏性的层级M操作。只有当用户明确使用诸如“停止存储”、“永久丢弃”或“接收器排除规则”等破坏性术语时,才配置存储排除规则(在“从存储中丢弃敏感日志”部分下)。
1. Exclude sensitive logs from default view (Tier M)
1. 从默认视图中排除敏感日志(层级M)
To explicitly exclude sensitive logs from general access, update the filter for
the log view:
_Defaultbash
gcloud logging views update _Default \
--bucket=_Default \
--location=global \
--project={project_id} \
--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}")'要明确排除敏感日志以防止一般访问,请更新日志视图的过滤器:
_Defaultbash
gcloud logging views update _Default \
--bucket=_Default \
--location=global \
--project={project_id} \
--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}")'2. Create a log view (Tier M)
2. 创建日志视图(层级M)
Create a new log view that includes the sensitive logs in the project's
log bucket. For example, a "security-logs-view" with access to the
_Default{sensitive_log_id}bash
gcloud logging views create security-logs-view \
--bucket=_Default \
--location=global \
--project={project_id} \
--log-filter='LOG_ID("{sensitive_log_id}")' \
--description="Sensitive logs"创建一个新的日志视图,包含项目日志桶中的敏感日志。例如,一个名为“security-logs-view”的视图,可访问
_Default{sensitive_log_id}bash
gcloud logging views create security-logs-view \
--bucket=_Default \
--location=global \
--project={project_id} \
--log-filter='LOG_ID("{sensitive_log_id}")' \
--description="敏感日志"3. Grant access to log view using IAM conditions (Tier B)
3. 使用IAM条件授予日志视图访问权限(层级B)
[!IMPORTANT] Security Action (Tier B): Granting IAM permissions changes access control policy and must be explicitly confirmed by the user before execution.
To restrict access to log view use IAM. When granting the Logs Viewer Accessor
role, always attach an IAM condition that restricts the grant to a specific log
view. For example, to grant access ONLY to the
in the bucket:
{security_group_email}security-logs-view_Defaultbash
gcloud projects add-iam-policy-binding {project_id} \
--member='group:{security_group_email}' \
--role='roles/logging.viewAccessor' \
--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"Replace with the location of the log bucket, for example
or a regional location like .
{location}globalus-central1[!IMPORTANT] **安全操作(层级B):**授予IAM权限会更改访问控制策略,必须在执行前获得用户明确确认。
要使用IAM限制日志视图的访问权限。授予Logs Viewer Accessor角色时,请始终附加一个IAM条件,将权限限制为特定日志视图。例如,授予仅访问桶中的的权限:
{security_group_email}_Defaultsecurity-logs-viewbash
gcloud projects add-iam-policy-binding {project_id} \
--member='group:{security_group_email}' \
--role='roles/logging.viewAccessor' \
--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"将替换为日志桶的位置,例如或区域位置如。
{location}globalus-central14. Verify Sensitive Log Restrictions (Tier R)
4. 验证敏感日志限制(层级R)
To verify that your Log View for sensitive logs is configured correctly:
bash
gcloud logging views describe {view_id} \
--bucket={bucket_id} \
--location={region} \
--project={project_id}Ensure that the block contains the appropriate restriction expression.
filter要验证您的敏感日志视图配置是否正确:
bash
gcloud logging views describe {view_id} \
--bucket={bucket_id} \
--location={region} \
--project={project_id}确保块包含适当的限制表达式。
filterDiscarding Sensitive Logs from Storage (Tier D)
从存储中丢弃敏感日志(层级D)
If your organization's compliance policies prohibit storing sensitive logs at
all, you can configure an exclusion to discard them before they are written to
disk.
[!CAUTION] Destructive Action (Tier D): Excluding logs from all log sinks deletes the log entries immediately and irreversibly.Safety Rule: You MUST ask the user for explicit typed confirmation, for example, "I confirm I want to excludelogs from storage", before running this command. Same-Turn Restriction: Do NOT execute the{sensitive_log_id}command in the same turn as asking for confirmation. Stop tool execution immediately and wait for the user to reply.gcloud logging sinks update
Exclude sensitive logs from storage using sink exclusions
bash
gcloud logging sinks update _Default \
--project={project_id} \
--add-exclusion=name=exclude-sensitive,filter='LOG_ID("{sensitive_log_id}")'如果您的组织合规政策禁止存储敏感日志,您可以配置排除规则,在日志写入磁盘之前将其丢弃。
[!CAUTION] **破坏性操作(层级D):**从所有日志接收器中排除日志会立即且不可逆地删除日志条目。**安全规则:**您必须请求用户明确的键入确认,例如“我确认要从存储中排除日志”,然后再运行此命令。**同轮次限制:**请勿在请求确认的同一轮次执行{sensitive_log_id}命令。立即停止工具执行并等待用户回复。gcloud logging sinks update
使用接收器排除规则从存储中排除敏感日志
bash
gcloud logging sinks update _Default \
--project={project_id} \
--add-exclusion=name=exclude-sensitive,filter='LOG_ID("{sensitive_log_id}")'Cost Optimization (Reducing Logging Costs)
成本优化(降低日志记录成本)
Cloud Logging costs are based on the volume of data ingested and stored. You can
reduce costs by excluding high-volume, low-value logs or by sampling them. Each
log sink that routes logs to a distinct log bucket contributes to cost and is a
candidate for optimization.
[!CAUTION] Destructive Actions (Tier D): Exclusions in this section may immediately halt storage of log entries.Safety Rule: You MUST ask for explicit typed confirmation (for example, "I confirm I want to exclude load balancer logs") before executing exclusions or sampling updates.
Cloud Logging成本基于摄入和存储的数据量。您可以通过排除高容量、低价值日志或对其进行采样来降低成本。每个将日志路由到不同日志桶的日志接收器都会产生成本,是优化的候选对象。
[!CAUTION] **破坏性操作(层级D):**本节中的排除规则可能会立即停止日志条目的存储。**安全规则:**在执行排除规则或采样更新之前,您必须请求明确的键入确认(例如“我确认要排除负载均衡器日志”)。
Exclude all high-volume logs (Tier D)
排除所有高容量日志(层级D)
To completely stop ingesting a specific type of log into a log bucket, add an
exclusion to the log sinks that route logs into that bucket.
bash
gcloud logging sinks update {sink_id} \
--project={project_id} \
--add-exclusion=name={exclusion_name},filter={exclusion_filter}- : for example '_Default'
{sink_id} - : for example 'exclude-lb-logs'
{exclusion_name} - : for example 'resource.type="http_load_balancer"'
{exclusion_filter}
要完全停止将特定类型的日志摄入日志桶,请向将日志路由到该桶的日志接收器添加排除规则。
bash
gcloud logging sinks update {sink_id} \
--project={project_id} \
--add-exclusion=name={exclusion_name},filter={exclusion_filter}- :例如'_Default'
{sink_id} - :例如'exclude-lb-logs'
{exclusion_name} - :例如'resource.type="http_load_balancer"'
{exclusion_filter}
Sample high-volume logs (Tier D)
对高容量日志进行采样(层级D)
If you need some logs for analysis but want to reduce volume, use the
function in the exclusion filter.
sample()[!IMPORTANT] Thefunction matches asample(field, fraction)of logs. When used in an exclusion filter, the matched logs are discarded. If you exclude 90% of log entries, then only 10% are retained. To exclude 90%, usefractionin the exclusion filter.sample(insertId, 0.9)
To exclude 90% of severity logs:
DEBUGbash
gcloud logging sinks update _Default \
--project={project_id} \
--add-exclusion=name=sample-debug-logs,filter='severity=DEBUG AND sample(insertId, 0.9)'如果您需要一些日志用于分析但想减少数据量,请在排除过滤器中使用函数。
sample()[!IMPORTANT]函数会匹配sample(field, fraction)比例的日志。在排除过滤器中使用时,匹配的日志会被丢弃。如果您排除90%的日志条目,则仅保留10%。要排除90%,请在排除过滤器中使用fraction。sample(insertId, 0.9)
排除90%的级日志:
DEBUGbash
gcloud logging sinks update _Default \
--project={project_id} \
--add-exclusion=name=sample-debug-logs,filter='severity=DEBUG AND sample(insertId, 0.9)'Verify Log Exclusions and Cost Optimization (Tier R)
验证日志排除规则和成本优化(层级R)
To verify that log exclusions are correct, list the details of the sink and
check the to ensure your filter is present. For example, for the
sink:
exclusions_Defaultbash
gcloud logging sinks describe _Default --project={project_id}要验证日志排除规则是否正确,请列出接收器的详细信息并检查以确保您的过滤器已存在。例如,对于接收器:
exclusions_Defaultbash
gcloud logging sinks describe _Default --project={project_id}