Skip to content

Commit

Permalink
fix(onelog): add tests and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed Jul 25, 2023
1 parent 94cc596 commit d06ad58
Show file tree
Hide file tree
Showing 12 changed files with 1,045 additions and 42 deletions.
90 changes: 69 additions & 21 deletions adapter/slog/adapter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package slogadapter

import (
"context"
"fmt"
"net"
"time"

"github.com/shopspring/decimal"

"golang.org/x/exp/slog"

"github.com/nikoksr/onelog"
Expand Down Expand Up @@ -71,6 +72,39 @@ func (a *Adapter) Fatal() onelog.LoggerContext {
return a.newContext(slog.LevelError) // Using Error level here because Fatal is not supported by slog
}

// Bytes adds the field key with val as a []byte to the logger context.
func (c *Context) Bytes(key string, value []byte) onelog.LoggerContext {
if c == nil {
return nil
}

c.fields = append(c.fields, slog.String(key, string(value)))

return c
}

// Hex adds the field key with val as a hex string to the logger context.
func (c *Context) Hex(key string, value []byte) onelog.LoggerContext {
if c == nil {
return nil
}

c.fields = append(c.fields, slog.String(key, fmt.Sprintf("%x", value)))

return c
}

// RawJSON adds the field key with val as a raw JSON string to the logger context.
func (c *Context) RawJSON(key string, value []byte) onelog.LoggerContext {
if c == nil {
return nil
}

c.fields = append(c.fields, slog.String(key, string(value)))

return c
}

// Str adds the field key with val as a string to the logger context.
func (c *Context) Str(key string, value string) onelog.LoggerContext {
if c == nil {
Expand All @@ -88,7 +122,7 @@ func (c *Context) Strs(key string, value []string) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -110,11 +144,12 @@ func (c *Context) Stringers(key string, value []fmt.Stringer) onelog.LoggerConte
return nil
}

// Todo: Better way to do this?
strs := make([]string, len(value))
for i, str := range value {
strs[i] = str.String()
}
c.fields = append(c.fields, slog.Group(key, strs))
c.fields = append(c.fields, slog.Any(key, strs))

return c
}
Expand All @@ -136,7 +171,7 @@ func (c *Context) Ints(key string, value []int) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -158,7 +193,7 @@ func (c *Context) Ints8(key string, value []int8) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -180,7 +215,7 @@ func (c *Context) Ints16(key string, value []int16) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -202,7 +237,7 @@ func (c *Context) Ints32(key string, value []int32) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -224,7 +259,7 @@ func (c *Context) Ints64(key string, value []int64) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -246,7 +281,7 @@ func (c *Context) Uints(key string, value []uint) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -268,7 +303,14 @@ func (c *Context) Uints8(key string, value []uint8) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
// Todo: Better way to do this?
// Convert []uint8 to []uint64
uints := make([]uint64, len(value))
for i, v := range value {
uints[i] = uint64(v)
}

c.fields = append(c.fields, slog.Any(key, uints))

return c
}
Expand All @@ -290,7 +332,7 @@ func (c *Context) Uints16(key string, value []uint16) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -312,7 +354,7 @@ func (c *Context) Uints32(key string, value []uint32) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -334,7 +376,7 @@ func (c *Context) Uints64(key string, value []uint64) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -345,7 +387,9 @@ func (c *Context) Float32(key string, value float32) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Float64(key, float64(value)))
d, _ := decimal.NewFromFloat32(value).Float64()

c.fields = append(c.fields, slog.Float64(key, d))

return c
}
Expand All @@ -356,7 +400,7 @@ func (c *Context) Floats32(key string, value []float32) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -378,7 +422,7 @@ func (c *Context) Floats64(key string, value []float64) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -400,7 +444,7 @@ func (c *Context) Bools(key string, value []bool) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -422,7 +466,7 @@ func (c *Context) Times(key string, value []time.Time) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand All @@ -444,7 +488,7 @@ func (c *Context) Durs(key string, value []time.Duration) onelog.LoggerContext {
return nil
}

