Skip to content

Commit c4023e5

Browse files
committedSep 30, 2019
Fix hclog adapter
1 parent 6a0ddc5 commit c4023e5

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed
 

‎log/hclog.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"log"
78

@@ -15,33 +16,54 @@ type HclogAdapter struct {
1516
log Logger
1617
}
1718

19+
// NewHclogAdapter creates an HclogAdapter for a given logger.
1820
func NewHclogAdapter(log Logger) *HclogAdapter {
1921
return &HclogAdapter{log: log}
2022
}
2123

2224
// Trace implementation
2325
func (l HclogAdapter) Trace(msg string, args ...interface{}) {
24-
l.log.Tracef("%s: %v", msg, argsToString(args...))
26+
if len(args) > 0 {
27+
l.log.Tracef("%s: %v", msg, argsToString(args...))
28+
} else {
29+
l.log.Trace(msg)
30+
}
2531
}
2632

2733
// Debug implementation
2834
func (l HclogAdapter) Debug(msg string, args ...interface{}) {
29-
l.log.Debugf("%s: %v", msg, argsToString(args...))
35+
if len(args) > 0 {
36+
l.log.Debugf("%s: %v", msg, argsToString(args...))
37+
} else {
38+
l.log.Debug(msg)
39+
}
3040
}
3141

3242
// Info implementation
3343
func (l HclogAdapter) Info(msg string, args ...interface{}) {
34-
l.log.Infof("%s: %v", msg, argsToString(args...))
44+
if len(args) > 0 {
45+
l.log.Infof("%s: %v", msg, argsToString(args...))
46+
} else {
47+
l.log.Info(msg)
48+
}
3549
}
3650

3751
// Warn implementation
3852
func (l HclogAdapter) Warn(msg string, args ...interface{}) {
39-
l.log.Warnf("%s: %v", msg, argsToString(args...))
53+
if len(args) > 0 {
54+
l.log.Warnf("%s: %v", msg, argsToString(args...))
55+
} else {
56+
l.log.Warn(msg)
57+
}
4058
}
4159

4260
// Error implementation
4361
func (l HclogAdapter) Error(msg string, args ...interface{}) {
44-
l.log.Errorf("%s: %v", msg, argsToString(args...))
62+
if len(args) > 0 {
63+
l.log.Errorf("%s: %v", msg, argsToString(args...))
64+
} else {
65+
l.log.Error(msg)
66+
}
4567
}
4668

4769
// IsTrace implementation.
@@ -114,9 +136,7 @@ func (l HclogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Write
114136
func argsToString(args ...interface{}) string {
115137
buf := bytes.Buffer{}
116138
for i := 0; i < len(args); i += 2 {
117-
buf.WriteString(args[i].(string))
118-
buf.WriteByte('=')
119-
buf.WriteString(args[i+1].(string))
139+
buf.WriteString(fmt.Sprintf("%v=%v", args[i], args[i+1]))
120140
}
121141
return buf.String()
122142
}

0 commit comments

Comments
 (0)