我正在使用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)
我错过了什么?
答案 0 :(得分:2)
我错过了良好的语法。将切片传递给可变参数函数时,需要在切片名称后面添加 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row" style="background-color: grey;">
<br>
</div>
<div class="row class1">
<br>
</div>
<div class="row" style="background-color: green;">
<br>
</div>
</div>
,如规格所示:https://golang.org/ref/spec#Passing_arguments_to_..._parameters
正确的语法是:
...