/
logger.go
92 lines (71 loc) · 2.53 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
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
package plugin
import (
"fmt"
"io/ioutil"
"log"
hclog "github.com/hashicorp/go-hclog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Hclog2ZapLogger implements Hashicorp's hclog.Logger interface using Uber's zap.Logger. It's a workaround for plugin
// system. go-plugin doesn't support other logger than hclog. This logger implements only methods used by the go-plugin.
type Hclog2ZapLogger struct {
Zap *zap.Logger
}
// Trace implementation.
func (l Hclog2ZapLogger) Trace(msg string, args ...interface{}) {}
// Debug implementation.
func (l Hclog2ZapLogger) Debug(msg string, args ...interface{}) {
l.Zap.Debug(msg, argsToFields(args...)...)
}
// Info implementation.
func (l Hclog2ZapLogger) Info(msg string, args ...interface{}) {
l.Zap.Info(msg, argsToFields(args...)...)
}
// Warn implementation.
func (l Hclog2ZapLogger) Warn(msg string, args ...interface{}) {
l.Zap.Warn(msg, argsToFields(args...)...)
}
// Error implementation.
func (l Hclog2ZapLogger) Error(msg string, args ...interface{}) {
l.Zap.Error(msg, argsToFields(args...)...)
}
// IsTrace implementation.
func (l Hclog2ZapLogger) IsTrace() bool { return false }
// IsDebug implementation.
func (l Hclog2ZapLogger) IsDebug() bool { return false }
// IsInfo implementation.
func (l Hclog2ZapLogger) IsInfo() bool { return false }
// IsWarn implementation.
func (l Hclog2ZapLogger) IsWarn() bool { return false }
// IsError implementation.
func (l Hclog2ZapLogger) IsError() bool { return false }
// With implementation.
func (l Hclog2ZapLogger) With(args ...interface{}) hclog.Logger {
return Hclog2ZapLogger{Zap: l.Zap.With(argsToFields(args...)...)}
}
// Named implementation.
func (l Hclog2ZapLogger) Named(name string) hclog.Logger {
return Hclog2ZapLogger{Zap: l.Zap.Named(name)}
}
// ResetNamed implementation.
func (l Hclog2ZapLogger) ResetNamed(name string) hclog.Logger {
// no need to implement that as go-plugin doesn't use this method.
return Hclog2ZapLogger{}
}
// SetLevel implementation.
func (l Hclog2ZapLogger) SetLevel(level hclog.Level) {
// no need to implement that as go-plugin doesn't use this method.
}
// StandardLogger implementation.
func (l Hclog2ZapLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
// no need to implement that as go-plugin doesn't use this method.
return log.New(ioutil.Discard, "", 0)
}
func argsToFields(args ...interface{}) []zapcore.Field {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.String(args[i].(string), fmt.Sprintf("%v", args[i+1])))
}
return fields
}