Skip to content

Commit c0c474b

Browse files
committed
修复logger测试用例
1 parent 2ffb2b4 commit c0c474b

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

file/fileinfo_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// +build !windows,!openbsd
32

43
// Test for openbsd are excluded here as info.GID() returns 0 instead of the actual value
@@ -15,7 +14,7 @@ import (
1514

1615
"github.com/stretchr/testify/assert"
1716

18-
"github.com/infraboard/mcube/logger/zap/file"
17+
"github.com/infraboard/mcube/file"
1918
)
2019

2120
func TestStat(t *testing.T) {
@@ -76,4 +75,4 @@ func TestLstat(t *testing.T) {
7675
t.Fatal(err)
7776
}
7877
assert.EqualValues(t, os.Getegid(), gid)
79-
}
78+
}

file/rotator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"time"
99

10+
"github.com/infraboard/mcube/logger"
1011
"github.com/pkg/errors"
1112
)
1213

@@ -61,7 +62,7 @@ type Rotator struct {
6162

6263
// Logger allows the rotator to write debug information.
6364
type Logger interface {
64-
Debugw(msg string, keysAndValues map[string]interface{}) // Debug
65+
Debugw(msg string, fields ...logger.Field) // Debug
6566
}
6667

6768
// RotatorOption is a configuration option for Rotator.
@@ -162,7 +163,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
162163
"interval": r.interval,
163164
}
164165
if r.log != nil {
165-
r.log.Debugw("Initialized file rotator", meta)
166+
r.log.Debugw("Initialized file rotator", logger.NewFieldsFromKV(meta)...)
166167
}
167168

168169
return r, nil
@@ -431,7 +432,7 @@ func (r *Rotator) rotateByInterval(reason rotateReason) error {
431432
"reason": reason,
432433
}
433434
if r.log != nil {
434-
r.log.Debugw("Rotating file", meta)
435+
r.log.Debugw("Rotating file", logger.NewFieldsFromKV(meta)...)
435436
}
436437

437438
r.intervalRotator.Rotate()
@@ -462,7 +463,7 @@ func (r *Rotator) rotateBySize(reason rotateReason) error {
462463
"filename": old,
463464
"reason": reason,
464465
}
465-
r.log.Debugw("Rotating file", meta)
466+
r.log.Debugw("Rotating file", logger.NewFieldsFromKV(meta)...)
466467
}
467468
}
468469
}

file/rotator_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
"github.com/stretchr/testify/assert"
1313

14-
"github.com/infraboard/mcube/logger/zap"
1514
"github.com/infraboard/mcube/file"
15+
"github.com/infraboard/mcube/logger"
16+
"github.com/infraboard/mcube/logger/zap"
1617
)
1718

1819
const logMessage = "Test file rotator.\n"
@@ -27,13 +28,15 @@ func TestFileRotator(t *testing.T) {
2728
defer os.RemoveAll(dir)
2829

2930
filename := filepath.Join(dir, "sample.log")
31+
debugL := zap.NewLogger("rotator").With(logger.NewAny("key", "rotate"))
3032
r, err := file.NewFileRotator(filename,
3133
file.MaxBackups(2),
32-
file.WithLogger(zap.NewLogger("rotator").With(map[string]interface{}{"key": "rotate"})),
34+
file.WithLogger(debugL),
3335
)
3436
if err != nil {
3537
t.Fatal(err)
3638
}
39+
3740
defer r.Close()
3841

3942
WriteMsg(t, r)

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ require (
77
github.com/coreos/bbolt v1.3.3 // indirect
88
github.com/coreos/etcd v3.3.18+incompatible // indirect
99
github.com/coreos/go-semver v0.3.0 // indirect
10+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
1011
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
1112
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
1213
github.com/go-redis/redis v6.15.6+incompatible
13-
github.com/go-stack/stack v1.8.0 // indirect
1414
github.com/gogo/protobuf v1.3.1 // indirect
1515
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
1616
github.com/google/btree v1.0.0 // indirect
@@ -21,8 +21,6 @@ require (
2121
github.com/grpc-ecosystem/grpc-gateway v1.12.1 // indirect
2222
github.com/jonboulle/clockwork v0.1.0 // indirect
2323
github.com/julienschmidt/httprouter v1.3.0
24-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
25-
github.com/modern-go/reflect2 v1.0.1 // indirect
2624
github.com/onsi/ginkgo v1.11.0 // indirect
2725
github.com/onsi/gomega v1.8.1 // indirect
2826
github.com/pkg/errors v0.8.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ github.com/coreos/etcd v3.3.18+incompatible h1:Zz1aXgDrFFi1nadh58tA9ktt06cmPTwNN
2222
github.com/coreos/etcd v3.3.18+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
2323
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
2424
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
25+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
26+
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
2527
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg=
2628
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
2729
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)