使用日志查看器(预览版)的示例查询

本页面提供了建议的查询,以便您能够使用 Google Cloud Console 中的日志查看器(预览版)更轻松地查找重要日志。

所有列出的查询都可以在日志查看器(经典版和预览版)、Logging API命令行界面中应用,但本页面重点介绍如何在日志查看器(预览版)中使用查询。

日志查看器使用布尔表达式指定项目中的部分日志条目。您可以使用这些查询选择特定日志或日志服务中的日志条目,或选择满足元数据或用户定义字段相关条件的日志条目。如需详细了解如何查询,请参阅构建查询

开始使用

如需转到日志查看器(预览版),请执行以下操作:

  1. 转到 Google Cloud 导航菜单 ,然后依次选择 Logging > 日志查看器
    转到日志查看器
  2. 选择 Google Cloud 项目。
  3. 从版本选择器菜单中,将日志查看器版本从经典版切换为新的日志查看器预览版

您现在已进入日志查看器(预览版)。

使用示例查询

若要应用下表中的查询,请通过点击任意表达式行末尾的剪贴板图标 来复制表达式,然后将复制的表达式粘贴到查询构建器文本框中:

查询构建器文本框显示了要在何处输入查询

输入表达式后,点击运行查询。与查询匹配的日志会列在查询结果下。

本页面的后面部分列出的一些查询包括您应该用有效值替换的变量(用方括号 [] 表示)。例如,如果查询包含 logName,您提供的 [PROJECT_ID] 必须引用当前选定的 Google Cloud 项目;否则,该查询将不起作用。如需了解详情,请参阅问题排查

以下部分按 Google Cloud 服务对查询进行分组。

App Engine 查询

过滤条件名称 表达式
新年前夜(按世界协调时间)的 App Engine 日志

resource.type="gae_app" AND
severity>=ERROR AND
timestamp>="2018-12-31T00:00:00Z" AND timestamp<="2019-01-01T00:00:00Z" 
具有服务器错误的 App Engine 请求日志

resource.type="gae_app" AND
log_name="projects/[PROJECT_ID]/logs/appengine.googleapis.com%2Frequest_log" AND
http_request.status>=500 
采样 HTTP 错误日志

resource.type="gae_app" AND
proto_payload.status >= 400 AND
sample(insertId, 0.1) 
搜索 App Engine 跟踪 ID

resource.type="gae_app" AND
trace="projects/[PROJECT_ID]/traces/[TRACE_ID]" 

BigQuery 查询

过滤条件名称 表达式
BigQuery 审核日志

resource.type="bigquery_resource" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com" 
BigQuery 数据传输服务作业

resource.type="bigquery_resource" AND
proto_payload.request_metadata.caller_supplied_user_agent="BigQuery Data Transfer Service" AND
proto_payload.method_name="jobservice.insert" 
BigQuery 数据集更新

resource.type="bigquery_resource" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="datasetservice.update" 
BigQuery 作业已完成

resource.type="bigquery_resource" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Fdata_access" AND
proto_payload.method_name="jobservice.jobcompleted" 
BigQuery 大型查询

resource.type="bigquery_resource" AND
proto_payload.method_name="jobservice.jobcompleted" AND
proto_payload.service_data.job_completed_event.job.job_statistics.total_billed_bytes>1073741824 
已超出 BigQuery 配额

resource.type="bigquery_resource" AND
proto_payload.status.code=8 AND
severity>=WARNING 
已开始 BigQuery 查询

resource.type="bigquery_resource" AND
proto_payload.method_name="jobservice.insert" 

Dataflow 查询

过滤条件名称 表达式
Dataflow 工作器中的错误和警告

resource.type="dataflow_step" AND
log_name="projects/[PROJECT_ID]/logs/dataflow.googleapis.com%2Fworker" AND
severity>=WARNING 

Dataproc 查询

过滤条件名称 表达式
Dataproc Apache Hadoop 日志

resource.type="cloud_dataproc_cluster" AND
json_payload.class:"org.apache.hadoop.mapreduce" 

Cloud Deployment Manager

过滤条件名称 表达式
Deployment Manager 错误

resource.type="deployment" AND
severity>=ERROR 

Cloud Functions 查询

过滤条件名称 表达式
Cloud function 错误

resource.type="cloud_function" AND
log_name="projects/[PROJECT_ID]/logs/cloudfunctions.googleapis.com%2Fcloud-functions" AND
severity>=ERROR 

