|
|
@ -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) |
|
|
|
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")) |
|
|
|
h.out.Write([]byte("\n")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|