c.fields = append(c.fields, slog.Group(key, value))
c.fields = append(c.fields, slog.Any(key, value))

return c
}
Expand Down Expand Up @@ -522,11 +566,14 @@ func (c *Context) Errs(key string, value []error) onelog.LoggerContext {
return nil
}

// Todo: Better way to do this?
// Convert []error to []string. If we don't do this, slog prints empty objects
errs := make([]string, len(value))
for i, err := range value {
errs[i] = err.Error()
}
c.fields = append(c.fields, slog.Group(key, errs))

c.fields = append(c.fields, slog.Any(key, errs))

return c
}
Expand Down Expand Up @@ -560,7 +607,8 @@ func (c *Context) Msg(msg string) {
return
}

c.logger.Log(context.Background(), c.level, msg, c.fields...)
//nolint:staticcheck // passing a nil context is fine, check slog.Logger.Info implementation for example
c.logger.Log(nil, c.level, msg, c.fields...)
c.fields = make([]any, 0) // reset fields
}

Expand Down
73 changes: 73 additions & 0 deletions adapter/slog/adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package slogadapter

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/slog"

"github.com/nikoksr/onelog/internal/testutils"
)

// TestNewAdapter tests if NewAdapter returns a non-nil *Adapter.
func TestNewAdapter(t *testing.T) {
t.Parallel()

slogLogger := slog.Default()
logger := NewAdapter(slogLogger)

assert.NotNil(t, logger, "the returned adapter should not be nil")
}

// TestContexts tests if each log level returns a valid *Context.
func TestContexts(t *testing.T) {
t.Parallel()

slogLogger := slog.Default()
logger := NewAdapter(slogLogger)

// Debug
logContext := logger.Debug()
assert.NotNil(t, logContext, "the returned context should not be nil")
assert.IsType(t, new(Context), logContext, "the returned context should be of type *Context")
assert.Equal(t, logContext.(*Context).level, slog.LevelDebug, "the returned context should have the correct log level")

// Info
logContext = logger.Info()
assert.NotNil(t, logContext, "the returned context should not be nil")
assert.IsType(t, new(Context), logContext, "the returned context should be of type *Context")
assert.Equal(t, logContext.(*Context).level, slog.LevelInfo, "the returned context should have the correct log level")

// Warn
logContext = logger.Warn()
assert.NotNil(t, logContext, "the returned context should not be nil")
assert.IsType(t, new(Context), logContext, "the returned context should be of type *Context")
assert.Equal(t, logContext.(*Context).level, slog.LevelWarn, "the returned context should have the correct log level")

// Error
logContext = logger.Error()
assert.NotNil(t, logContext, "the returned context should not be nil")
assert.IsType(t, new(Context), logContext, "the returned context should be of type *Context")
assert.Equal(t, logContext.(*Context).level, slog.LevelError, "the returned context should have the correct log level")

// Fatal; note that slog does not have a fatal level, so this should return an error level context
logContext = logger.Fatal()
assert.NotNil(t, logContext, "the returned context should not be nil")
assert.IsType(t, new(Context), logContext, "the returned context should be of type *Context")
assert.Equal(t, logContext.(*Context).level, slog.LevelError, "the returned context should have the correct log level")
}

// TestMethods tests if each method returns a non-nil *Context and if the log is written correctly.
func TestMethods(t *testing.T) {
t.Parallel()

buff := new(bytes.Buffer)
handler := slog.NewJSONHandler(buff, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
logger := slog.New(handler)
adapter := NewAdapter(logger)

testutils.TestingMethods(t, adapter, buff)
}
2 changes: 1 addition & 1 deletion adapter/zap/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func (c *Context) Msg(msg string) {
}

c.logger.Log(c.level, msg, c.fields...)
c.fields = make([]zapcore.Field, 0) // reset fields
c.reset()
}

// Msgf sends the LoggerContext with formatted msg to the logger.
Expand Down
Loading

0 comments on commit d06ad58

Please sign in to comment.