Golang logrus - 如何进行集中配置? [英] Golang logrus - how to do a centralized configuration?

查看:1621
本文介绍了Golang logrus - 如何进行集中配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Go应用中使用了logrus。我相信这个问题也适用于任何其他日志包(它不提供基于外部文件的配置)。



logrus提供了设置各种配置的功能,例如。 SetOutput,SetLevel等。



像其他应用程序一样,我需要从多个源文件/包进行日志记录,似乎您需要在每个文件中使用logrus设置这些选项。

有什么方法可以在中央位置某个地方设置这些选项,以便在整个应用程序中共享。这样,如果我必须改变日志记录级别,我可以在一个地方做到这一点,并适用于应用程序的所有组件。

p>您无需在每个文件中使用Logrus设置这些选项。



您可以将Logrus导入为 log

 导入日志github.com/Sirupsen/logrus

然后像 log.SetOutput()这样的函数只是函数并修改了全局记录器并应用于任何文件包括此导入。



您可以创建一个全局 log 变量包:

  var log = logrus.New()



然后像 log.SetOutput()这样的函数是方法并修改了你的包的全局。如果你的程序中有多个软件包,这是很尴尬的,因为他们每个人都有不同的设置(但也许这对一些用例很有用)的记录​​器。我也不喜欢这种方法,因为它会混淆 goimports (它会在您的导入列表中插入 log >) 。



或者你可以创建自己的包装器(这就是我所做的)。我有自己的 log 包,它有自己的 logger var:

  var logger = logrus.New()

然后我使用顶级函数来包装Logrus:

  func Info(args ... interface {}){
logger.Info(args ...)
}

func Debug(args ... interface {}){
logger.Debug(args ...)
}

这有些乏味,但允许我添加特定于我的程序的函数:

  func WithConn(conn net.Conn)* logrus.Entry {
var addr string =unknown
if conn!= nil {
addr = conn.RemoteAddr()。String()
}
return logger.WithField(addr,addr)
}

func WithRequest(req * http.Request)* logrus.Entry {
return logger.WithFields(RequestFields(req))
}

所以我可以这样做:

  log.WithConn (c)中.INFO(连接)

(我计划在将来包装 logrus.Entry 转换为我自己的类型,这样我可以更好地链接它们;目前我无法调用 log.WithConn(c).WithRequest(r).Error(...),因为我无法添加 WithRequest () logrus.Entry 。)


I am using logrus in a Go app. I believe this question is applicable to any other logging package (which doesn't offer external file based configuration) as well.

logrus provides functions to setup various configuration, e.g. SetOutput, SetLevel etc.

Like any other application I need to do logging from multiple source files/packages, it seems you need to setup these options in each file with logrus.

Is there any way to setup these options once somewhere in a central place to be shared all over the application. That way if I have to make logging level change I can do it in one place and applies to all the components of the app.

解决方案

You don't need to set these options in each file with Logrus.

You can import Logrus as log:

import log "github.com/Sirupsen/logrus"

Then functions like log.SetOutput() are just functions and modify the global logger and apply to any file that includes this import.

You can create a package global log variable:

var log = logrus.New()

Then functions like log.SetOutput() are methods and modify your package global. This is awkward IMO if you have multiple packages in your program, because each of them has a different logger with different settings (but maybe that's good for some use cases). I also don't like this approach because it confuses goimports (which will want to insert log into your imports list).

Or you can create your own wrapper (which is what I do). I have my own log package with its own logger var:

var logger = logrus.New()

Then I make top-level functions to wrap Logrus:

func Info(args ...interface{}) {
    logger.Info(args...)
}

func Debug(args ...interface{}) {
    logger.Debug(args...)
}

This is slightly tedious, but allows me to add functions specific to my program:

func WithConn(conn net.Conn) *logrus.Entry {
    var addr string = "unknown"
    if conn != nil {
        addr = conn.RemoteAddr().String()
    }
    return logger.WithField("addr", addr)
}

func WithRequest(req *http.Request) *logrus.Entry {
    return logger.WithFields(RequestFields(req))
}

So I can then do things like:

log.WithConn(c).Info("Connected")

(I plan in the future to wrap logrus.Entry into my own type so that I can chain these better; currently I can't call log.WithConn(c).WithRequest(r).Error(...) because I can't add WithRequest() to logrus.Entry.)

这篇关于Golang logrus - 如何进行集中配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