Skip to content

Commit

Permalink
Splitting formatter out
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Sklyar committed Oct 6, 2017
1 parent 0f610a1 commit 258d570
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
31 changes: 31 additions & 0 deletions std/formatter.go
Original file line number Diff line number Diff line change
@@ -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)
}
48 changes: 13 additions & 35 deletions std/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,48 @@ 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 {
}

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...)}
}
Expand Down

0 comments on commit 258d570

Please sign in to comment.