File tree Expand file tree Collapse file tree 4 files changed +23
-17
lines changed Expand file tree Collapse file tree 4 files changed +23
-17
lines changed Original file line number Diff line number Diff line change @@ -15,11 +15,11 @@ var ErrSome = errors.New("some error")
15
15
func main () {
16
16
ctx := context .Background ()
17
17
18
- // First create a logrus logger entry. This is basically a Logger with some optional fields .
19
- entry := newLogrusEntry ()
20
- // Then create a logger.Adapter
18
+ // First create a logrus logger.
19
+ l := newLogrusLogger ()
20
+ // Then create a logger.Adapter.
21
21
adapter := logrusadapter.Adapter {
22
- Entry : entry , // inject logrus
22
+ Logger : l , // you can also pass * logrus.Entry
23
23
}
24
24
// Create yala logger
25
25
log := logger .WithAdapter (adapter )
@@ -30,11 +30,11 @@ func main() {
30
30
log .WithError (ErrSome ).Error (ctx , "Some error" )
31
31
}
32
32
33
- func newLogrusEntry () * logrus.Entry {
33
+ func newLogrusLogger () * logrus.Logger {
34
34
l := logrus .New ()
35
35
l .SetLevel (logrus .DebugLevel )
36
36
l .SetFormatter (& logrus.TextFormatter {
37
37
ForceColors : true ,
38
38
})
39
- return logrus . NewEntry ( l )
39
+ return l
40
40
}
Original file line number Diff line number Diff line change @@ -13,26 +13,33 @@ import (
13
13
14
14
// Adapter is a logger.Adapter implementation, which is using `logrus` module (https://github.com/sirupsen/logrus).
15
15
type Adapter struct {
16
- Entry * logrus.Entry
16
+ Logger LogrusLogger
17
+ }
18
+
19
+ // LogrusLogger is either *logrus.Logger or *logrus.Entry.
20
+ type LogrusLogger interface {
21
+ WithField (key string , value interface {}) * logrus.Entry
22
+ WithError (err error ) * logrus.Entry
23
+ Log (lvl logrus.Level , args ... interface {})
17
24
}
18
25
19
26
// Log logs the entry using logrus module.
20
27
func (a Adapter ) Log (ctx context.Context , entry logger.Entry ) {
21
- if a .Entry == nil {
28
+ if a .Logger == nil {
22
29
return
23
30
}
24
31
25
- logrusEntry := a .Entry
32
+ logrusLogger := a .Logger
26
33
27
34
for _ , f := range entry .Fields {
28
- logrusEntry = logrusEntry .WithField (f .Key , f .Value )
35
+ logrusLogger = logrusLogger .WithField (f .Key , f .Value )
29
36
}
30
37
31
38
if entry .Error != nil {
32
- logrusEntry = logrusEntry .WithError (entry .Error )
39
+ logrusLogger = logrusLogger .WithError (entry .Error )
33
40
}
34
41
35
- logrusEntry .Log (logrusLevel (entry ), entry .Message )
42
+ logrusLogger .Log (logrusLevel (entry ), entry .Message )
36
43
}
37
44
38
45
func logrusLevel (entry logger.Entry ) logrus.Level {
Original file line number Diff line number Diff line change @@ -14,9 +14,8 @@ import (
14
14
func BenchmarkLogrus (b * testing.B ) {
15
15
l := logrus .New ()
16
16
l .SetOutput (benchmark.DiscardWriter {})
17
- logrusEntry := logrus .NewEntry (l )
18
17
19
- adapter := logrusadapter.Adapter {Entry : logrusEntry }
18
+ adapter := logrusadapter.Adapter {Logger : l }
20
19
21
20
benchmark .Adapter (b , adapter )
22
21
}
Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ const message = "message"
23
23
func TestAdapter_Log (t * testing.T ) {
24
24
ctx := context .Background ()
25
25
26
- t .Run ("should not panic when entry is nil" , func (t * testing.T ) {
27
- adapter := logrusadapter.Adapter {Entry : nil }
26
+ t .Run ("should not panic when logger is nil" , func (t * testing.T ) {
27
+ adapter := logrusadapter.Adapter {Logger : nil }
28
28
assert .NotPanics (t , func () {
29
29
adapter .Log (ctx , logger.Entry {
30
30
Level : logger .InfoLevel ,
@@ -45,7 +45,7 @@ func newAdapter(writer io.Writer) logger.Adapter { // nolint
45
45
logrusLogger .SetOutput (writer )
46
46
logrusLogger .SetLevel (logrus .DebugLevel )
47
47
48
- return logrusadapter.Adapter {Entry : logrus . NewEntry ( logrusLogger ) }
48
+ return logrusadapter.Adapter {Logger : logrusLogger }
49
49
}
50
50
51
51
var levelMapping = map [string ]logger.Level {
You can’t perform that action at this time.
0 commit comments