Identity and Access Management 查询

过滤条件名称 表达式
服务帐号创建日志

resource.type="service_account" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="google.iam.admin.v1.CreateServiceAccount" 
服务帐号创建密钥日志

resource.type="service_account" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="google.iam.admin.v1.CreateServiceAccountKey" 
设置访问控制政策日志

resource.type="project" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="SetIamPolicy" 
已获得组织访问权限的外部成员

resource.type="project" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.@type="type.googleapis.com/google.cloud.audit.AuditLog" AND
proto_payload.request.@type:"IamPolicy" AND
proto_payload.service_data.policy_delta.binding_deltas.member:* AND
NOT proto_payload.service_data.policy_delta.binding_deltas.member:"@[DOMAIN_NAME].com" 
创建、修改或删除资源

log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName:("create" OR "delete" OR "update")

Cloud Source Repositories 查询

过滤条件名称 表达式
Cloud Source Repository 日志

resource.type="csr_repository" AND
resource.labels.name="[REPOSITORY_NAME]"

Cloud Spanner 查询

过滤条件名称 表达式
特定 Spanner 实例的 Cloud Spanner 日志

resource.type="spanner_instance" AND
resource.labels.instance_id="[SPANNER_INSTANCE]"

Cloud SQL 查询

过滤条件名称 表达式
Cloud SQL 审核日志

resource.type="cloudsql_database" AND
resource.labels.database_id="[DATABASE_ID]" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity"
Cloud SQL MySQL 错误日志

resource.type="cloudsql_database" AND
log_name="projects/[PROJECT_ID]/logs/cloudsql.googleapis.com%2Fmysql.err"
基于 Cloud SQL MySQL 的数据库

resource.type="cloudsql_database" AND
resource.labels.database_id="[DATABASE_ID]" AND
log_name="projects/[PROJECT_ID]/logs/cloudsql.googleapis.com%2Fmysql"
基于 Cloud SQL Postgres 的数据库

resource.type="cloudsql_database" AND
resource.labels.database_id="[DATABASE_ID]" AND
log_name="projects/[PROJECT_ID]/logs/cloudsql.googleapis.com%2Fpostgres.log"

Compute Engine 查询

过滤条件名称 表达式
Google Compute Engine 管理员活动日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity"
Google Compute Engine 防火墙规则删除

resource.type="gce_firewall_rule" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name:"firewalls.delete" 
Google Compute Engine 旧版活动日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Factivity_log" 
Google Compute Engine 虚拟机系统日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/syslog" 

Cloud Storage 查询

过滤条件名称 表达式
GCS 存储分区日志

resource.type="gcs_bucket" AND
resource.labels.bucket_name="[BUCKET_NAME]"
GCS 存储分区审核日志

resource.type="gcs_bucket" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com" 
GCS 存储分区创建日志

resource.type="gcs_bucket" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="storage.buckets.create" 
GCS 存储分区删除日志

resource.type="gcs_bucket" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name="storage.buckets.delete" 

Cloud Tasks 查询

过滤条件名称 表达式
Cloud Tasks 队列日志

resource.type="cloud_tasks_queue" AND
resource.labels.queue_id="[QUEUE_ID]"

与 Kubernetes 相关的查询

如需查看管理员活动审核日志查询的概览和示例,请参阅 GKE 审核日志记录页面

集群级层查询

过滤条件名称 表达式
Google Kubernetes Engine 集群操作

resource.type="gke_cluster" AND
log_id("cloudaudit.googleapis.com/activity")
Google Kubernetes Engine 集群创建

resource.type="gke_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName="google.container.v1.ClusterManager.CreateCluster"
      
Kubernetes 集群部署

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName:"deployments"
      
Kubernetes 集群身份验证失败

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.authenticationInfo.principalEmail="system:anonymous"
      
us-central1-b 中的 Kubernetes 集群操作和事件

resource.type="k8s_cluster" AND
resource.labels.location="us-central1-b"
      
来自用户的 Kubernetes pod 请求

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName:"io.k8s.core.v1.pods" AND
protoPayload.authenticationInfo.principalEmail="[USER_EMAIL]"
      
Kubernetes 事件

resource.type="k8s_cluster" AND
log_id("events")
      
Kubernetes 端点更新

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.request.kind="Endpoints"
      
