@@ -2,6 +2,7 @@ package log
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"io"
6
7
"log"
7
8
@@ -15,33 +16,54 @@ type HclogAdapter struct {
15
16
log Logger
16
17
}
17
18
19
+ // NewHclogAdapter creates an HclogAdapter for a given logger.
18
20
func NewHclogAdapter (log Logger ) * HclogAdapter {
19
21
return & HclogAdapter {log : log }
20
22
}
21
23
22
24
// Trace implementation
23
25
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
+ }
25
31
}
26
32
27
33
// Debug implementation
28
34
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
+ }
30
40
}
31
41
32
42
// Info implementation
33
43
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
+ }
35
49
}
36
50
37
51
// Warn implementation
38
52
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
+ }
40
58
}
41
59
42
60
// Error implementation
43
61
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
+ }
45
67
}
46
68
47
69
// IsTrace implementation.
@@ -114,9 +136,7 @@ func (l HclogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Write
114
136
func argsToString (args ... interface {}) string {
115
137
buf := bytes.Buffer {}
116
138
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 ]))
120
140
}
121
141
return buf .String ()
122
142
}
0 commit comments