Skip to content

Commit

Permalink
minor fixes (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Sitbon authored Feb 21, 2020
1 parent 4762659 commit fdfc1e8
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 138 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main(){
// logger will print on STDOUT with default line format
l := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter(formatter.WithContext(true))))

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

l.Info("Another")
Expand Down Expand Up @@ -97,11 +97,11 @@ Available formatters:

## Middlewares

The middleware are handler decorator/wrapper. It will allow you to do some process arround child handler
The middleware are handler decorator/wrapper. It will allow you to do some process around child handler

Available middleware:
- **caller** _it will add caller file/line to context_ `<file:/my/example/path/file> <line:31>`
- **context** _it permit to have a default context value_ usefull when you want to set global context value
- **context** _it permit to have a default context value_ useful when you want to set global context value
- **error** _it will print provided handler error_ (can be configure to silent it)
- **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_
Expand All @@ -126,7 +126,7 @@ Available writer:
- socket server
- https://craig.is/writing/chrome-logger
- fingercross
- grpc / protobuff
- grpc / protobuf
- curl
- Mail
- Slack
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func Benchmark_Context_Typed(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
logger.NewContext().
Binary("my binary", []byte{1, 2, 3}). // 2 allocs
ByteString("my byte string", []byte{1, 2, 3}) // 2 allocs
Binary("my binary", []byte{1, 2, 3}). // 2 allocations
ByteString("my byte string", []byte{1, 2, 3}) // 2 allocations
}
}

Expand All @@ -21,7 +21,7 @@ func Benchmark_Context_Add(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
logger.NewContext().
Add("my binary", []byte{1, 2, 3}). // 3 allocs
Add("my byte string", []byte{1, 2, 3}) // 3 allocs
Add("my binary", []byte{1, 2, 3}). // 3 allocations
Add("my byte string", []byte{1, 2, 3}) // 3 allocations
}
}
2 changes: 1 addition & 1 deletion benchmarks/handler/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func BenchmarkGroupHandler(b *testing.B) {

b.ResetTimer()
for n := 0; n < b.N; n++ {
groupHandler(logger.Entry{Message: "This log message goes nowhere.", Level: logger.InfoLevel})
_ = groupHandler(logger.Entry{Message: "This log message goes nowhere.", Level: logger.InfoLevel})
}
}
4 changes: 2 additions & 2 deletions benchmarks/handler/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type NopWriter struct{}

func (w *NopWriter) Write(p []byte) (n int, err error) {
func (w *NopWriter) Write(_ []byte) (n int, err error) {
return 0, nil
}

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

b.ResetTimer()
for n := 0; n < b.N; n++ {
streamHandler(logger.Entry{Message: "This log message goes nowhere.", Level: logger.InfoLevel})
_ = streamHandler(logger.Entry{Message: "This log message goes nowhere.", Level: logger.InfoLevel})
}
}
2 changes: 1 addition & 1 deletion benchmarks/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type NopWriter struct{}

func (w *NopWriter) Write(p []byte) (n int, err error) {
func (w *NopWriter) Write(_ []byte) (n int, err error) {
return 0, nil
}

Expand Down
14 changes: 7 additions & 7 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (

func TestContext_Merge(t *testing.T) {
context1 := logger.Context(map[string]logger.Field{
"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"},
"my_key": {Name: "my_key", Value: "my_value"},
"key_gonna_be_overwritten": {Name: "key_gonna_be_overwritten", Value: "my_initial_value"},
})
context2 := logger.Context(map[string]logger.Field{
"key_gonna_be_overwrited": logger.Field{Name: "key_gonna_be_overwrited", Value: 2},
"new_key": logger.Field{Name: "new_key", Value: "my_new_value"},
"key_gonna_be_overwritten": {Name: "key_gonna_be_overwritten", Value: 2},
"new_key": {Name: "new_key", Value: "my_new_value"},
})

(&context1).Merge(context2)

assert.Equal(t, "my_value", context1["my_key"].Value)
assert.Equal(t, 2, context1["key_gonna_be_overwrited"].Value)
assert.Equal(t, 2, context1["key_gonna_be_overwritten"].Value)
assert.Equal(t, "my_new_value", context1["new_key"].Value)
}

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

assert.True(t, context1.Has("my_key"))
Expand All @@ -36,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{Name: "my_key", Value: "my_value"},
"my_key": {Name: "my_key", Value: "my_value"},
})