Kubernetes 控制层面日志

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.serviceName="k8s.io"
      
Kubernetes Engine 控制层面日志

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.serviceName="container.googleapis.com"
      
pod 删除

resource.type="k8s_cluster" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName=~"io\.k8s\.core\.v1\.pods\.(create|delete)"
      
来自控制层面的 Kubernetes pod 审核日志

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.resourceName="core/v1/namespaces/POD_NAMESPACE/pods/POD_NAME
      
Kubernetes pod 逐出

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName="io.k8s.core.v1.pods.eviction.create"
      
来自控制层面的 Kubernetes 节点审核日志

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.methodName:"io.k8s.core.v1.nodes"
      
插件管理器活动的 Kubernetes 集群控制层面

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.authenticationInfo.principalEmail="system:addon-manager"
      
Kubernetes 控制层面错误(不包括 Conflict,这是正常的)

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("cloudaudit.googleapis.com/activity") AND
protoPayload.status.message!="Conflict" AND
protoPayload.status.code!=0
      
Ingress 控制器事件

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("events") AND
jsonPayload.source.component="loadbalancer-controller"
      
Service 控制器事件 (kube-controller-manager)

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("events") AND
jsonPayload.source.component="service-controller"
      
集群自动扩缩器事件

resource.type="k8s_cluster" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("events") AND
jsonPayload.source.component="cluster-autoscaler"
      

pod 级层查询

过滤条件名称 表达式
创建期间的查询 pod

resource.type="k8s_pod" AND
resource.labels.pod_name="POD_NAME" AND
log_id("events")
      
调度器事件

resource.type="k8s_pod" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("events") AND
jsonPayload.source.component="default-scheduler"
      
调度器事件(抢占)

resource.type="k8s_pod" AND
resource.labels.location="CLUSTER_LOCATION" AND
resource.labels.cluster_name="CLUSTER_NAME" AND
log_id("events") AND
jsonPayload.source.component="default-scheduler" AND
jsonPayload.reason="Preempted"
      

节点级层查询

过滤条件名称 表达式
节点事件

resource.type="k8s_node" AND
log_id("events")
      
查看 Kube-proxy 日志

resource.type="k8s_node" AND
log_id("kube-proxy")
      
查看 dockerd 日志

resource.type="k8s_node" AND
log_id("container-runtime")
      
查看 kubelet 错误或故障

resource.type="k8s_node" AND
log_id("kubelet") AND
jsonPayload.MESSAGE:("error" OR "fail")
      

容器查询

过滤条件名称 表达式
集群中所有 pod 和容器内的 stdout 容器日志

resource.type="k8s_container" AND
log_id("stdout")
      
集群中所有 pod 和容器内的容器错误日志

resource.type="k8s_container" AND
log_id("stderr") AND
severity=ERROR
      
具有特定名称的 pod 的容器错误日志

resource.type="k8s_container" AND
resource.labels.pod_name="POD_NAME" AND
severity=ERROR
      
特定 pod 中特定容器的容器错误日志

resource.type="k8s_container" AND
resource.labels.pod_name="POD_NAME" AND
resource.labels.container_name="server" AND
severity=ERROR
      
特定命名空间和容器的容器错误日志

resource.type="k8s_container" AND
resource.labels.namespace_name="istio-system" AND
resource.labels.container_name="egressgateway" AND
severity=ERROR
      
具有特定标签的 pod 的容器日志

resource.type="k8s_container" AND
labels."k8s-pod/app"="loadgenerator" AND
severity=ERROR
      
pod 的容器日志,该 pod 具有使用 skaffold 生成的标签

resource.type="k8s_container" AND
labels."k8s-pod/app"="loadgenerator" AND
labels."k8s-pod/skaffold_dev/run-id"=[SKAFFOLD_RUN_ID]
severity=ERROR
      
在 textPayload 中包含 POST 的特定 pod 的容器错误日志

resource.type="k8s_container" AND
resource.labels.pod_name="POD_NAME" AND
textPayload:"POST" AND
severity=ERROR
      
在结构化 JSON 中包含 GET 的特定 pod 的容器错误日志

resource.type="k8s_container" AND
resource.labels.pod_name="POD_NAME" AND
jsonPayload."http.req.method"="GET" AND
severity=ERROR
      
kube-system 命名空间中的容器错误日志

resource.type="k8s_container" AND
resource.labels.namespace_name="kube-system" AND
severity=ERROR
      
