/
adapter.go
195 lines (162 loc) · 4.94 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package log
import (
"io"
"log"
"github.com/hashicorp/go-hclog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewHCLogAdapter creates a new adapter, wrapping an underlying
// zap.Logger inside an implementation that emulates hclog.Logger.
func NewHCLogAdapter(wrapped *zap.Logger) *HCLogAdapter {
if wrapped == nil {
wrapped = zap.L()
}
wrapped = zap.New(wrapped.Core(), zap.AddCallerSkip(1))
return &HCLogAdapter{
name: "",
zap: wrapped,
}
}
// HCLogAdapter is an adapter that allows to use a zap.Logger where
// and hclog.Logger is expected.
type HCLogAdapter struct {
name string
zap *zap.Logger
}
func (l *HCLogAdapter) Clone() *HCLogAdapter {
return &HCLogAdapter{
name: l.name,
zap: l.zap,
}
}
// // Logger describes the interface that must be implemeted by all loggers.
// type Logger interface {
// // Args are alternating key, val pairs
// // keys must be strings
// // vals can be any type, but display is implementation specific
// // Emit a message and key/value pairs at a provided log level
func (l *HCLogAdapter) Log(level hclog.Level, msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
switch level {
case hclog.Trace, hclog.Debug:
l.zap.Debug(msg, fields...)
case hclog.NoLevel, hclog.Info:
l.zap.Info(msg, fields...)
case hclog.Warn:
l.zap.Warn(msg, fields...)
case hclog.Error:
l.zap.Error(msg, fields...)
case hclog.Off:
// do nothing
}
}
// Emit a message and key/value pairs at the TRACE level
func (l *HCLogAdapter) Trace(msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
l.zap.Debug(msg, fields...)
}
// Emit a message and key/value pairs at the DEBUG level
func (l *HCLogAdapter) Debug(msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
l.zap.Debug(msg, fields...)
}
// Emit a message and key/value pairs at the INFO level
func (l *HCLogAdapter) Info(msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
l.zap.Info(msg, fields...)
}
// Emit a message and key/value pairs at the WARN level
func (l *HCLogAdapter) Warn(msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
l.zap.Warn(msg, fields...)
}
// Emit a message and key/value pairs at the ERROR level
func (l *HCLogAdapter) Error(msg string, args ...interface{}) {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
l.zap.Error(msg, fields...)
}
func (l *HCLogAdapter) IsTrace() bool {
return l.zap.Core().Enabled(zap.DebugLevel)
}
func (l *HCLogAdapter) IsDebug() bool {
return l.zap.Core().Enabled(zap.DebugLevel)
}
func (l *HCLogAdapter) IsInfo() bool {
return l.zap.Core().Enabled(zap.InfoLevel)
}
func (l *HCLogAdapter) IsWarn() bool {
return l.zap.Core().Enabled(zap.WarnLevel)
}
func (l *HCLogAdapter) IsError() bool {
return l.zap.Core().Enabled(zap.ErrorLevel)
}
// ImpliedArgs returns With key/value pairs
func (l *HCLogAdapter) ImpliedArgs() []interface{} {
return nil
}
func (l *HCLogAdapter) With(args ...interface{}) hclog.Logger {
fields := []zapcore.Field{}
for i := 0; i < len(args); i += 2 {
fields = append(fields, zap.Any(args[i].(string), args[i+1]))
}
return NewHCLogAdapter(l.zap.With(fields...))
}
func (l *HCLogAdapter) Name() string {
return l.name
}
func (l *HCLogAdapter) Named(name string) hclog.Logger {
nl := l.Clone()
nl.name = nl.name + "name"
return nl
}
func (l *HCLogAdapter) ResetNamed(name string) hclog.Logger {
nl := l.Clone()
nl.name = name
return nl
}
func (l *HCLogAdapter) SetLevel(level hclog.Level) {
switch level {
case hclog.Trace, hclog.Debug:
case hclog.NoLevel, hclog.Info:
case hclog.Warn:
case hclog.Error:
case hclog.Off:
}
}
func (l *HCLogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
return zap.NewStdLog(l.zap)
}
// Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
func (l *HCLogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return nil
}
/*
// Updates the level. This should affect all related loggers as well,
// unless they were created with IndependentLevels. If an
// implementation cannot update the level on the fly, it should no-op.
SetLevel(level hclog.Level)
// Return a value that conforms to the stdlib log.Logger interface
StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger
// Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer
}
*/