@@ -8,101 +8,84 @@ import (
8
8
"golang.org/x/net/context"
9
9
)
10
10
11
+ // ErrMissingLogValue is the value used to log keys with missing values
12
+ const ErrMissingLogValue = "MISSING"
13
+
11
14
type (
12
15
// LogAdapter is the logger interface used by goa to log informational and error messages.
13
- // Adapters to different logging backends are provided in the logging package .
16
+ // Adapters to different logging backends are provided in the logging sub-packages .
14
17
// goa takes care of initializing the logging context with the service, controller and
15
- // action name. Additional logging context values may be set via WithValue .
18
+ // action names .
16
19
LogAdapter interface {
17
20
// Info logs an informational message.
18
21
Info (msg string , keyvals ... interface {})
19
22
// Error logs an error.
20
23
Error (msg string , keyvals ... interface {})
21
- // New appends to the logger context and returns the updated logger adapter .
24
+ // New appends to the logger context and returns the updated logger logger .
22
25
New (keyvals ... interface {}) LogAdapter
23
26
}
24
27
25
- // stdLogger uses the stdlib logger.
26
- stdLogger struct {
28
+ // adapter is the stdlib logger adapter .
29
+ adapter struct {
27
30
* log.Logger
28
31
keyvals []interface {}
29
32
}
30
33
)
31
34
32
- // ErrMissingLogValue is the value used to log keys with missing values
33
- const ErrMissingLogValue = "MISSING"
34
-
35
- // LogInfo extracts the logger from the given context and calls Info on it.
36
- // In general this shouldn't be needed (the client code should already have a handle on the logger)
37
- // This is mainly useful for "out-of-band" code like middleware.
38
- func LogInfo (ctx context.Context , msg string , keyvals ... interface {}) {
39
- logit (ctx , msg , keyvals , false )
35
+ // NewLogger returns a goa log adpater backed by a log logger.
36
+ func NewLogger (logger * log.Logger ) LogAdapter {
37
+ return & adapter {Logger : logger }
40
38
}
41
39
42
- // LogError extracts the logger from the given context and calls Error on it.
43
- // In general this shouldn't be needed (the client code should already have a handle on the logger)
44
- // This is mainly useful for "out-of-band" code like middleware.
45
- func LogError (ctx context.Context , msg string , keyvals ... interface {}) {
46
- logit (ctx , msg , keyvals , true )
47
- }
48
-
49
- func logit (ctx context.Context , msg string , keyvals []interface {}, aserror bool ) {
50
- if l := ctx .Value (logKey ); l != nil {
51
- if logger , ok := l .(LogAdapter ); ok {
52
- if aserror {
53
- logger .Error (msg , keyvals ... )
54
- } else {
55
- logger .Info (msg , keyvals ... )
56
- }
57
- }
40
+ // Logger returns the logger stored in the context if any, nil otherwise.
41
+ func Logger (ctx context.Context ) * log.Logger {
42
+ logger := ContextLogger (ctx )
43
+ if a , ok := logger .(* adapter ); ok {
44
+ return a .Logger
58
45
}
46
+ return nil
59
47
}
60
48
61
- // NewStdLogger returns an implementation of Logger backed by a stdlib Logger.
62
- func NewStdLogger (logger * log.Logger ) LogAdapter {
63
- return & stdLogger {Logger : logger }
64
- }
65
-
66
- func (l * stdLogger ) Info (msg string , keyvals ... interface {}) {
67
- l .logit (msg , keyvals , false )
49
+ func (a * adapter ) Info (msg string , keyvals ... interface {}) {
50
+ a .logit (msg , keyvals , false )
68
51
}
69
52
70
- func (l * stdLogger ) Error (msg string , keyvals ... interface {}) {
71
- l .logit (msg , keyvals , true )
53
+ func (a * adapter ) Error (msg string , keyvals ... interface {}) {
54
+ a .logit (msg , keyvals , true )
72
55
}
73
56
74
- func (l * stdLogger ) New (keyvals ... interface {}) LogAdapter {
57
+ func (a * adapter ) New (keyvals ... interface {}) LogAdapter {
75
58
if len (keyvals ) == 0 {
76
- return l
59
+ return a
77
60
}
78
- kvs := append (l .keyvals , keyvals ... )
61
+ kvs := append (a .keyvals , keyvals ... )
79
62
if len (kvs )% 2 != 0 {
80
63
kvs = append (kvs , ErrMissingLogValue )
81
64
}
82
- return & stdLogger {
83
- Logger : l .Logger ,
65
+ return & adapter {
66
+ Logger : a .Logger ,
84
67
// Limiting the capacity of the stored keyvals ensures that a new
85
68
// backing array is created if the slice must grow.
86
69
keyvals : kvs [:len (kvs ):len (kvs )],
87
70
}
88
71
}
89
72
90
- func (l * stdLogger ) logit (msg string , keyvals []interface {}, iserror bool ) {
73
+ func (a * adapter ) logit (msg string , keyvals []interface {}, iserror bool ) {
91
74
n := (len (keyvals ) + 1 ) / 2
92
75
if len (keyvals )% 2 != 0 {
93
76
keyvals = append (keyvals , ErrMissingLogValue )
94
77
}
95
78
var fm bytes.Buffer
96
79
lvl := "INFO"
97
80
if iserror {
98
- lvl = "ERROR "
81
+ lvl = "EROR "
99
82
}
100
83
fm .WriteString (fmt .Sprintf ("[%s] %s" , lvl , msg ))
101
84
vals := make ([]interface {}, n )
102
- offset := len (l .keyvals )
85
+ offset := len (a .keyvals )
103
86
for i := 0 ; i < offset ; i += 2 {
104
- k := l .keyvals [i ]
105
- v := l .keyvals [i + 1 ]
87
+ k := a .keyvals [i ]
88
+ v := a .keyvals [i + 1 ]
106
89
vals [i / 2 ] = v
107
90
fm .WriteString (fmt .Sprintf (" %s=%%+v" , k ))
108
91
}
@@ -112,5 +95,27 @@ func (l *stdLogger) logit(msg string, keyvals []interface{}, iserror bool) {
112
95
vals [i / 2 + offset / 2 ] = v
113
96
fm .WriteString (fmt .Sprintf (" %s=%%+v" , k ))
114
97
}
115
- l .Logger .Printf (fm .String (), vals ... )
98
+ a .Logger .Printf (fm .String (), vals ... )
99
+ }
100
+
101
+ // LogInfo extracts the logger from the given context and calls Info on it.
102
+ // This is intended for code that needs portable logging such as the internal code of goa and
103
+ // middleware. User code should use the log adapters instead.
104
+ func LogInfo (ctx context.Context , msg string , keyvals ... interface {}) {
105
+ if l := ctx .Value (logKey ); l != nil {
106
+ if logger , ok := l .(LogAdapter ); ok {
107
+ logger .Info (msg , keyvals ... )
108
+ }
109
+ }
110
+ }
111
+
112
+ // LogError extracts the logger from the given context and calls Error on it.
113
+ // This is intended for code that needs portable logging such as the internal code of goa and
114
+ // middleware. User code should use the log adapters instead.
115
+ func LogError (ctx context.Context , msg string , keyvals ... interface {}) {
116
+ if l := ctx .Value (logKey ); l != nil {
117
+ if logger , ok := l .(LogAdapter ); ok {
118
+ logger .Error (msg , keyvals ... )
119
+ }
120
+ }
116
121
}
0 commit comments