Logging to the Google Cloud in Google Container/Compute Engine with Go

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

Logging to the Google Cloud in Google Container/Compute Engine with Go

问题

我有一个在GKE上运行Go的应用程序,有20个节点。我想将所有日志合并到Google开发者控制台日志查看器中,但我遇到了两个问题。我无法进行严重性过滤,并且我的日志消息中的每个换行符都会在查看器中开始一个新的日志条目(对于日志中的换行符来说是有问题的)。

我已经设置了google-fluent-d,以便将所有的标准输出日志记录到云端,并且我已经利用了log.Lshortfile、调用深度和log.Logger.Output来从"log"库中获取文件名和行号。

我已经查看了这个库:"google.golang.org/cloud/logging",但是我在使用它时遇到了麻烦。

是否有一个库或者一个示例,可以最好地在GKE和GCE中编写日志?

英文:

I have a GKE application with 20 nodes running Go. I would like to consolidate all the logs to view in the Google Developers Console log viewer, but I am having 2 problems. I can't get severity filtering, and each newline in my log message starts a new log entry in the viewer (problematic with newlines in the log).

I have the google-fluent-d setup so all stdout gets logged in the cloud, and I have made use of log.Lshortfile, call depth and log.Logger.Output to get filename and line number from the "log" library.

I've looked at this library: "google.golang.org/cloud/logging" but I am having trouble getting it to work.

Is there a library somewhere or an example of the best way to write logs in GKE and GCE?

答案1

得分: 7

根据你的感觉,你可以采取几种不同的方法。

最简单的选择可能是将日志格式切换为JSON格式。google-fluentd代理将自动解析JSON,并将每条消息的结构化数据导出到云日志API中。如果JSON中有severity字段,它将自动获取严重性信息,并且使用JSON可以避免消息中的(转义的)换行符导致错误。

更困难(但更灵活)的选择是获取你找到的客户端库。我不确定它是否与你提供的链接相同,但我相信这是最新的库。如果你能提供更多关于你遇到的问题的上下文,我可以提供帮助,或者联系云日志团队的人员来提供帮助。

英文:

There are a couple things you can do depending on how ambitious you're feeling.

The easiest option would likely be to switch your logging format to be JSON. The google-fluentd agent will automatically parse the JSON for you, exporting each message's structured data to the cloud logging API. It will automatically grab severity information if a severity field is in the JSON, and using JSON can keep it from breaking on (escaped) newlines in your messages.

The tougher (but more flexible) option would be to get the client library that you found working. I'm not sure whether it's the same as the one you linked to, but I believe this is the most recent one. If you can give more context about the problems you had, I can help or wrangle someone from the cloud logging team to help.

答案2

得分: 0

我使用了自定义的日志记录类,而不是每次都记录每条消息,我将它们保存在一个变量中,并在服务器响应后发送。因此,我在Google Cloud中只有一个非常长的textPayload,用于每个请求的日志记录。

自定义的日志记录类大致如下:

class CustomLogging {
    messages = "";

    log(message) {
        this.messages += '\n' + message;
    }

    sendLogs(message) {
        console.log(this.messages);
    }
}

每个请求的日志记录看起来像这样:

Logging to the Google Cloud in Google Container/Compute Engine with Go

英文:

I used custom logging class and instead of log every message I saved them in a variable, and send it after the server response - so I finally have in google cloud only one logging for every request with a very long textPayload.

The custom logging class looks something like this:

class CustomLogging {
   
    messages = "";

    log(message) {
        this.messages += '\n' + message;
    }

    sendLogs(message) {
        console.log(this.messages);
    }

}

Every request log look like this:

Logging to the Google Cloud in Google Container/Compute Engine with Go

huangapple
  • 本文由 发表于 2016年2月23日 23:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35582153.html
匿名

发表评论

匿名网友

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

确定