我的 Golang 应用程序中有一个 logrus 日志处理程序。日志使用 JSONFormatter 进行格式化,并作为单行提交给 Datadog,Datadog 会聚合它们并很好地显示它们。然而,我最近发现了一种情况,其中存在未处理的恐慌,并且logrus 记录器没有捕获到这种情况。这导致实际的恐慌和堆栈跟踪分布在多个输出行上,Datadog 单独收集这些输出行。这会花费我们金钱并且使日志很难阅读。
我将解决这个问题,但如果发生任何进一步的未处理的恐慌,我希望能够使用 logrus JSONFormatter 捕获它们。
像这样的东西:
package main
import (
"os"
"sync"
"github.com/sirupsen/logrus"
)
var (
loggerInstance *logrus.Entry
once sync.Once
logger = GetLogger()
)
// GetLogger initializes and returns a reference to a CustomLogger object.
func GetLogger() *logrus.Entry {
once.Do(func() {
logrus.SetFormatter(&logrus.JSONFormatter{})
// We'll just pipe everything to stdout. It's json so Datadog will parse the level regardless
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.TraceLevel) // We'll leave this as trace and just …Run Code Online (Sandbox Code Playgroud)