Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将cloud.google.com/go/logging配置为类似于写入stdout的日志?_Go_Logging_Google Cloud Functions_Google Cloud Stackdriver - Fatal编程技术网

如何将cloud.google.com/go/logging配置为类似于写入stdout的日志?

如何将cloud.google.com/go/logging配置为类似于写入stdout的日志?,go,logging,google-cloud-functions,google-cloud-stackdriver,Go,Logging,Google Cloud Functions,Google Cloud Stackdriver,我正在用Go编写一个Google云函数。我可以简单地通过向stdout写入日志消息。生成的日志包括有关函数、其运行时、跟踪信息等的信息。不过,我想在日志中添加一些结构化数据,这意味着stdout不够灵活。所以我尝试使用“cloud.google.com/go/logging”。我是这样设置的: // Set up like this logClient, err = logging.NewClient(ctx, "my-project-id") if err != nil { retur

我正在用Go编写一个Google云函数。我可以简单地通过向stdout写入日志消息。生成的日志包括有关函数、其运行时、跟踪信息等的信息。不过,我想在日志中添加一些结构化数据,这意味着stdout不够灵活。所以我尝试使用“cloud.google.com/go/logging”。我是这样设置的:

// 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,
})
但是,我在云控制台中看到的日志缺少自动附加到标准输出日志的所有好信息

要恢复部分,我可以在设置记录器时添加此选项:

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"),
    },
})
这允许我在云控制台中单击云功能列表中的“查看日志”按钮时查看日志。但是,它缺少函数执行id和跟踪id


我是否遗漏了一些显而易见的东西,或者说为谷歌云服务建立良好的日志记录非常复杂?感谢您的帮助。

诀窍是只需将json字符串写入标准输出

fmt.Println(`{"field_name": "Hello World!"}`)
日志的架构可以是

e、 g.设置严重性,如下所示:

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