容器数据分析日志中的容器错误

resource.type="k8s_container" AND
log_id("clouderrorreporting.googleapis.com/insights")
      
Kubernetes 容器日志

resource.type="k8s_container" AND
resource.labels.cluster_name="CONTAINER_NAME"
      

Logging 代理应用查询

过滤条件名称 表达式
Apache 日志

resource.type="gce_instance" AND
(log_name:"/apache-access" OR log_name:"/apache-error")
Cassandra 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/cassandra"
Chef 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/chef-"
Gitlab 日志

resource.type="gce_instance"
log_name:"projects/[PROJECT_ID]/logs/gitlab-" 
Jenkins 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/jenkins"
Jetty 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/jetty-"
Joomla 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/joomla"
Linux 系统日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/syslog"
Magneto 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/magneto-"
Mediawiki 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/mediawiki"
memcached 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/memcached"
MongoDB 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/mongodb"
MySQL 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/mysql"
Nginx 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/nginx-"
Postgresql 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/postgresql"
Puppet 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/puppet-"
RabbitMQ 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/rabbitmq-"
Redmine 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/redmine"
Salt 日志

resource.type="gce_instance" AND
log_name:"projects/[PROJECT_ID]/logs/salt-"
Slow MySQL 查询

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/mysql-slow"
Solr 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/solr"
SugarCRM 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/sugarcrm"
Tomcat 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/tomcat"
Zookeeper 日志

resource.type="gce_instance" AND
log_name="projects/[PROJECT_ID]/logs/zookeeper"

网络查询

过滤条件名称 表达式
防火墙 - 所有日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Ffirewall"
各国家/地区的防火墙日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Ffirewall" AND
json_payload.remote_location.country=[COUNTRY_ISO_ALPHA_3]
各虚拟机的防火墙日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Ffirewall" AND
json_payload.instance.vm_name="[INSTANCE_NAME]"
防火墙子网日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Ffirewall" AND
resource.labels.subnetwork_name="[SUBNET_NAME]"
Compute Engine 子网络到某一子网的流量日志

resource.type="gce_subnetwork" AND
ip_in_net(json_payload.connection.dest_ip, "[SUBNET_IP]")
VPC 流日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Fvpc_flows"
特定端口和协议的 VPC 流日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Fvpc_flows" AND
json_payload.connection.src_port="[PORT_ID]" AND
json_payload.connection.protocol="[PROTOCOL]"
特定子网的 VPC 流日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Fvpc_flows" AND
resource.labels.subnetwork_name"=[SUBNET_NAME]"
特定子网前缀的 VPC 流日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Fvpc_flows" AND
ip_in_net(json_payload.connection.dest_ip,[SUBNET_IP])
特定虚拟机的 VPC 流日志

resource.type="gce_subnetwork" AND
log_name="projects/[PROJECT_ID]/logs/compute.googleapis.com%2Fvpc_flows" AND
json_payload.src_instance.vm_name="[VM_NAME]"
VPN 网关日志

resource.type="vpn_gateway" AND
resource.labels.gateway_id="[GATEWAY_ID]"
HTTP 负载平衡器 5xx 错误

resource.type="http_load_balancer" AND
http_request.status>=500
HTTP 负载平衡器发送到 PHPMyAdmin 的请求

resource.type="http_load_balancer" AND
http_request.request_url:"phpmyadmin"

安全日志记录查询

过滤条件名称 表达式
审核日志 - 全部

log_name:"projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com"
审核日志 - Access Transparency (AXT)

log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Faccess_transparency"
审核日志 - 管理员活动

log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity"
审核日志 - 数据访问

log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Fdata_access"
审核日志 - 系统事件

log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Fsystem_event"

Google Cloud 的运维套件查询

过滤条件名称 表达式
日志接收器活动

resource.type="logging_sink" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity"
基于日志的指标创建或更新活动

resource.type="metric" AND
log_name="projects/[PROJECT_ID]/logs/cloudaudit.googleapis.com%2Factivity" AND
proto_payload.method_name:(UpdateLogMetric OR CreateLogMetric)
主机的正常运行时间网址检查

resource.type="uptime_url" AND
resource.labels.host="[URL]"

问题排查

如需详细了解查询语法和问题排查说明,请参阅构建查询:问题排查

后续步骤

如需详细了解可用于自定义这些查询的查询语法,请参阅构建查询