mirror of
https://github.com/grafana/grafana.git
synced 2025-02-10 07:35:45 -06:00
Log: Fix text logging for unsupported types (#51306)
* Fix text log for unsupported types * Apply suggestions from code review Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
parent
1f49296f8f
commit
84e2e1ec8e
@ -20,6 +20,7 @@ import (
|
||||
"github.com/go-stack/stack"
|
||||
"github.com/grafana/grafana/pkg/infra/log/level"
|
||||
"github.com/grafana/grafana/pkg/infra/log/term"
|
||||
"github.com/grafana/grafana/pkg/infra/log/text"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/mattn/go-isatty"
|
||||
"gopkg.in/ini.v1"
|
||||
@ -325,11 +326,11 @@ func getLogFormat(format string) Formatedlogger {
|
||||
}
|
||||
}
|
||||
return func(w io.Writer) gokitlog.Logger {
|
||||
return gokitlog.NewLogfmtLogger(w)
|
||||
return text.NewTextLogger(w)
|
||||
}
|
||||
case "text":
|
||||
return func(w io.Writer) gokitlog.Logger {
|
||||
return gokitlog.NewLogfmtLogger(w)
|
||||
return text.NewTextLogger(w)
|
||||
}
|
||||
case "json":
|
||||
return func(w io.Writer) gokitlog.Logger {
|
||||
@ -337,7 +338,7 @@ func getLogFormat(format string) Formatedlogger {
|
||||
}
|
||||
default:
|
||||
return func(w io.Writer) gokitlog.Logger {
|
||||
return gokitlog.NewLogfmtLogger(w)
|
||||
return text.NewTextLogger(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
39
pkg/infra/log/text/text_logger.go
Normal file
39
pkg/infra/log/text/text_logger.go
Normal file
@ -0,0 +1,39 @@
|
||||
package text
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
gokitlog "github.com/go-kit/kit/log"
|
||||
)
|
||||
|
||||
type textLogger struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
// NewTextLogger similar to gokitlog.NewLogfmtLogger
|
||||
// but converts unsupported types to string
|
||||
func NewTextLogger(w io.Writer) gokitlog.Logger {
|
||||
return &textLogger{w}
|
||||
}
|
||||
|
||||
func (l textLogger) Log(keyvals ...interface{}) error {
|
||||
for i, val := range keyvals {
|
||||
switch val.(type) {
|
||||
case nil, string, []byte, encoding.TextMarshaler, error, fmt.Stringer: // supported natively by gokit.
|
||||
default:
|
||||
switch reflect.TypeOf(val).Kind() {
|
||||
case reflect.Array, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Struct:
|
||||
keyvals[i] = fmt.Sprintf("%+v", val)
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
ll := gokitlog.NewLogfmtLogger(l.w)
|
||||
if err := ll.Log(keyvals...); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user