From 258d5705347cc488d9833d02942a4012987fc986 Mon Sep 17 00:00:00 2001 From: Oleg Sklyar Date: Sat, 7 Oct 2017 01:22:22 +0200 Subject: [PATCH] Splitting formatter out --- std/formatter.go | 31 +++++++++++++++++++++++++++++++ std/handler.go | 48 +++++++++++++----------------------------------- 2 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 std/formatter.go diff --git a/std/formatter.go b/std/formatter.go new file mode 100644 index 0000000..c890cec --- /dev/null +++ b/std/formatter.go @@ -0,0 +1,31 @@ +// Copyright 2017 teris.io. License MIT + +package std + +import ( + "code.teris.io/go/log" + "fmt" + "github.com/fatih/color" + "time" +) + +var formatter = func(start time.Time, lvl log.Level, fields []Field, msg string) string { + timestr := color.CyanString(start.Format("02 15:04:05.000000")) + lvlstr := "" + switch lvl { + case log.Debug: + lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgYellow).Sprint("DBG")) + case log.Info: + lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgGreen).Sprint("INF")) + case log.Error: + lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgRed).Sprint("ERR")) + } + fieldstr := "" + for _, f := range fields { + if fieldstr != "" { + fieldstr += ", " + } + fieldstr += fmt.Sprintf("{%s: %v}", color.New(color.Bold).Sprint(f.Name), f.Value) + } + return fmt.Sprintf("%s%s %s %s", timestr, lvlstr, msg, fieldstr) +} diff --git a/std/handler.go b/std/handler.go index e535080..2b297e7 100644 --- a/std/handler.go +++ b/std/handler.go @@ -5,18 +5,28 @@ package std import ( "code.teris.io/go/log" "fmt" - "github.com/fatih/color" "github.com/pkg/errors" stdlog "log" "strings" "time" ) +type Formatter func(start time.Time, level log.Level, fields []Field, msg string) string + +type Field struct { + Name string + Value interface{} +} + func Use() { stdlog.SetFlags(0) log.SetFactory(&factory{}) } +func SetFormatter(f Formatter) { + formatter = f +} + type factory struct { } @@ -24,51 +34,19 @@ func (f *factory) With(k string, v interface{}) log.Logger { return &logger{lvl: log.Unset, fields: []Field{{Name: k, Value: v}}} } -type Formatter func(start time.Time, level log.Level, fields []Field, msg string) string - -func SetFormatter(f Formatter) { - formatter = f -} - -var formatter = func(start time.Time, lvl log.Level, fields []Field, msg string) string { - timestr := color.CyanString(start.Format("02 15:04:05.000000")) - lvlstr := "" - switch lvl { - case log.Debug: - lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgYellow).Sprint("DBG")) - case log.Info: - lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgGreen).Sprint("INF")) - case log.Error: - lvlstr = fmt.Sprintf(" %s", color.New(color.Bold, color.FgRed).Sprint("ERR")) - } - fieldstr := "" - for _, f := range fields { - if fieldstr != "" { - fieldstr += ", " - } - fieldstr += fmt.Sprintf("{%s: %v}", color.New(color.Bold).Sprint(f.Name), f.Value) - } - return fmt.Sprintf("%s%s %s %s", timestr, lvlstr, msg, fieldstr) -} - type logger struct { lvl log.Level fields []Field } -type Field struct { - Name string - Value interface{} +func (l *logger) With(k string, v interface{}) log.Logger { + return &logger{lvl: l.lvl, fields: append([]Field{{Name: k, Value: v}}, l.fields...)} } type stackTracer interface { StackTrace() errors.StackTrace } -func (l *logger) With(k string, v interface{}) log.Logger { - return &logger{lvl: l.lvl, fields: append([]Field{{Name: k, Value: v}}, l.fields...)} -} - func (l *logger) WithLevel(lvl log.Level) log.Logger { return &logger{lvl: lvl, fields: append([]Field{}, l.fields...)} }