Skip to content

Commit 5756109

Browse files
arlTao Jiang
authored and
Tao Jiang
committed
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 <[email protected]>
1 parent ae383b8 commit 5756109

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

logger/logger_test.go

+1-32
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,11 @@
2121
package logger
2222

2323
import (
24-
"github.com/stretchr/testify/assert"
24+
"testing"
2525

2626
"github.com/sirupsen/logrus"
27-
"go.uber.org/zap"
28-
"testing"
2927
)
3028

31-
func TestZapLoggerWithConfig(t *testing.T) {
32-
config := Configuration{
33-
EnableConsole: true,
34-
ConsoleLevel: Debug,
35-
ConsoleJSONFormat: true,
36-
EnableFile: false,
37-
FileLevel: Info,
38-
FileJSONFormat: true,
39-
Filename: "log.log",
40-
}
41-
42-
log := NewZapLoggerWithConfig(config)
43-
44-
contextLogger := log.WithFields(Fields{"key1": "value1"})
45-
contextLogger.Debugf("Starting with zap")
46-
contextLogger.Infof("Zap is awesome")
47-
}
48-
49-
func TestZapLogger(t *testing.T) {
50-
zapLogger, err := zap.NewProduction()
51-
assert.Nil(t, err)
52-
53-
log := NewZapLogger(zapLogger.Sugar())
54-
55-
contextLogger := log.WithFields(Fields{"key1": "value1"})
56-
contextLogger.Debugf("Starting with zap")
57-
contextLogger.Infof("Zap is awesome")
58-
}
59-
6029
func TestLogrusLoggerWithConfig(t *testing.T) {
6130
config := Configuration{
6231
EnableConsole: true,

logger/zap.go logger/zap/zap.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
// Note: The implementation comes from https://www.mountedthoughts.com/golang-logger-interface/
2020
// https://github.com/amitrai48/logger
2121

22-
package logger
22+
package zap
2323

2424
import (
2525
"os"
2626

27-
"go.uber.org/zap"
27+
"github.com/vmware/vmware-go-kcl/logger"
28+
uzap "go.uber.org/zap"
2829
"go.uber.org/zap/zapcore"
2930
lumberjack "gopkg.in/natefinch/lumberjack.v2"
3031
)
3132

3233
type ZapLogger struct {
33-
sugaredLogger *zap.SugaredLogger
34+
sugaredLogger *uzap.SugaredLogger
3435
}
3536

3637
// NewZapLogger adapts existing sugared zap logger to Logger interface.
@@ -44,15 +45,15 @@ type ZapLogger struct {
4445
// Base zap logger can be convert to SugaredLogger by calling to add a wrapper:
4546
// sugaredLogger := log.Sugar()
4647
//
47-
func NewZapLogger(logger *zap.SugaredLogger) Logger {
48+
func NewZapLogger(logger *uzap.SugaredLogger) logger.Logger {
4849
return &ZapLogger{
4950
sugaredLogger: logger,
5051
}
5152
}
5253

5354
// NewZapLoggerWithConfig creates and configs Logger instance backed by
5455
// zap Sugared logger.
55-
func NewZapLoggerWithConfig(config Configuration) Logger {
56+
func NewZapLoggerWithConfig(config logger.Configuration) logger.Logger {
5657
cores := []zapcore.Core{}
5758

5859
if config.EnableConsole {
@@ -80,9 +81,9 @@ func NewZapLoggerWithConfig(config Configuration) Logger {
8081

8182
// AddCallerSkip skips 2 number of callers, this is important else the file that gets
8283
// logged will always be the wrapped file. In our case zap.go
83-
logger := zap.New(combinedCore,
84-
zap.AddCallerSkip(2),
85-
zap.AddCaller(),
84+
logger := uzap.New(combinedCore,
85+
uzap.AddCallerSkip(2),
86+
uzap.AddCaller(),
8687
).Sugar()
8788

8889
return &ZapLogger{
@@ -114,7 +115,7 @@ func (l *ZapLogger) Panicf(format string, args ...interface{}) {
114115
l.sugaredLogger.Fatalf(format, args...)
115116
}
116117

117-
func (l *ZapLogger) WithFields(fields Fields) Logger {
118+
func (l *ZapLogger) WithFields(fields logger.Fields) logger.Logger {
118119
var f = make([]interface{}, 0)
119120
for k, v := range fields {
120121
f = append(f, k)
@@ -125,7 +126,7 @@ func (l *ZapLogger) WithFields(fields Fields) Logger {
125126
}
126127

127128
func getEncoder(isJSON bool) zapcore.Encoder {
128-
encoderConfig := zap.NewProductionEncoderConfig()
129+
encoderConfig := uzap.NewProductionEncoderConfig()
129130
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
130131
if isJSON {
131132
return zapcore.NewJSONEncoder(encoderConfig)
@@ -135,15 +136,15 @@ func getEncoder(isJSON bool) zapcore.Encoder {
135136

136137
func getZapLevel(level string) zapcore.Level {
137138
switch level {
138-
case Info:
139+
case logger.Info:
139140
return zapcore.InfoLevel
140-
case Warn:
141+
case logger.Warn:
141142
return zapcore.WarnLevel
142-
case Debug:
143+
case logger.Debug:
143144
return zapcore.DebugLevel
144-
case Error:
145+
case logger.Error:
145146
return zapcore.ErrorLevel
146-
case Fatal:
147+
case logger.Fatal:
147148
return zapcore.FatalLevel
148149
default:
149150
return zapcore.InfoLevel

logger/zap/zap_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package zap_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/vmware/vmware-go-kcl/logger"
8+
"github.com/vmware/vmware-go-kcl/logger/zap"
9+
uzap "go.uber.org/zap"
10+
)
11+
12+
func TestZapLoggerWithConfig(t *testing.T) {
13+
config := logger.Configuration{
14+
EnableConsole: true,
15+
ConsoleLevel: logger.Debug,
16+
ConsoleJSONFormat: true,
17+
EnableFile: false,
18+
FileLevel: logger.Info,
19+
FileJSONFormat: true,
20+
Filename: "log.log",
21+
}
22+
23+
log := zap.NewZapLoggerWithConfig(config)
24+
25+
contextLogger := log.WithFields(logger.Fields{"key1": "value1"})
26+
contextLogger.Debugf("Starting with zap")
27+
contextLogger.Infof("Zap is awesome")
28+
}
29+
30+
func TestZapLogger(t *testing.T) {
31+
zapLogger, err := uzap.NewProduction()
32+
assert.Nil(t, err)
33+
34+
log := zap.NewZapLogger(zapLogger.Sugar())
35+
36+
contextLogger := log.WithFields(logger.Fields{"key1": "value1"})
37+
contextLogger.Debugf("Starting with zap")
38+
contextLogger.Infof("Zap is awesome")
39+
}

0 commit comments

Comments
 (0)