mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Backend plugins: Log wrapper args formatting (#20514)
Backend plugins is recommended to use hclog with json formatting to get proper log output in grafana server log. Old hclog-wrapper.go that I deleted while back is still in the repo so deletes that.
This commit is contained in:
parent
ec18e2bfc3
commit
58b7958952
@ -13,20 +13,41 @@ type logWrapper struct {
|
||||
Logger glog.Logger
|
||||
}
|
||||
|
||||
func formatArgs(args ...interface{}) []interface{} {
|
||||
if len(args) == 0 || len(args)%2 != 0 {
|
||||
return args
|
||||
}
|
||||
|
||||
res := []interface{}{}
|
||||
|
||||
for n := 0; n < len(args); n = n + 2 {
|
||||
key := args[n]
|
||||
|
||||
if stringKey, ok := key.(string); ok && stringKey == "timestamp" {
|
||||
continue
|
||||
}
|
||||
|
||||
res = append(res, key)
|
||||
res = append(res, args[n+1])
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (lw logWrapper) Trace(msg string, args ...interface{}) {
|
||||
lw.Logger.Debug(msg, args...)
|
||||
lw.Logger.Debug(msg, formatArgs(args...)...)
|
||||
}
|
||||
func (lw logWrapper) Debug(msg string, args ...interface{}) {
|
||||
lw.Logger.Debug(msg, args...)
|
||||
lw.Logger.Debug(msg, formatArgs(args...)...)
|
||||
}
|
||||
func (lw logWrapper) Info(msg string, args ...interface{}) {
|
||||
lw.Logger.Info(msg, args...)
|
||||
lw.Logger.Info(msg, formatArgs(args...)...)
|
||||
}
|
||||
func (lw logWrapper) Warn(msg string, args ...interface{}) {
|
||||
lw.Logger.Warn(msg, args...)
|
||||
lw.Logger.Warn(msg, formatArgs(args...)...)
|
||||
}
|
||||
func (lw logWrapper) Error(msg string, args ...interface{}) {
|
||||
lw.Logger.Error(msg, args...)
|
||||
lw.Logger.Error(msg, formatArgs(args...)...)
|
||||
}
|
||||
|
||||
func (lw logWrapper) IsTrace() bool { return true }
|
||||
|
29
pkg/plugins/backendplugin/log_wrapper_test.go
Normal file
29
pkg/plugins/backendplugin/log_wrapper_test.go
Normal file
@ -0,0 +1,29 @@
|
||||
package backendplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogWrapper(t *testing.T) {
|
||||
tcs := []struct {
|
||||
args []interface{}
|
||||
expectedResult []interface{}
|
||||
}{
|
||||
{args: []interface{}{}, expectedResult: []interface{}{}},
|
||||
{args: []interface{}{"1", "2", "3"}, expectedResult: []interface{}{"1", "2", "3"}},
|
||||
{args: []interface{}{"1", "2"}, expectedResult: []interface{}{"1", "2"}},
|
||||
{args: []interface{}{"1", "2", "timestamp", time.Now()}, expectedResult: []interface{}{"1", "2"}},
|
||||
{args: []interface{}{"1", "2", "timestamp", time.Now(), "3", "4"}, expectedResult: []interface{}{"1", "2", "3", "4"}},
|
||||
}
|
||||
|
||||
for i, tc := range tcs {
|
||||
t.Run(fmt.Sprintf("formatArgs testcase %d", i), func(t *testing.T) {
|
||||
res := formatArgs(tc.args...)
|
||||
assert.Exactly(t, tc.expectedResult, res)
|
||||
})
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
glog "github.com/grafana/grafana/pkg/infra/log"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
)
|
||||
|
||||
type LogWrapper struct {
|
||||
Logger glog.Logger
|
||||
}
|
||||
|
||||
func (lw LogWrapper) Trace(msg string, args ...interface{}) {
|
||||
lw.Logger.Debug(msg, args...)
|
||||
}
|
||||
func (lw LogWrapper) Debug(msg string, args ...interface{}) {
|
||||
lw.Logger.Debug(msg, args...)
|
||||
}
|
||||
func (lw LogWrapper) Info(msg string, args ...interface{}) {
|
||||
lw.Logger.Info(msg, args...)
|
||||
}
|
||||
func (lw LogWrapper) Warn(msg string, args ...interface{}) {
|
||||
lw.Logger.Warn(msg, args...)
|
||||
}
|
||||
func (lw LogWrapper) Error(msg string, args ...interface{}) {
|
||||
lw.Logger.Error(msg, args...)
|
||||
}
|
||||
|
||||
func (lw LogWrapper) IsTrace() bool { return true }
|
||||
func (lw LogWrapper) IsDebug() bool { return true }
|
||||
func (lw LogWrapper) IsInfo() bool { return true }
|
||||
func (lw LogWrapper) IsWarn() bool { return true }
|
||||
func (lw LogWrapper) IsError() bool { return true }
|
||||
|
||||
func (lw LogWrapper) With(args ...interface{}) hclog.Logger {
|
||||
return LogWrapper{Logger: lw.Logger.New(args...)}
|
||||
}
|
||||
func (lw LogWrapper) Named(name string) hclog.Logger {
|
||||
return LogWrapper{Logger: lw.Logger.New()}
|
||||
}
|
||||
|
||||
func (lw LogWrapper) ResetNamed(name string) hclog.Logger {
|
||||
return LogWrapper{Logger: lw.Logger.New()}
|
||||
}
|
||||
|
||||
func (lw LogWrapper) StandardLogger(ops *hclog.StandardLoggerOptions) *log.Logger {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lw LogWrapper) SetLevel(level hclog.Level) {}
|
||||
|
||||
// Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
|
||||
func (lw LogWrapper) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
|
||||
return ioutil.Discard
|
||||
}
|
@ -10,5 +10,6 @@ require (
|
||||
github.com/unknwon/com v1.0.1 // indirect
|
||||
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect
|
||||
github.com/urfave/cli v1.20.0 // indirect
|
||||
golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97 // indirect
|
||||
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
|
||||
)
|
||||
|
@ -364,6 +364,8 @@ golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678/go.mod h1:b+2E5dAYhXwXZwtn
|
||||
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191015150414-f936694f27bf h1:60bAcFbqQ3hW1i1JqrAcVJhY4qUUJhgBAimCcAKNvDE=
|
||||
golang.org/x/tools v0.0.0-20191015150414-f936694f27bf/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97 h1:u8hSFDulpuhoY0GHMbG6Rp23PzphtTnZrQX3dOvEae0=
|
||||
golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
|
Loading…
Reference in New Issue
Block a user