From ea1f4e06302d2cb1c0a1cfa7932f05f235783760 Mon Sep 17 00:00:00 2001 From: dimacgka Date: Tue, 16 Jul 2024 16:10:51 +0300 Subject: [PATCH] [+] added zapLogger --- go.mod | 2 ++ go.sum | 5 +++ logger/fmt.go | 2 +- logger/zap.go | 49 ++++++++++++++++++++++++++ logger/zap_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 logger/zap.go create mode 100644 logger/zap_test.go diff --git a/go.mod b/go.mod index 20942b3..1cb73e5 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ 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 ) @@ -13,4 +14,5 @@ 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 ) diff --git a/go.sum b/go.sum index a3d9061..90f6df7 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/logger/fmt.go b/logger/fmt.go index 8d3b17d..afb0009 100644 --- a/logger/fmt.go +++ b/logger/fmt.go @@ -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 { diff --git a/logger/zap.go b/logger/zap.go new file mode 100644 index 0000000..a09d21c --- /dev/null +++ b/logger/zap.go @@ -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} +} diff --git a/logger/zap_test.go b/logger/zap_test.go new file mode 100644 index 0000000..5b662f7 --- /dev/null +++ b/logger/zap_test.go @@ -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'") + } +}