forked from openzipkin-contrib/zipkin-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
64 lines (53 loc) · 1.62 KB
/
logger.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
package zipkintracer
import (
"errors"
"fmt"
"log"
"strings"
)
// ErrMissingValue adds a Missing Value Error when the Logging Parameters are
// not even in number
var ErrMissingValue = errors.New("(MISSING)")
// Logger is the fundamental interface for all log operations. Log creates a
// log event from keyvals, a variadic sequence of alternating keys and values.
// The signature is compatible with the Go kit log package.
type Logger interface {
Log(keyvals ...interface{}) error
}
// NewNopLogger provides a Logger that discards all Log data sent to it.
func NewNopLogger() Logger {
return &nopLogger{}
}
// LogWrapper wraps a standard library logger into a Logger compatible with this
// package.
func LogWrapper(l *log.Logger) Logger {
return &wrappedLogger{l: l}
}
// wrappedLogger implements Logger
type wrappedLogger struct {
l *log.Logger
}
// Log implements Logger
func (l *wrappedLogger) Log(k ...interface{}) error {
if len(k)%2 == 1 {
k = append(k, ErrMissingValue)
}
o := make([]string, len(k)/2)
for i := 0; i < len(k); i += 2 {
o[i/2] = fmt.Sprintf("%s=%q", k[i], k[i+1])
}
l.l.Println(strings.Join(o, " "))
return nil
}
// nopLogger implements Logger
type nopLogger struct{}
// Log implements Logger
func (*nopLogger) Log(_ ...interface{}) error { return nil }
// LoggerFunc is an adapter to allow use of ordinary functions as Loggers. If
// f is a function with the appropriate signature, LoggerFunc(f) is a Logger
// object that calls f.
type LoggerFunc func(...interface{}) error
// Log implements Logger by calling f(keyvals...).
func (f LoggerFunc) Log(keyvals ...interface{}) error {
return f(keyvals...)
}