使用多个后端进行日志记录

huangapple go评论86阅读模式
英文:

Go logging with multiple backend

问题

我正在使用go-logging,并尝试构建一个具有可变数量的Backends(即stderr + file + syslog + ...)的日志记录器。因此,我的程序会读取一个配置文件,并根据定义创建日志记录器。

由于SetBackend是一个可变参数函数,我以为可以使用切片作为参数,但我错了。

以下是我的代码示例:

func configureLogger(debug, file bool) {
    var backendList = []logging.Backend{}
    var format = logging.MustStringFormatter(
        `%{color}%{time:15:04:05.000} [%{level:.4s}] %{shortfunc}%{color:reset}: %{message}`,
    )

    if debug {
        consoleBackend := logging.NewLogBackend(os.Stderr, "", 0)
        consoleBackendFormatter := logging.NewBackendFormatter(consoleBackend, format)
        consoleBackendLeveled := logging.AddModuleLevel(consoleBackendFormatter)
        consoleBackendLeveled.SetLevel(logging.DEBUG, "")
        backendList = append(backendList, consoleBackendLeveled)
    }

    if file {
        f, err := os.OpenFile("file.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
        filebackend := logging.NewLogBackend(f, "", 0)
        filebackendFormatter := logging.NewBackendFormatter(filebackend, format)
        filebackendLeveled := logging.AddModuleLevel(filebackendFormatter)
        filebackendLeveled.SetLevel(logging.DEBUG, "")
        backendList = append(backendList, filebackendLeveled)
    }

    logging.SetBackend(backendList)
}

这会导致以下错误:

cannot use backendList (type []logging.Backend) as type logging.Backend in argument to logging.SetBackend:
    []logging.Backend does not implement logging.Backend (missing Log method)

我漏掉了什么?

英文:

I'm using go-logging and I'm trying to build a logger with a variable number of Backends (i.e. stderr + file + syslog + ...). So my program reads a configuration file, and create the logger according to what's defined.

As SetBackend is a variadic function, I thought I could use a slice as argument, but I was wrong.

Here is an example of my code:

func configureLogger(debug, file bool) {
    var backendList = []logging.Backend{}
    var format = logging.MustStringFormatter(
        `%{color}%{time:15:04:05.000} [%{level:.4s}] %{shortfunc}%{color:reset}: %{message}`,
    )

    if debug {
        consoleBackend := logging.NewLogBackend(os.Stderr, "", 0)
        consoleBackendFormatter := logging.NewBackendFormatter(consoleBackend, format)
        consoleBackendLeveled := logging.AddModuleLevel(consoleBackendFormatter)
        consoleBackendLeveled.SetLevel(logging.DEBUG, "")
        backendList = append(backendList, consoleBackendLeveled)
    }

    if file {
        f, err := os.OpenFile("file.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
        filebackend := logging.NewLogBackend(f, "", 0)
        filebackendFormatter := logging.NewBackendFormatter(filebackend, format)
        filebackendLeveled := logging.AddModuleLevel(filebackendFormatter)
        filebackendLeveled.SetLevel(logging.DEBUG, "")
        backendList = append(backendList, filebackendLeveled)
    }

    logging.SetBackend(backendList)
}

This end up with the following error:

cannot use backendList (type []logging.Backend) as type logging.Backend in argument to logging.SetBackend:
    []logging.Backend does not implement logging.Backend (missing Log method)

What am I missing?

答案1

得分: 2

我发现了一个好的语法。当将切片传递给可变参数函数时,你需要在切片名称后面添加...,如规范中所示:https://golang.org/ref/spec#Passing_arguments_to_..._parameters

正确的语法是:

logging.SetBackend(backendList...)
英文:

I was missing the good syntax. When passing a slice to a variadic function, you need to add ... after your slice name, as shown in the specs: https://golang.org/ref/spec#Passing_arguments_to_..._parameters

The right syntax is:

logging.SetBackend(backendList...)

huangapple
  • 本文由 发表于 2017年5月18日 22:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/44050887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定