简体   繁体   English

使用 Go 在 Google Container/Compute Engine 中登录到 Google Cloud

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

I have a GKE application with 20 nodes running Go.我有一个 GKE 应用程序,有 20 个运行 Go 的节点。 I would like to consolidate all the logs to view in the Google Developers Console log viewer, but I am having 2 problems.我想合并所有日志以在 Google Developers Console 日志查看器中查看,但我遇到了 2 个问题。 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.我有 google-fluent-d 设置,所以所有标准输出都记录在云中,我已经使用 log.Lshortfile、调用深度和 log.Logger.Output 从“日志”库中获取文件名和行号。

I've looked at this library: "google.golang.org/cloud/logging" but I am having trouble getting it to work.我看过这个库:“google.golang.org/cloud/logging”,但我无法让它工作。

Is there a library somewhere or an example of the best way to write logs in GKE and GCE?是否有某个库或在 GKE 和 GCE 中写入日志的最佳方法的示例?

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.最简单的选择可能是将您的日志记录格式切换为 JSON。 The google-fluentd agent will automatically parse the JSON for you, exporting each message's structured data to the cloud logging API. google-fluentd 代理会自动为您解析 JSON,将每条消息的结构化数据导出到云日志 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.如果severity字段在 JSON 中,它将自动获取严重性信息,并且使用 JSON 可以防止它在您的消息中破坏(转义)换行符。

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.如果您可以提供有关您遇到的问题的更多背景信息,我可以帮助或与云日志记录团队中的某个人争论以提供帮助。

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.我使用了自定义日志记录类,而不是记录我将它们保存在变量中的每条消息,并在服务器响应之后发送它 - 所以我最终在谷歌云中只有一个日志记录,每个请求都有很长的 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:每个请求日志看起来像这样:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM