简体   繁体   English

如何在 Cloud Run 上使用 Stackdriver 日志记录

[英]How to use Stackdriver logging on Cloud Run

I'm trying to get stackdriver logging working for a simple Go app running in Google Cloud Run (fully managed), but don't see stackdriver entries in CloudRun logs.我正在尝试让 stackdriver 日志记录适用于在 Google Cloud Run(完全托管)中运行的简单 Go 应用程序,但在 CloudRun 日志中看不到 stackdriver 条目。

I've createdsimplest possible demo app based on "official" stackdriver golang example我基于“官方”stackdriver golang 示例创建了最简单的演示应用程序

Cloud Run docs states that no additional actions should be performed to write stackdriver logs Cloud Run 文档指出不应执行任何其他操作来写入堆栈驱动程序日志

My service uses default service account我的服务使用默认服务帐户

I'm using Go 1.13 to compile code (Dockerfile is copied from Cloud Run example "as-is")我正在使用 Go 1.13 编译代码(Dockerfile 是从Cloud Run 示例“按原样”复制的)

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在本地运行容器时,使用服务帐户凭据,堆栈驱动程序日志消息不会出现在本地终端或堆栈驱动程序控制台中

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无论如何,在应用程序启动时,我只看到"Before stackdriver logging" ,然后是"After stackdriver logging" ,中间没有其他消息\错误

日志视图截图

Here's part of logging code (use link above to get full source, Dockerfile and instructions to build and run the app):这是日志记录代码的一部分(使用上面的链接获取完整源代码、Dockerfile 以及构建和运行应用程序的说明):

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
}

It turns out that log entries are written successfully原来日志条目写入成功
But default Cloud Run filter in logviewer web UI does not include them但是logviewer web UI中的默认 Cloud Run 过滤器不包括它们
Filter expression below worked for me to get all the logs:下面的过滤器表达式为我工作以获取所有日志:

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

(service name, location, severity omitted) (服务名称、位置、严重性省略)
合成日志截图

"stdout\stderr" log entries match resource.type="cloud_run_revision" , while stackdriver log entries match resource.type="project" "stdout\stderr" 日志条目匹配resource.type="cloud_run_revision" ,而 stackdriver 日志条目匹配resource.type="project"

Update: I've created a ticket for this in google tracker更新:我在 google tracker 中为此创建了一张票

Since Cloud Run is already integrated with Cloud Logging, there is no need to use the Go Client Library.由于 Cloud Run 已与 Cloud Logging 集成,因此无需使用 Go 客户端库。 We run all our gRPC services on Cloud Run and use the following to ensure the logs are properly formatted in Cloud Logging:我们在 Cloud Run 上运行所有 gRPC 服务,并使用以下内容确保日志在 Cloud Logging 中的格式正确:

package main

import (
    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
    "os"
)

// initializeLogging sets up the logging configuration for the service.
// Invoke this method in your Init() method.
func initializeLogging() {
    // Set logging options for production development
    if os.Getenv("ENV") != "DEV" {
        // change the level field name to ensure these are parsed correctly in Stackdriver
        zerolog.LevelFieldName = "severity"
        // UNIX Time is faster and smaller than most timestamps
        zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
    } else {
        // Set logging options for local development
        log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
        zerolog.SetGlobalLevel(zerolog.DebugLevel)
    }
    
    // Example log
    log.Info().Msg("This is how you log at Info level")
}

The logs are then nicely displayed for local development.然后很好地显示日志以供本地开发使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Stackdriver Logging将错误记录到Stackdriver Error Reporting - How to log a error to Stackdriver Error Reporting via Stackdriver Logging 如何在 Google Cloud Run 中运行 go-cloud-debug-agent,以便我可以在 Stackdriver Debug 中调试我的 go 应用程序 - How to run go-cloud-debug-agent in Google Cloud Run so that I can debug my go app in Stackdriver Debug 如何使用日志中间件 - How to use logging middleware 在Kubernetes / Google容器引擎(GKE)上使用Stackdriver API进行日志记录 - Logging using Stackdriver API on Kubernetes / Google Container Engine (GKE) 如何在StackDriver ErrorReporting UI中填充响应代码 - How to populate response code in StackDriver ErrorReporting UI 如何在 GCP(云运行)上部署 Go 应用程序? - How to deploy an Go application on GCP (Cloud run)? 如何正确配置Dockerfile以在Google Cloud Run上运行? - How to configure Dockerfile correctly to run on Google Cloud Run? 如何使用模块替换云函数中的功能 - How to use modules replace functionality in cloud functions 如何配置 cloud.google.com/go/logging 看起来像写入标准输出的日志? - How to configure cloud.google.com/go/logging to look like logs written to stdout? 为什么 Google Logging 客户端库不在 Google 云功能中记录? - Why Google Logging client libraries not logging inside Google cloud functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM