为什么Google Logging客户端库在Google云函数内部不记录日志?

huangapple go评论88阅读模式
英文:

Why Google Logging client libraries not logging inside Google cloud functions?

问题

我正在尝试实现一个Google Cloud函数来测试Google Logging客户端库。以下是我的代码:

// Package p contains an HTTP Cloud Function.
package loggingclient

import (
    "cloud.google.com/go/logging"
    "net/http"
    "context"
    "fmt"
)

// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
    label := map[string]string{"priority": "High"}
    var projectName = "my-project-id"
    ctx := context.Background()
    client, err := logging.NewClient(ctx, projectName)
    if err != nil {
        fmt.Printf("client not created: %v", err)
    }
    lg := client.Logger("MY-LOGGER")
    lg.Log(logging.Entry{
        Payload:  "Hello, This is error!!",
        Severity: logging.Error,
        Labels:   label,
    })
    client.Close()
}

在这里,我期望得到一条带有消息:"Hello, This is error!!"、标签:"priority": "High"和严重性"ERROR"的日志条目。

但实际上,当我触发这个Cloud函数时,我没有收到任何新的日志条目。因此,客户端日志库在Cloud函数内部不起作用,如何解决这个问题呢?

谢谢。

英文:

I'm trying to implement a google cloud function to test Google Logging client library. below is my code

// Package p contains an HTTP Cloud Function.
package loggingclient
import (
    "cloud.google.com/go/logging"
    "net/http"
    "context"
    "fmt"
)
// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
    label := map[string]string{"priority": "High"}
    var projectName = "my-project-id"
    ctx := context.Background()
    client, err := logging.NewClient(ctx, projectName)
    if err != nil {
        fmt.Printf("client not created: %v", err)
    }
    lg := client.Logger("MY-LOGGER")
    lg.Log(logging.Entry{
        Payload:  "Hello, This is error!!",
        Severity: logging.Error,
        Labels:   label,
    })
  client.Close()
}

Here, I'm expecting a log entry with a message:"Hello, This is error!!" and with a lable:"priority": "High" and severirty "ERROR"

But actually, when I trigger this Cloud Function, I didn't get any new log entries. Therefore don't client logging libraries work inside cloud functions?, How to resolve this?<Br>
Thanks

答案1

得分: 1

这个功能适用于云函数。我之前在云函数中做过完全相同的事情。你可以使用谷歌的官方文档来进行云函数日志记录,点击这里

另外,请确保服务账号具有日志记录所需的权限。你可以参考这里的谷歌云日志访问控制文档。

英文:

It works on cloud functions. I have done the exact same thing in a cloud function before. You can use google's official documenation with cloud function logging here

Also ensure that the service account have necessary permissions for logging
https://cloud.google.com/logging/docs/access-control

huangapple
  • 本文由 发表于 2022年6月5日 12:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72504886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定