本文介绍了如何在Cloud Run上使用Stackdriver Logging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Stackdriver Logging适用于在Google Cloud Run(完全托管)中运行的简单Go应用程序.

I'm trying to get stackdriver logging working for a simple Go app running in Google Cloud Run (fully managed).

我创建了基于官方"堆栈驱动器golang示例
云运行文档指出,不应执行任何其他操作来编写堆栈驱动程序日志

I've created simplest possible demo app based on "official" stackdriver golang example
Cloud Run docs states that no additional actions should be performed to write stackdriver logs

我的服务使用默认服务帐户
我正在使用Go 1.13编译代码(Dockerfile是从复制的云运行示例按原样")
我尝试将其部署到其他区域,但没有成功
使用服务帐户凭据在本地运行容器时,stackdriver日志消息不会出现在本地终端或stackdriver控制台中

My service uses default service account
I'm using Go 1.13 to compile code (Dockerfile is copied from Cloud Run example "as-is")
I've tried to deploy it to different regions, with no success
When running container locally, using service account credentials, stackdriver log message does not appear in local terminal or stackdriver console

无论如何,在应用启动时,我只会看到"Before stackdriver logging",然后是"After stackdriver logging",中间没有其他消息\错误

No matter what, on app start I see only "Before stackdriver logging" followed by "After stackdriver logging" with no other messages\errors in the middle

这是日志代码的一部分(有关完整的源代码,Dockerfile以及上面的构建和运行use链接的说明):

Here's part of logging code (for full source, Dockerfile and instructions to build and run use link above):

import (
    "context"
    "log"
    "os"
    ...

    "cloud.google.com/go/compute/metadata"
    "cloud.google.com/go/logging"
)

func main() {
    loggingClient, err := stackdriverClient()
    ...
    log.Println("Before stackdriver logging")
    logger.StandardLogger(logging.Info).Println("Stackdriver log")
    if err = logger.Flush(); err != nil {
        log.Fatalf("Failed to flush client: %v", err)
    }
    if err = loggingClient.Close(); err != nil {
        log.Fatalf("Failed to close client: %v", err)
    }
    log.Println("After stackdriver logging")
    ...
}

func stackdriverClient() (client *logging.Client, err error) {
    var projectID string
    if projectID, err = metadata.ProjectID(); err == nil {
        client, err = logging.NewClient(context.Background(), projectID)
    }
    return
}

推荐答案

事实证明日志条目已成功写入
但是 logviewer网络用户界面中的默认Cloud Run过滤器不包含它们
下面的过滤器表达式对我有用,它可以获取所有日志:

It turns out that log entries are written successfully
But default Cloud Run filter in logviewer web UI does not include them
Filter expression below worked for me to get all the logs:

resource.type = "project" OR resource.type = "cloud_run_revision"

(服务名称,位置,严重性省略)

"stdout \ stderr"日志条目与resource.type="cloud_run_revision"匹配,而堆栈驱动程序日志条目与resource.type="project"

"stdout\stderr" log entries match resource.type="cloud_run_revision", while stackdriver log entries match resource.type="project"

更新:我已经在Google跟踪器中为此创建了票证

这篇关于如何在Cloud Run上使用Stackdriver Logging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:12