【问题标题】:Integrate elastic search with Logrus (golang logging)将弹性搜索与 Logrus 集成(golang 日志记录)
【发布时间】:2020-04-27 05:11:02
【问题描述】:

我正在使用 logrus 来记录我的 golang 应用程序的所有日志。但是,我还想将它与 Elastic Search 集成,这样当我创建 logrus 日志条目时,所有日志也会刷新到 Elastic Search。目前所有日志都创建在一个文件中,如下面的 sn-p 所示。如何与弹性搜索集成?

type LoggerConfig struct {
    Filename string `validate:regexp=.log$`
    AppName  string `validate:regexp=^[a-zA-Z]$`
}  

type AppLogger struct {
    Err    error
    Logger logrus.Entry
}

func Logger(loggerConfig LoggerConfig) AppLogger {
    response := new(AppLogger)
    // validate the schema of the logger_config
    if errs := validator.Validate(loggerConfig); errs != nil {
        response.Err = errs
        // this sets up the error on the the response struct
    }

    logrus.SetFormatter(&logrus.JSONFormatter{})
    f, err := os.OpenFile(loggerConfig.Filename, os.O_WRONLY|os.O_CREATE, 0755)
    if err != nil {
        response.Err = err
    }
    multipleWriter := io.MultiWriter(os.Stdout, f)
    logrus.SetOutput(multipleWriter)

    contextLogger := logrus.WithFields(logrus.Fields{
        "app": loggerConfig.AppName,
    })


    //logrus.AddHook(hook)
    response.Logger = *contextLogger

    //response.Logger.Info("adele")
    return *response

}

我试过elogrus,它添加了一个钩子,但我不知道如何使用它。这是尝试创建弹性搜索客户端的方法。我如何将它与 logrus 实例集成?

func prepareElasticSearchClient() *elastic.Client {
    indexName := "my-server"

    client, _ := elastic.NewClientFromConfig(&config.Config{
        URL:      os.Getenv("ELASTIC_SEARCH_URL_LOGS") + ":" + os.Getenv("ELASTIC_SEARCH_PORT_LOGS"),
        Index:    indexName,
        Username: os.Getenv("ELASTIC_SEARCH_USERNAME_LOGS"),
        Password: os.Getenv("ELASTIC_SEARCH_PASSWORD_LOGS"),
    })

    return client
}

之前我使用过像 Winston 这样的模块,在其中设置弹性搜索日志记录非常容易,但不知何故,我发现很少有关于如何将 Golang 日志记录与弹性搜索集成的 golang 文档

【问题讨论】:

    标签: go elasticsearch logging error-logging logrus


    【解决方案1】:

    使用elogrus,您首先创建弹性客户端,并在使用elogrus.NewAsyncElasticHook() 创建它时将其传递给elogrus 钩子。 Hook 只是包装了向 Elastic 发送消息。然后你把这个钩子添加到logruslog。每次您使用 log 记录消息时,它都会触发您的钩子并将消息(如果日志级别过滤器通过)发送到 Elastic。

    log := logrus.New()
    client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
    // ... handle err
    hook, err := elogrus.NewAsyncElasticHook(client, "localhost", logrus.DebugLevel, "testlog")
    // ... handle err
    log.Hooks.Add(hook)
    

    NewAsyncElasticHook 的签名是 (client *elastic.Client, host string, level logrus.Level, index string),其中:

    • client 是指向您在使用elastic 之前获得的Elastic.Client 的指针
    • host 是字符串,表示您从哪个主机发送日志跟踪(它是一个字符串 - 正在运行日志程序的主机的主机名)
    • level 是您希望发送消息的最大 logrus 日志级别(例如,如果您想在本地查看 DEBUG 消息但只向 Elastic 发送 ERROR 及以下消息)
    • index 是您要将来自 log 的消息添加到的 Elastic Search 索引的名称

    从这里您可以像使用 logrus 一样正常使用 log,所有消息都将传递给 Elastic。

    问题的另一部分有点棘手,并且植根于(不仅)Golang elastic 客户端节点嗅探行为。我们在聊天中对其进行了调试,并将摘要发布为我对 OP 的另一个问题的回答:Cannot connect to elastic search : no active connection found: no Elasticsearch node available

    【讨论】:

    • localhosttestlog 在对 NewAsyncElasticHook 的调用中是什么意思?你能描述一下吗?
    • 如果我在我的问题中调用prepareElasticSearchClient 方法,它只会给出错误no active connection found: no Elasticsearch node available。做。不明白为什么会这样。
    • @Amanda 更新了我的答案,详细回答了 "localhost""testlog" 问题。关于您的prepareElasticSearchClient,您的环境变量设置是否正确,您的主机可以访问弹性服务器吗?
    • @Amanda 还有,你有匹配版本的 Elastic 和 elogrus 吗?见github.com/sohlich/elogrus
    • 我的弹性搜索版本号是7.6.2,我的elogrus包版本是gopkg.in/sohlich/elogrus.v7
    猜你喜欢
    • 2023-01-19
    • 2016-12-15
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多