-
Notifications
You must be signed in to change notification settings - Fork 10
/
adapter.go
60 lines (49 loc) · 1.48 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Package goalog15 contains an adapter that makes it possible to configure goa so it uses log15
as logger backend.
Usage:
logger := log15.New()
// ... Initialize logger handler using log15 package
service.WithLogger(goalog15.New(logger))
// ... Proceed with configuring and starting the goa service
// In handlers:
goalog15.Logger(ctx).Info("foo")
*/
package goalog15
import (
"context"
"github.com/inconshreveable/log15"
"github.com/shogo82148/goa-v1"
)
// adapter is the log15 goa adapter logger.
type adapter struct {
log15.Logger
}
// New wraps a log15 logger into a goa logger adapter.
func New(logger log15.Logger) goa.LogAdapter {
return &adapter{Logger: logger}
}
// Logger returns the log15 logger stored in the given context if any, nil otherwise.
func Logger(ctx context.Context) log15.Logger {
logger := goa.ContextLogger(ctx)
if a, ok := logger.(*adapter); ok {
return a.Logger
}
return nil
}
// Info logs informational messages using log15.
func (a *adapter) Info(msg string, data ...interface{}) {
a.Logger.Info(msg, data...)
}
// Warn logs informational messages using log15.
func (a *adapter) Warn(msg string, data ...interface{}) {
a.Logger.Warn(msg, data...)
}
// Error logs error messages using log15.
func (a *adapter) Error(msg string, data ...interface{}) {
a.Logger.Error(msg, data...)
}
// New creates a new logger given a context.
func (a *adapter) New(data ...interface{}) goa.LogAdapter {
return &adapter{Logger: a.Logger.New(data...)}
}