-
Notifications
You must be signed in to change notification settings - Fork 4
/
hclogger.go
182 lines (154 loc) · 4.03 KB
/
hclogger.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
package common
import (
"io"
"log"
"github.com/hashicorp/go-hclog"
"github.com/sirupsen/logrus"
)
type LogrusHclogger struct {
logger *logrus.Logger
entry *logrus.Entry
name string
}
func (l LogrusHclogger) GetLevel() hclog.Level {
switch l.logger.GetLevel() {
case logrus.InfoLevel:
return hclog.Info
case logrus.TraceLevel:
return hclog.Trace
case logrus.DebugLevel:
return hclog.Debug
case logrus.WarnLevel:
return hclog.Warn
case logrus.ErrorLevel:
return hclog.Error
}
return hclog.NoLevel
}
func NewLogrusHclogger(logger *logrus.Logger) *LogrusHclogger {
return &LogrusHclogger{logger: logger, entry: logrus.NewEntry(logger)}
}
//const (
// // NoLevel is a special level used to indicate that no level has been
// // set and allow for a default to be used.
// NoLevel Level = 0
//
// // Trace is the most verbose level. Intended to be used for the tracing
// // of actions in code, such as function enters/exits, etc.
// Trace Level = 1
//
// // Debug information for programmer lowlevel analysis.
// Debug Level = 2
//
// // Info information about steady state operations.
// Info Level = 3
//
// // Warn information about rare but handled events.
// Warn Level = 4
//
// // Error information about unrecoverable events.
// Error Level = 5
//)
func (l LogrusHclogger) Log(level hclog.Level, msg string, args ...interface{}) {
switch level {
case hclog.NoLevel:
l.Info(msg, args...)
case hclog.Trace:
l.Trace(msg, args...)
case hclog.Debug:
l.Debug(msg, args...)
case hclog.Info:
l.Info(msg, args...)
case hclog.Warn:
l.Warn(msg, args...)
case hclog.Error:
l.Error(msg, args...)
}
}
func (l LogrusHclogger) Trace(msg string, args ...interface{}) {
l.CreateEntry(args).Trace(msg)
}
func (l LogrusHclogger) Debug(msg string, args ...interface{}) {
l.CreateEntry(args).Debug(msg)
}
func (l LogrusHclogger) Info(msg string, args ...interface{}) {
l.CreateEntry(args).Info(msg)
}
func (l LogrusHclogger) Warn(msg string, args ...interface{}) {
l.CreateEntry(args).Warn(msg)
}
func (l LogrusHclogger) Error(msg string, args ...interface{}) {
l.CreateEntry(args).Error(msg)
}
func (l LogrusHclogger) IsTrace() bool {
return l.logger.GetLevel() == logrus.TraceLevel
}
func (l LogrusHclogger) IsDebug() bool {
return l.logger.GetLevel() >= logrus.DebugLevel
}
func (l LogrusHclogger) IsInfo() bool {
return l.logger.GetLevel() >= logrus.InfoLevel
}
func (l LogrusHclogger) IsWarn() bool {
return l.logger.GetLevel() >= logrus.WarnLevel
}
func (l LogrusHclogger) IsError() bool {
return l.logger.GetLevel() >= logrus.ErrorLevel
}
func (l LogrusHclogger) ImpliedArgs() []interface{} {
return []interface{}{}
}
func (l *LogrusHclogger) With(args ...interface{}) hclog.Logger {
l.entry = l.CreateEntry(args)
return l
}
func (l LogrusHclogger) Name() string {
return l.name
}
func (l *LogrusHclogger) Named(name string) hclog.Logger {
l.entry = l.entry.WithField("name", name)
l.name = name
return l
}
func (l *LogrusHclogger) ResetNamed(name string) hclog.Logger {
l.entry = logrus.NewEntry(l.logger)
l.name = ""
return l
}
func (l LogrusHclogger) SetLevel(level hclog.Level) {
switch level {
case hclog.NoLevel:
l.logger.SetLevel(logrus.InfoLevel)
case hclog.Trace:
l.logger.SetLevel(logrus.TraceLevel)
case hclog.Debug:
l.logger.SetLevel(logrus.DebugLevel)
case hclog.Info:
l.logger.SetLevel(logrus.InfoLevel)
case hclog.Warn:
l.logger.SetLevel(logrus.WarnLevel)
case hclog.Error:
l.logger.SetLevel(logrus.ErrorLevel)
}
}
func (l LogrusHclogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
return log.New(logrus.StandardLogger().Out, "", log.LstdFlags)
}
func (l LogrusHclogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return l.logger.Writer()
}
func (l LogrusHclogger) CreateEntry(args []interface{}) *logrus.Entry {
if len(args)%2 != 0 {
args = append(args, "<unknown>")
}
fields := make(logrus.Fields)
for i := 0; i < len(args); i += 2 {
k, ok := args[i].(string)
if !ok {
continue
}
v := args[i+1]
fields[k] = v
}
return l.entry.WithFields(fields)
}