forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
108 lines (94 loc) · 3.05 KB
/
logging.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package goa
import (
"bytes"
"fmt"
"log"
"golang.org/x/net/context"
)
type (
// Logger is the logger interface used by goa to log informational and error messages.
// Adapters to different logging backends are provided in the logging package.
Logger interface {
// Info logs an informational message.
Info(msg string, keyvals ...interface{})
// Error logs an error.
Error(msg string, keyvals ...interface{})
}
// stdLogger uses the stdlib logger.
stdLogger struct {
*log.Logger
}
)
// ErrMissingLogValue is the value used to log keys with missing values
const ErrMissingLogValue = "MISSING"
// LogInfo extracts the logger from the given context and calls Info on it.
// In general this shouldn't be needed (the client code should already have a handle on the logger)
// This is mainly useful for "out-of-band" code like middleware.
func LogInfo(ctx context.Context, msg string, keyvals ...interface{}) {
logit(ctx, msg, keyvals, false)
}
// LogError extracts the logger from the given context and calls Error on it.
// In general this shouldn't be needed (the client code should already have a handle on the logger)
// This is mainly useful for "out-of-band" code like middleware.
func LogError(ctx context.Context, msg string, keyvals ...interface{}) {
logit(ctx, msg, keyvals, true)
}
func logit(ctx context.Context, msg string, keyvals []interface{}, aserror bool) {
if l := ctx.Value(logKey); l != nil {
if logger, ok := l.(Logger); ok {
var logctx []interface{}
if lctx := ctx.Value(logContextKey); lctx != nil {
logctx = lctx.([]interface{})
}
data := append(logctx, keyvals...)
if aserror {
logger.Error(msg, data...)
} else {
logger.Info(msg, data...)
}
}
}
}
// LogWith stores logging context to be used by all Log invocations using the returned context.
func LogWith(ctx context.Context, keyvals ...interface{}) context.Context {
return context.WithValue(ctx, logContextKey, append(LogContext(ctx), keyvals...))
}
// LogContext returns the logging context initialized via LogWith.
func LogContext(ctx context.Context) []interface{} {
var lctx []interface{}
if v := ctx.Value(logContextKey); v != nil {
lctx = v.([]interface{})
}
return lctx
}
// NewStdLogger returns an implementation of Logger backed by a stdlib Logger.
func NewStdLogger(logger *log.Logger) Logger {
return &stdLogger{Logger: logger}
}
func (l *stdLogger) Info(msg string, keyvals ...interface{}) {
l.logit(msg, keyvals, false)
}
func (l *stdLogger) Error(msg string, keyvals ...interface{}) {
l.logit(msg, keyvals, true)
}
func (l *stdLogger) logit(msg string, keyvals []interface{}, iserror bool) {
n := (len(keyvals) + 1) / 2
var fm bytes.Buffer
lvl := "INFO"
if iserror {
lvl = "ERROR"
}
fm.WriteString(fmt.Sprintf("[%s] %s", lvl, msg))
vals := make([]interface{}, n)
for i := 0; i < len(keyvals); i += 2 {
k := keyvals[i]
var v interface{} = ErrMissingLogValue
if i+1 < len(keyvals) {
v = keyvals[i+1]
}
vals[i/2] = v
fm.WriteString(" ")
fm.WriteString(fmt.Sprintf("%s=%%+v", k))
}
l.Logger.Printf(fm.String(), vals...)
}