defaultValue := &logger.Field{}
Expand Down
2 changes: 1 addition & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package logger
import "strings"

// Entry represents a log in its entirety
// it is composed of a level, a message and context
// it is composed of a level, a message and a context
type Entry struct {
Message string
Level Level
Expand Down
24 changes: 12 additions & 12 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const (
StringerType
// ReflectType indicates that the field carries an interface{}, which should be serialized using reflection.
ReflectType
// @TODO ArrayMarshalerType
// @TODO ObjectMarshalerType
// @TODO ArrayMarshallerType
// @TODO ObjectMarshallerType
)

// Field represents a contextual information
Expand All @@ -62,23 +62,23 @@ func (f *Field) String() string {
}
return "false"
case Int8Type:
return strconv.Itoa(int(f.Value.(int8)))
return strconv.FormatInt(int64(f.Value.(int8)), 10)
case Int16Type:
return strconv.Itoa(int(f.Value.(int16)))
return strconv.FormatInt(int64(f.Value.(int16)), 10)
case Int32Type:
return strconv.Itoa(int(f.Value.(int32)))
return strconv.FormatInt(int64(f.Value.(int32)), 10)
case Int64Type:
return strconv.Itoa(int(f.Value.(int64)))
return strconv.FormatInt(f.Value.(int64), 10)
case Uint8Type:
return strconv.Itoa(int(f.Value.(uint8)))
return strconv.FormatUint(uint64(f.Value.(uint8)), 10)
case Uint16Type:
return strconv.Itoa(int(f.Value.(uint16)))
return strconv.FormatUint(uint64(f.Value.(uint16)), 10)
case Uint32Type:
return strconv.Itoa(int(f.Value.(uint32)))
return strconv.FormatUint(uint64(f.Value.(uint32)), 10)
case Uint64Type:
return strconv.Itoa(int(f.Value.(uint64)))
return strconv.FormatUint(f.Value.(uint64), 10)
case UintptrType:
return strconv.Itoa(int(f.Value.(uintptr)))
return strconv.FormatUint(uint64(f.Value.(uintptr)), 10)
case Float32Type:
return strconv.FormatFloat(float64(f.Value.(float32)), 'g', 10, 64)
case Float64Type:
Expand Down Expand Up @@ -236,7 +236,7 @@ func Reflect(name string, value interface{}) Field {
return Field{Name: name, Type: ReflectType, Value: value}
}

// Any will guess and create Field with givan value
// Any will guess and create Field for given value
func Any(name string, value interface{}) Field {
switch val := value.(type) {
case bool:
Expand Down
56 changes: 27 additions & 29 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s MyStringer) String() string {

func TestField(t *testing.T) {
now := time.Now()
error := errors.New("my_value")
err := errors.New("my_value")

tests := []struct {
name string
Expand All @@ -47,7 +47,7 @@ func TestField(t *testing.T) {
{field: logger.String("String field", "my_value"), expectedValue: "my_value", expectedType: logger.StringType},
{field: logger.Binary("Binary field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.BinaryType},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.ByteStringType},
{field: logger.Error("Error field", error), expectedValue: error, expectedType: logger.ErrorType},
{field: logger.Error("Error field", err), expectedValue: err, expectedType: logger.ErrorType},
{field: logger.Time("Time field", now), expectedValue: now, expectedType: logger.TimeType},
{field: logger.Duration("Duration field", 5 * time.Second), expectedValue: 5 * time.Second, expectedType: logger.DurationType},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedValue: MyStringer{}, expectedType: logger.StringerType},
Expand All @@ -67,32 +67,30 @@ func TestField_Any(t *testing.T) {
name string
value interface{}
expectedType logger.FieldType
expectedMallocs uint64
expectedTotalAlloc uint64
}{
{name: "my bool", value: true, expectedType: logger.BoolType, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my bool", value: false, expectedType: logger.BoolType, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my int", value: 123, expectedType: logger.Int64Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my int8", value: int8(123), expectedType: logger.Int8Type, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my int16", value: int16(123), expectedType: logger.Int16Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my int32", value: int32(123), expectedType: logger.Int32Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my int64", value: int64(123), expectedType: logger.Int64Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my uint8", value: uint8(123), expectedType: logger.Uint8Type, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my uint16", value: uint16(123), expectedType: logger.Uint16Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my uint32", value: uint32(123), expectedType: logger.Uint32Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my uint64", value: uint64(123), expectedType: logger.Uint64Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my uintptr", value: uintptr(123), expectedType: logger.UintptrType, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my float32", value: float32(123), expectedType: logger.Float32Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my float64", value: float64(123), expectedType: logger.Float64Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my complex64", value: complex64(123), expectedType: logger.Complex64Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my complex128", value: complex128(123), expectedType: logger.Complex128Type, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my string", value: "my string", expectedType: logger.StringType, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my binary", value: []byte{1, 2, 3}, expectedType: logger.BinaryType, expectedMallocs: 1, expectedTotalAlloc: 32},
{name: "my error", value: errors.New("my error message"), expectedType: logger.ErrorType, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my time", value: time.Now(), expectedType: logger.TimeType, expectedMallocs: 1, expectedTotalAlloc: 32},
{name: "my duration", value: time.Second, expectedType: logger.DurationType, expectedMallocs: 1, expectedTotalAlloc: 16},
{name: "my stringer", value: MyStringer{}, expectedType: logger.StringerType, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my reflect", value: struct{}{}, expectedType: logger.ReflectType, expectedMallocs: 0, expectedTotalAlloc: 0},
{name: "my bool", value: true, expectedType: logger.BoolType},
{name: "my bool", value: false, expectedType: logger.BoolType},
{name: "my int", value: 123, expectedType: logger.Int64Type},
{name: "my int8", value: int8(123), expectedType: logger.Int8Type},
{name: "my int16", value: int16(123), expectedType: logger.Int16Type},
{name: "my int32", value: int32(123), expectedType: logger.Int32Type},
{name: "my int64", value: int64(123), expectedType: logger.Int64Type},
{name: "my uint8", value: uint8(123), expectedType: logger.Uint8Type},
{name: "my uint16", value: uint16(123), expectedType: logger.Uint16Type},
{name: "my uint32", value: uint32(123), expectedType: logger.Uint32Type},
{name: "my uint64", value: uint64(123), expectedType: logger.Uint64Type},
{name: "my uintptr", value: uintptr(123), expectedType: logger.UintptrType},
{name: "my float32", value: float32(123), expectedType: logger.Float32Type},
{name: "my float64", value: float64(123), expectedType: logger.Float64Type},
{name: "my complex64", value: complex64(123), expectedType: logger.Complex64Type},
{name: "my complex128", value: complex128(123), expectedType: logger.Complex128Type},
{name: "my strings", value: "my strings", expectedType: logger.StringType},
{name: "my binary", value: []byte{1, 2, 3}, expectedType: logger.BinaryType},
{name: "my error", value: errors.New("my error message"), expectedType: logger.ErrorType},
{name: "my time", value: time.Now(), expectedType: logger.TimeType},
{name: "my duration", value: time.Second, expectedType: logger.DurationType},
{name: "my stringer", value: MyStringer{}, expectedType: logger.StringerType},
{name: "my reflect", value: struct{}{}, expectedType: logger.ReflectType},
}

for _, tt := range tests {
Expand All @@ -105,7 +103,7 @@ func TestField_Any(t *testing.T) {

func TestField_String(t *testing.T) {
now := time.Now()
error := errors.New("my_error_value")
err := errors.New("my_error_value")

tests := []struct {
name string
Expand All @@ -131,7 +129,7 @@ func TestField_String(t *testing.T) {
{field: logger.String("String field", "my_value"), expectedString: "my_value"},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedString: "\x01\x02\x03"},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedString: "my_value"},
{field: logger.Error("Error field", error), expectedString: "my_error_value"},
{field: logger.Error("Error field", err), expectedString: "my_error_value"},
{field: logger.Time("Time field", now), expectedString: now.String()},
{field: logger.Duration("Duration field", 5 * time.Second), expectedString: "5s"},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedString: "my_stringer"},
Expand Down
2 changes: 1 addition & 1 deletion formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type FormatterInterface interface {
type NopFormatter struct{}

// Format will return empty string
func (n *NopFormatter) Format(entry Entry) string {
func (n *NopFormatter) Format(_ Entry) string {
return ""
}

Expand Down
9 changes: 5 additions & 4 deletions formatter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,26 @@ func (n *DefaultFormatter) Format(entry logger.Entry) string {
}

// NewDefaultFormatter will create a new DefaultFormatter
func NewDefaultFormatter(options ...option) *DefaultFormatter {
func NewDefaultFormatter(options ...Option) *DefaultFormatter {
f := &DefaultFormatter{}
for _, option := range options {
option(f)
}
return f
}

type option func(*DefaultFormatter)
// Option is the option pattern interface for the DefaultFormatter
type Option func(*DefaultFormatter)

// WithColor function will enable ANSI colored formatting
func WithColor(enable bool) option {
func WithColor(enable bool) Option {
return func(formatter *DefaultFormatter) {
formatter.colored = enable
}
}

// WithContext function will display context printing
func WithContext(enable bool) option {
func WithContext(enable bool) Option {
return func(formatter *DefaultFormatter) {
formatter.displayContext = enable
}
Expand Down
4 changes: 2 additions & 2 deletions formatter/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestDefaultFormatter_Format_AllColor(t *testing.T) {
{level: logger.InfoLevel, expected: "\x1b[1;32m<info>\x1b[m my message"},
{level: logger.DebugLevel, expected: "\x1b[1;36m<debug>\x1b[m my message"},
}
formatter := formatter.NewDefaultFormatter(formatter.WithColor(true))
defaultFormatter := formatter.NewDefaultFormatter(formatter.WithColor(true))
for _, tt := range tests {
t.Run(tt.level.String(), func(t *testing.T) {
assert.Equal(t, tt.expected, formatter.Format(logger.Entry{Level: tt.level, Message: "my message"}))
assert.Equal(t, tt.expected, defaultFormatter.Format(logger.Entry{Level: tt.level, Message: "my message"}))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion handler/chan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestChan_Handle(t *testing.T) {
chanHandler := handler.Chan(entryChan)

entryContext := logger.Context(map[string]logger.Field{
"my_key": {Value: "my_overwrited_value"},
"my_key": {Value: "my_overwritten_value"},
"my_entry_key": {Value: "my_entry_value"},
})
logEntry := logger.Entry{
Expand Down
12 changes: 6 additions & 6 deletions handler/gelf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func TestGelf_withWrongNetwork(t *testing.T) {

type WrongConn struct{}

func (w *WrongConn) Read(b []byte) (n int, err error) { return 0, nil }
func (w *WrongConn) Write(b []byte) (n int, err error) { return 0, nil }
func (w *WrongConn) Read(_ []byte) (n int, err error) { return 0, nil }
func (w *WrongConn) Write(_ []byte) (n int, err error) { return 0, nil }
func (w *WrongConn) Close() error { return nil }
func (w *WrongConn) LocalAddr() net.Addr { return nil }
func (w *WrongConn) RemoteAddr() net.Addr { return nil }
func (w *WrongConn) SetDeadline(t time.Time) error { return nil }
func (w *WrongConn) SetReadDeadline(t time.Time) error { return nil }
func (w *WrongConn) SetWriteDeadline(t time.Time) error { return nil }
func (w *WrongConn) RemoteAddr() net.Addr { return nil }
func (w *WrongConn) SetDeadline(_ time.Time) error { return nil }
func (w *WrongConn) SetReadDeadline(_ time.Time) error { return nil }
func (w *WrongConn) SetWriteDeadline(_ time.Time) error { return nil }

func TestGelfFromConnection(t *testing.T) {

Expand Down
4 changes: 2 additions & 2 deletions handler/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/gol4ng/logger"
)

// Group will send Entry to each underlaying handlers
// usefull when you want to send your log in multiple destination eg stdOut/file/logserver
// Group will send Entry to each underlying handlers
// useful when you want to send your log in multiple destination eg stdOut/file/logserver
func Group(handlers ...logger.HandlerInterface) logger.HandlerInterface {
return func(entry logger.Entry) error {
var err error
Expand Down
Loading

0 comments on commit fdfc1e8

Please sign in to comment.