-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'") | ||
} | ||
} |