目前在GCP上运行go服务,但是在日志查看器中,每条消息都被视为错误。
是否有一种通常建议的方法来根据日志级别记录到stderr和stdout。即错误到标准错误和任何其他标准输出。
我目前正在使用logrus包,并遇到了这个实现。我看到的在使用同一个包的情况下实现这一点的其他方法是将日志记录器传递给每个需要它的包,或者创建一个全局日志对象,这两种方法我都不太热衷。
https://github.com/microsoft/fabrikate/pull/252/commits/bd24d62d7c2b851ad6e7b36653eb0a6dc364474b#diff-ed0770fdbf87b0c6d536e33a99a8df9c
4条答案
按热度按时间nbysray51#
您可以使用Stackdriver库包为GoLang:
然后你可以使用StandardLogger:
在这里您可以找到documentation on Google Cloud website
更新:
或者你可以给予GCP formatter for logrus
这不会将您的应用绑定到Google Cloud Platform。但是,这并不意味着在另一个平台上,您不需要更改代码来格式化输出。
使用StackDriver库是Google Cloud的推荐解决方案。
3j86kqsm2#
我们使用https://github.com/rs/zerolog和Init()方法中调用的以下方法来设置全局级别的日志选项:
gkl3eglg3#
我推荐https://github.com/apsystole/log。您可以交换
log
和logrus
导入。它是一个小的零依赖模块,因此比logrus轻得多。wfauudbj4#
如果你是从一个运行在GKE pod中的进程中进行日志记录,并且你希望你的日志通过stdout流被摄取,那么只需要使用标准的Google golang日志库,但是将其配置为写入stdout。