fix log field

pull/13/head
felixhao 6 years ago
parent 82f27d7c15
commit 50d0129461
  1. 5
      pkg/log/file.go
  2. 1
      pkg/log/pattern.go
  3. 17
      pkg/log/stdout.go
  4. 25
      pkg/log/util.go

@ -57,10 +57,7 @@ func NewFile(dir string, bufferSize, rotateSize int64, maxLogFile int) *FileHand
// Log loggint to file . // Log loggint to file .
func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) { func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) {
d := make(map[string]interface{}, 10+len(args)) d := toMap(args...)
for _, arg := range args {
d[arg.Key] = arg.Value
}
// add extra fields // add extra fields
addExtraField(ctx, d) addExtraField(ctx, d)
d[_time] = time.Now().Format(_timeFormat) d[_time] = time.Now().Format(_timeFormat)

@ -80,7 +80,6 @@ func (p *pattern) Render(w io.Writer, d map[string]interface{}) error {
for _, f := range p.funcs { for _, f := range p.funcs {
buf.WriteString(f(d)) buf.WriteString(f(d))
} }
_, err := buf.WriteTo(w) _, err := buf.WriteTo(w)
return err return err
} }

@ -7,34 +7,35 @@ import (
"time" "time"
) )
const defaultPattern = "%L %d-%T %f %M"
var _defaultStdout = NewStdout() var _defaultStdout = NewStdout()
// StdoutHandler stdout log handler // StdoutHandler stdout log handler
type StdoutHandler struct { type StdoutHandler struct {
out io.Writer out io.Writer
err io.Writer
render Render render Render
} }
// NewStdout create a stdout log handler // NewStdout create a stdout log handler
func NewStdout() *StdoutHandler { func NewStdout() *StdoutHandler {
return &StdoutHandler{ return &StdoutHandler{
out: os.Stderr, out: os.Stdout,
render: newPatternRender(defaultPattern), err: os.Stderr,
render: newPatternRender("[%D %T] [%s] %M"),
} }
} }
// Log stdout loging, only for developing env. // Log stdout loging, only for developing env.
func (h *StdoutHandler) Log(ctx context.Context, lv Level, args ...D) { func (h *StdoutHandler) Log(ctx context.Context, lv Level, args ...D) {
d := make(map[string]interface{}, 10+len(args)) d := toMap(args...)
for _, arg := range args {
d[arg.Key] = arg.Value
}
// add extra fields // add extra fields
addExtraField(ctx, d) addExtraField(ctx, d)
d[_time] = time.Now().Format(_timeFormat) d[_time] = time.Now().Format(_timeFormat)
if lv <= _infoLevel {
h.render.Render(h.out, d) h.render.Render(h.out, d)
} else {
h.render.Render(h.err, d)
}
h.out.Write([]byte("\n")) h.out.Write([]byte("\n"))
} }

@ -3,10 +3,13 @@ package log
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"runtime" "runtime"
"strconv" "strconv"
"time"
"github.com/bilibili/kratos/pkg/conf/env" "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/metadata"
"github.com/bilibili/kratos/pkg/net/trace" "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. // funcName get func name.
func funcName(skip int) (name string) { func funcName(skip int) (name string) {
if _, file, lineNo, ok := runtime.Caller(skip); ok { if _, file, lineNo, ok := runtime.Caller(skip); ok {

Loading…
Cancel
Save