diff --git a/pkg/log/file.go b/pkg/log/file.go index 3fcc906e9..a99a8515d 100644 --- a/pkg/log/file.go +++ b/pkg/log/file.go @@ -57,10 +57,7 @@ func NewFile(dir string, bufferSize, rotateSize int64, maxLogFile int) *FileHand // Log loggint to file . func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) { - d := make(map[string]interface{}, 10+len(args)) - for _, arg := range args { - d[arg.Key] = arg.Value - } + d := toMap(args...) // add extra fields addExtraField(ctx, d) d[_time] = time.Now().Format(_timeFormat) diff --git a/pkg/log/pattern.go b/pkg/log/pattern.go index 14a9853b5..89609d6b6 100644 --- a/pkg/log/pattern.go +++ b/pkg/log/pattern.go @@ -80,7 +80,6 @@ func (p *pattern) Render(w io.Writer, d map[string]interface{}) error { for _, f := range p.funcs { buf.WriteString(f(d)) } - _, err := buf.WriteTo(w) return err } diff --git a/pkg/log/stdout.go b/pkg/log/stdout.go index ef4cb037d..9d7746cd6 100644 --- a/pkg/log/stdout.go +++ b/pkg/log/stdout.go @@ -7,34 +7,35 @@ import ( "time" ) -const defaultPattern = "%L %d-%T %f %M" - var _defaultStdout = NewStdout() // StdoutHandler stdout log handler type StdoutHandler struct { out io.Writer + err io.Writer render Render } // NewStdout create a stdout log handler func NewStdout() *StdoutHandler { return &StdoutHandler{ - out: os.Stderr, - render: newPatternRender(defaultPattern), + out: os.Stdout, + err: os.Stderr, + render: newPatternRender("[%D %T] [%s] %M"), } } // Log stdout loging, only for developing env. func (h *StdoutHandler) Log(ctx context.Context, lv Level, args ...D) { - d := make(map[string]interface{}, 10+len(args)) - for _, arg := range args { - d[arg.Key] = arg.Value - } + d := toMap(args...) // add extra fields addExtraField(ctx, d) d[_time] = time.Now().Format(_timeFormat) - h.render.Render(h.out, d) + if lv <= _infoLevel { + h.render.Render(h.out, d) + } else { + h.render.Render(h.err, d) + } h.out.Write([]byte("\n")) } diff --git a/pkg/log/util.go b/pkg/log/util.go index da9e26d25..724c5f064 100644 --- a/pkg/log/util.go +++ b/pkg/log/util.go @@ -3,10 +3,13 @@ package log import ( "context" "fmt" + "math" "runtime" "strconv" + "time" "github.com/bilibili/kratos/pkg/conf/env" + "github.com/bilibili/kratos/pkg/log/internal/core" "github.com/bilibili/kratos/pkg/net/metadata" "github.com/bilibili/kratos/pkg/net/trace" ) @@ -37,6 +40,28 @@ func addExtraField(ctx context.Context, fields map[string]interface{}) { } } +// toMap convert D slice to map[string]interface{} for legacy file and stdout. +func toMap(args ...D) map[string]interface{} { + d := make(map[string]interface{}, 10+len(args)) + for _, arg := range args { + switch arg.Type { + case core.UintType, core.Uint64Type, core.IntTpye, core.Int64Type: + d[arg.Key] = arg.Int64Val + case core.StringType: + d[arg.Key] = arg.StringVal + case core.Float32Type: + d[arg.Key] = math.Float32frombits(uint32(arg.Int64Val)) + case core.Float64Type: + d[arg.Key] = math.Float64frombits(uint64(arg.Int64Val)) + case core.DurationType: + d[arg.Key] = time.Duration(arg.Int64Val) + default: + d[arg.Key] = arg.Value + } + } + return d +} + // funcName get func name. func funcName(skip int) (name string) { if _, file, lineNo, ok := runtime.Caller(skip); ok {