Skip to content

Commit

Permalink
[+] added zapLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacgka authored and Insei committed Jul 16, 2024
1 parent 9b095cb commit ea1f4e0
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ require (
github.com/insei/cast v1.1.1
github.com/insei/fmap/v2 v2.0.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion logger/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (t *fmtLogger) Warn(msg string, fld ...tinyconf.Field) {
}

func (t *fmtLogger) Info(msg string, fld ...tinyconf.Field) {
t.msg("WARN", msg, fld...)
t.msg("INFO", msg, fld...)
}

func (t *fmtLogger) With(flds ...tinyconf.Field) tinyconf.Logger {
Expand Down
49 changes: 49 additions & 0 deletions logger/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package logger

import (
"go.uber.org/zap"

"github.com/insei/tinyconf"
)

// zapLogger is a simple l that uses zap.Logger
type zapLogger struct {
l *zap.Logger
}

func (t *zapLogger) castToZapFields(flds ...tinyconf.Field) *zapLogger {
l := t.l
for _, fld := range flds {
l = l.With(zap.Any(fld.Key, fld.Value))
}
return &zapLogger{l: l}
}

// Debug logs a message at the DEBUG level
func (t *zapLogger) Debug(msg string, fld ...tinyconf.Field) {
t.castToZapFields(fld...).l.Debug(msg)
}

// Error logs a message at the ERROR level
func (t *zapLogger) Error(msg string, fld ...tinyconf.Field) {
t.castToZapFields(fld...).l.Error(msg)
}

// Warn logs a message at the WARN level
func (t *zapLogger) Warn(msg string, fld ...tinyconf.Field) {
t.castToZapFields(fld...).l.Warn(msg)
}

// Info logs a message at the INFO level
func (t *zapLogger) Info(msg string, fld ...tinyconf.Field) {
t.castToZapFields(fld...).l.Info(msg)
}

func (t *zapLogger) With(fld ...tinyconf.Field) tinyconf.Logger {
return t.castToZapFields(fld...)
}

// NewZapLogger creates a new zapLogger
func NewZapLogger(l *zap.Logger) tinyconf.Logger {
return &zapLogger{l: l}
}
87 changes: 87 additions & 0 deletions logger/zap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package logger

import (
"os"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/insei/tinyconf"
)

func TestZapDebug(t *testing.T) {
var config zapcore.EncoderConfig
config = zap.NewDevelopmentEncoderConfig()

zapLg := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(config),
zapcore.Lock(os.Stdout),
zapcore.DebugLevel,
))
lg := NewZapLogger(zapLg)
logger := lg.(*zapLogger)

logger.Debug("Debug message", tinyconf.Field{Key: "Login", Value: "world"})
}

func TestZapError(t *testing.T) {
var config zapcore.EncoderConfig
config = zap.NewDevelopmentEncoderConfig()

zapLg := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(config),
zapcore.Lock(os.Stdout),
zapcore.ErrorLevel,
))
lg := NewZapLogger(zapLg)
logger := lg.(*zapLogger)

logger.Error("Error message", tinyconf.Field{Key: "Name", Value: "Eddy"})
}

func TestZapWarning(t *testing.T) {
var config zapcore.EncoderConfig
config = zap.NewDevelopmentEncoderConfig()

zapLg := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(config),
zapcore.Lock(os.Stdout),
zapcore.WarnLevel,
))
lg := NewZapLogger(zapLg)
logger := lg.(*zapLogger)

logger.Warn("Warn message", tinyconf.Field{Key: "Table", Value: "admins"})
}

func TestZapInfo(t *testing.T) {
var config zapcore.EncoderConfig
config = zap.NewDevelopmentEncoderConfig()

zapLg := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(config),
zapcore.Lock(os.Stdout),
zapcore.InfoLevel,
))
lg := NewZapLogger(zapLg)
logger := lg.(*zapLogger)

logger.Info("Info message", tinyconf.Field{Key: "Success", Value: "true"})
}

func TestNewZapLogger(t *testing.T) {
var config zapcore.EncoderConfig
config = zap.NewDevelopmentEncoderConfig()

zapLg := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(config),
zapcore.Lock(os.Stdout),
zapcore.InfoLevel,
))
logger := NewZapLogger(zapLg)
_, ok := logger.(*zapLogger)
if !ok {
t.Error("The type of l is not '*zapLogger'")
}
}

0 comments on commit ea1f4e0

Please sign in to comment.