logger: move zap into its own package (#47)

Since #27 vmware-go-kcl has support the any logger interface,
which is very nice.

However due to the fact that `logger/zap.go` directly imports zap.
zap became a dependency of whoever uses `vmware-go-kcl.` The
problem is that zap also has many dependencies.

In order to avoid KCL users to pay a cost for a feature they don't
need, the zap stuff has been moved to a `logger/zap` sub-package.

Fixes #45

Signed-off-by: Aurélien Rainone <aurelien.rainone@gmail.com>
This commit is contained in:
Aurélien Rainone 2019-11-04 18:22:29 +01:00 committed by Tao Jiang
parent 971d748195
commit 8a8f9e6339
3 changed files with 56 additions and 47 deletions

View file

@ -21,42 +21,11 @@
package logger package logger
import ( import (
"github.com/stretchr/testify/assert" "testing"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.uber.org/zap"
"testing"
) )
func TestZapLoggerWithConfig(t *testing.T) {
config := Configuration{
EnableConsole: true,
ConsoleLevel: Debug,
ConsoleJSONFormat: true,
EnableFile: false,
FileLevel: Info,
FileJSONFormat: true,
Filename: "log.log",
}
log := NewZapLoggerWithConfig(config)
contextLogger := log.WithFields(Fields{"key1": "value1"})
contextLogger.Debugf("Starting with zap")
contextLogger.Infof("Zap is awesome")
}
func TestZapLogger(t *testing.T) {
zapLogger, err := zap.NewProduction()
assert.Nil(t, err)
log := NewZapLogger(zapLogger.Sugar())
contextLogger := log.WithFields(Fields{"key1": "value1"})
contextLogger.Debugf("Starting with zap")
contextLogger.Infof("Zap is awesome")
}
func TestLogrusLoggerWithConfig(t *testing.T) { func TestLogrusLoggerWithConfig(t *testing.T) {
config := Configuration{ config := Configuration{
EnableConsole: true, EnableConsole: true,

View file

@ -19,18 +19,19 @@
// Note: The implementation comes from https://www.mountedthoughts.com/golang-logger-interface/ // Note: The implementation comes from https://www.mountedthoughts.com/golang-logger-interface/
// https://github.com/amitrai48/logger // https://github.com/amitrai48/logger
package logger package zap
import ( import (
"os" "os"
"go.uber.org/zap" "github.com/vmware/vmware-go-kcl/logger"
uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2" lumberjack "gopkg.in/natefinch/lumberjack.v2"
) )
type ZapLogger struct { type ZapLogger struct {
sugaredLogger *zap.SugaredLogger sugaredLogger *uzap.SugaredLogger
} }
// NewZapLogger adapts existing sugared zap logger to Logger interface. // NewZapLogger adapts existing sugared zap logger to Logger interface.
@ -44,7 +45,7 @@ type ZapLogger struct {
// Base zap logger can be convert to SugaredLogger by calling to add a wrapper: // Base zap logger can be convert to SugaredLogger by calling to add a wrapper:
// sugaredLogger := log.Sugar() // sugaredLogger := log.Sugar()
// //
func NewZapLogger(logger *zap.SugaredLogger) Logger { func NewZapLogger(logger *uzap.SugaredLogger) logger.Logger {
return &ZapLogger{ return &ZapLogger{
sugaredLogger: logger, sugaredLogger: logger,
} }
@ -52,7 +53,7 @@ func NewZapLogger(logger *zap.SugaredLogger) Logger {
// NewZapLoggerWithConfig creates and configs Logger instance backed by // NewZapLoggerWithConfig creates and configs Logger instance backed by
// zap Sugared logger. // zap Sugared logger.
func NewZapLoggerWithConfig(config Configuration) Logger { func NewZapLoggerWithConfig(config logger.Configuration) logger.Logger {
cores := []zapcore.Core{} cores := []zapcore.Core{}
if config.EnableConsole { if config.EnableConsole {
@ -80,9 +81,9 @@ func NewZapLoggerWithConfig(config Configuration) Logger {
// AddCallerSkip skips 2 number of callers, this is important else the file that gets // AddCallerSkip skips 2 number of callers, this is important else the file that gets
// logged will always be the wrapped file. In our case zap.go // logged will always be the wrapped file. In our case zap.go
logger := zap.New(combinedCore, logger := uzap.New(combinedCore,
zap.AddCallerSkip(2), uzap.AddCallerSkip(2),
zap.AddCaller(), uzap.AddCaller(),
).Sugar() ).Sugar()
return &ZapLogger{ return &ZapLogger{
@ -114,7 +115,7 @@ func (l *ZapLogger) Panicf(format string, args ...interface{}) {
l.sugaredLogger.Fatalf(format, args...) l.sugaredLogger.Fatalf(format, args...)
} }
func (l *ZapLogger) WithFields(fields Fields) Logger { func (l *ZapLogger) WithFields(fields logger.Fields) logger.Logger {
var f = make([]interface{}, 0) var f = make([]interface{}, 0)
for k, v := range fields { for k, v := range fields {
f = append(f, k) f = append(f, k)
@ -125,7 +126,7 @@ func (l *ZapLogger) WithFields(fields Fields) Logger {
} }
func getEncoder(isJSON bool) zapcore.Encoder { func getEncoder(isJSON bool) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig() encoderConfig := uzap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if isJSON { if isJSON {
return zapcore.NewJSONEncoder(encoderConfig) return zapcore.NewJSONEncoder(encoderConfig)
@ -135,15 +136,15 @@ func getEncoder(isJSON bool) zapcore.Encoder {
func getZapLevel(level string) zapcore.Level { func getZapLevel(level string) zapcore.Level {
switch level { switch level {
case Info: case logger.Info:
return zapcore.InfoLevel return zapcore.InfoLevel
case Warn: case logger.Warn:
return zapcore.WarnLevel return zapcore.WarnLevel
case Debug: case logger.Debug:
return zapcore.DebugLevel return zapcore.DebugLevel
case Error: case logger.Error:
return zapcore.ErrorLevel return zapcore.ErrorLevel
case Fatal: case logger.Fatal:
return zapcore.FatalLevel return zapcore.FatalLevel
default: default:
return zapcore.InfoLevel return zapcore.InfoLevel

39
logger/zap/zap_test.go Normal file
View file

@ -0,0 +1,39 @@
package zap_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/vmware-go-kcl/logger"
"github.com/vmware/vmware-go-kcl/logger/zap"
uzap "go.uber.org/zap"
)
func TestZapLoggerWithConfig(t *testing.T) {
config := logger.Configuration{
EnableConsole: true,
ConsoleLevel: logger.Debug,
ConsoleJSONFormat: true,
EnableFile: false,
FileLevel: logger.Info,
FileJSONFormat: true,
Filename: "log.log",
}
log := zap.NewZapLoggerWithConfig(config)
contextLogger := log.WithFields(logger.Fields{"key1": "value1"})
contextLogger.Debugf("Starting with zap")
contextLogger.Infof("Zap is awesome")
}
func TestZapLogger(t *testing.T) {
zapLogger, err := uzap.NewProduction()
assert.Nil(t, err)
log := zap.NewZapLogger(zapLogger.Sugar())
contextLogger := log.WithFields(logger.Fields{"key1": "value1"})
contextLogger.Debugf("Starting with zap")
contextLogger.Infof("Zap is awesome")
}