Skip to content

Commit

Permalink
Improve logger signature error handling (#64)
Browse files Browse the repository at this point in the history
⚠️ Breaking change
Signature change
No more context mandatory
No more return error (LogInterface, LoggerInterface) use errorHandler instead
Field now contain his name as attribute

We replace
formatter.EnableColor by formatter.WithColor
formatter.DisplayContext by formatter.WithContext
  • Loading branch information
instabledesign authored Feb 10, 2020
1 parent 2d0f4ec commit 073c9df
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 443 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (

func main(){
// logger will print on STDOUT with default line format
l := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter(formatter.DisplayContext)))
l := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter(formatter.WithContext(true))))

l.Debug("Go debug informations", logger.Ctx("go_os", runtime.GOOS).Add("go_arch", runtime.GOARCH))
l.Debug("Go debug informations", logger.String("go_os", runtime.GOOS), logger.String("go_arch", runtime.GOARCH))
// <debug> MyExample message {"go_arch":"amd64","go_os":"darwin"}

l.Info("Another", nil)
l.Info("Another")
//<info> Another
}
```
Expand All @@ -47,22 +47,22 @@ This library expose some quite simple interfaces.
Simplest one
```go
type LogInterface interface {
Log(message string, level Level, context *Context) error
Log(message string, level Level, field ...Field)
}
```

The friendly one
```go
type LoggerInterface interface {
LogInterface
Debug(message string, context *Context) error
Info(message string, context *Context) error
Notice(message string, context *Context) error
Warning(message string, context *Context) error
Error(message string, context *Context) error
Critical(message string, context *Context) error
Alert(message string, context *Context) error
Emergency(message string, context *Context) error
Debug(message string, field ...Field)
Info(message string, field ...Field)
Notice(message string, field ...Field)
Warning(message string, field ...Field)
Error(message string, field ...Field)
Critical(message string, field ...Field)
Alert(message string, field ...Field)
Emergency(message string, field ...Field)
}
```

Expand Down Expand Up @@ -101,6 +101,7 @@ Available middleware:
- **filter** _it will permit to filter log entry_ level filter are available or you can use your own callback filter
- **placeholder** _it will replace log message placeholder with contextual value_
- **recover** _it will convert handler panic to error_
- **timestamp** _it will add timestamp to log context_

## Writers

Expand Down
12 changes: 6 additions & 6 deletions benchmarks/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func BenchmarkNopLogger(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = myLogger.Info("This log message go anywhere.", nil)
myLogger.Info("This log message go anywhere.")
}
}

Expand All @@ -35,7 +35,7 @@ func BenchmarkLoggerLineFormatter(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = myLogger.Info("Log example", nil)
myLogger.Info("Log example")
}
}

Expand All @@ -48,7 +48,7 @@ func BenchmarkLoggerJsonFormatter(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = myLogger.Info("Log example", nil)
myLogger.Info("Log example")
}
}

Expand All @@ -61,8 +61,8 @@ func BenchmarkLoggerMinLevelFilterHandler(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = myLogger.Debug("Log example", nil)
_ = myLogger.Info("Log example", nil)
myLogger.Debug("Log example")
myLogger.Info("Log example")
}
}

Expand All @@ -76,6 +76,6 @@ func BenchmarkLoggerGroupHandler(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = myLogger.Debug("Log example", nil)
myLogger.Debug("Log example")
}
}
26 changes: 14 additions & 12 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ type Context map[string]Field

// Merge and overwrite context data with the given one
func (c *Context) Merge(context Context) *Context {
for name, field := range context {
c.Set(name, field)
for _, field := range context {
c.SetField(field)
}
return c
}

// Set will add a new context field
func (c *Context) Set(name string, value Field) *Context {
(*c)[name] = value
// SetField will add a new context field
func (c *Context) SetField(fields ...Field) *Context {
for _, field := range fields {
(*c)[field.Name] = field
}
return c
}

Expand All @@ -39,22 +41,22 @@ func (c *Context) Get(name string, defaultField *Field) Field {

// Add will guess and add value to the context
func (c *Context) Add(name string, value interface{}) *Context {
return c.Set(name, Any(value))
return c.SetField(Any(name, value))
}

// Skip will add skip field to context
func (c *Context) Skip(name string, value string) *Context {
return c.Set(name, Skip(value))
return c.SetField(Skip(name, value))
}

// Binary will add binary field to context
func (c *Context) Binary(name string, value []byte) *Context {
return c.Set(name, Binary(value))
return c.SetField(Binary(name, value))
}

// ByteString will add byteString field to context
func (c *Context) ByteString(name string, value []byte) *Context {
return c.Set(name, ByteString(value))
return c.SetField(ByteString(name, value))
}

func (c *Context) stringTo(builder *strings.Builder) *Context {
Expand Down Expand Up @@ -102,7 +104,7 @@ func Ctx(name string, value interface{}) *Context {
return NewContext().Add(name, value)
}

// NewContext will create a new context
func NewContext() *Context {
return &Context{}
// NewContext will create a new context with optional fields
func NewContext(fields ...Field) *Context {
return (&Context{}).SetField(fields...)
}
22 changes: 6 additions & 16 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func TestContext_Merge(t *testing.T) {
context1 := logger.Context(map[string]logger.Field{
"my_key": logger.Field{Value: "my_value"},
"key_gonna_be_overwrited": logger.Field{Value: "my_initial_value"},
"my_key": logger.Field{Name: "my_key", Value: "my_value"},
"key_gonna_be_overwrited": logger.Field{Name: "key_gonna_be_overwrited", Value: "my_initial_value"},
})
context2 := logger.Context(map[string]logger.Field{
"key_gonna_be_overwrited": logger.Field{Value: 2},
"new_key": logger.Field{Value: "my_new_value"},
"key_gonna_be_overwrited": logger.Field{Name: "key_gonna_be_overwrited", Value: 2},
"new_key": logger.Field{Name: "new_key", Value: "my_new_value"},
})

(&context1).Merge(context2)
Expand All @@ -25,19 +25,9 @@ func TestContext_Merge(t *testing.T) {
assert.Equal(t, "my_new_value", context1["new_key"].Value)
}

func TestContext_Set(t *testing.T) {
context1 := logger.Context(map[string]logger.Field{
"my_key": logger.Field{Value: "my_value"},
})

context1.Set("my_key", logger.Field{Value: 2})

assert.Equal(t, 2, context1["my_key"].Value)
}

func TestContext_Has(t *testing.T) {
context1 := logger.Context(map[string]logger.Field{
"my_key": logger.Field{Value: "my_value"},
"my_key": logger.Field{Name: "my_key", Value: "my_value"},
})

assert.True(t, context1.Has("my_key"))
Expand All @@ -46,7 +36,7 @@ func TestContext_Has(t *testing.T) {

func TestContext_Get(t *testing.T) {
context := logger.Context(map[string]logger.Field{
"my_key": logger.Field{Value: "my_value"},
"my_key": logger.Field{Name: "my_key", Value: "my_value"},
})

defaultValue := &logger.Field{}
Expand Down
Loading

0 comments on commit 073c9df

Please sign in to comment.