简体   繁体   English

如何配置 cloud.google.com/go/logging 看起来像写入标准输出的日志?

[英]How to configure cloud.google.com/go/logging to look like logs written to stdout?

I'm writing a Google Cloud Function in Go.我正在 Go 中编写 Google Cloud Function。 I can write log messages simply by writing to stdout.我可以简单地通过写入标准输出来编写日志消息。 The logs produced includes info about the function, its runtime, trace info, etc. Super nice, however, I want to add some structured data to my logs which means stdout is not flexible enough.生成的日志包括有关 function、其运行时间、跟踪信息等的信息。非常好,但是,我想在我的日志中添加一些结构化数据,这意味着 stdout 不够灵活。 So I'm trying to use "cloud.google.com/go/logging".所以我正在尝试使用“cloud.google.com/go/logging”。 I set it up like so:我是这样设置的:

// Set up like this
logClient, err = logging.NewClient(ctx, "my-project-id")
if err != nil {
    return
}
logger := logClient.Logger("my-function-name")

// And log like this
logger.Log(logging.Entry{
    Payload:  "Hello World!",
    Severity: logging.Info,
})

However, the logs I see the Cloud Console are missing all of that nice info that is automatically attached to the stdout logs.但是,我在 Cloud Console 中看到的日志缺少所有自动附加到标准输出日志的好信息。

To get some of it back, I can add this option when setting up the logger:为了取回其中的一部分,我可以在设置记录器时添加此选项:

logging.CommonResource(&monitoredres.MonitoredResource{
    Type: fmt.Sprintf("projects/%s/logs/cloudfunctions.googleapis.com%scloud-functions", os.Getenv("GCP_PROJECT"), "%2F"),
    Labels: map[string]string{
        "function_name": os.Getenv("FUNCTION_NAME"),
        "project_id":    os.Getenv("GCP_PROJECT"),
        "region":        os.Getenv("FUNCTION_REGION"),
    },
})

This allows me to see the logs when I click the "see logs" button in the cloud function list in the Cloud Console.这允许我在 Cloud Console 中单击云 function 列表中的“查看日志”按钮时查看日志。 However, it's missing the function execution id and trace id.但是,它缺少 function 执行 ID 和跟踪 ID。

Am I missing something obvious here or is it just ridiculously complicated to set up decent logging for Google Cloud services?我在这里遗漏了一些明显的东西,还是为谷歌云服务设置体面的日志记录非常复杂? Any help appreciated.任何帮助表示赞赏。

The trick was to simply write a json string to stdout.诀窍是简单地将 json 字符串写入标准输出。

fmt.Println(`{"field_name": "Hello World!"}`)

The schema for the logs can be found here .可以在此处找到日志的架构。

eg set the severity like so:例如像这样设置严重性:

fmt.Println(`{"field_name": "Hello World!", "severity": "DEBUG"}`)

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

相关问题 我无法导入“cloud.google.com/go/datastore” - I can't import “cloud.google.com/go/datastore” 如何调查/修复:模糊导入:在多个模块中找到 package cloud.google.com/go/compute/metadata - How to investigate/fix: ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules `go get cloud.google.com/go/cloudtasks/apiv2` 的一些问题; 未定义:grpc.RoundRobin - Some issues with `go get cloud.google.com/go/cloudtasks/apiv2`; undefined: grpc.RoundRobin google.golang.org/appengine/datastore与cloud.google.com/go/datastore - google.golang.org/appengine/datastore vs cloud.google.com/go/datastore 记录到 stderr 和 stdout golang Google Cloud Platform - Logging to stderr and stdout golang Google Cloud Platform 使用 Google Cloud go 库进行日志记录时出现问题 - Problems using the Google Cloud go library for logging 去 source.cloud.google.com - go get with source.cloud.google.com 使用 Go 在 Google Container/Compute Engine 中登录到 Google Cloud - Logging to the Google Cloud in Google Container/Compute Engine with Go 如何配置用Go编写的Windows服务的失败操作? - How do I configure failure actions of a Windows service written in Go? 如何使用 github.com/mattn/go-sqlite3 为 Google Cloud VM 交叉编译 Go 包? - How to cross-compile a Go package using github.com/mattn/go-sqlite3 for a Google Cloud VM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM