Skip to content

Commit 070abb3

Browse files
authored
fix all (#4)
1 parent 4468f0d commit 070abb3

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

attr.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ type Attr struct {
55
key string
66
kfield kField
77

8-
intValue int
9-
int16Value int16
10-
int32Value int32
11-
int64Value int64
12-
stringValue string
13-
boolValue bool
8+
intValue int
9+
int16Value int16
10+
int32Value int32
11+
int64Value int64
12+
stringValue string
13+
boolValue bool
14+
stringSliceValue []string
1415

1516
anyValue any
1617

@@ -52,6 +53,11 @@ func Any(key string, value any) Attr {
5253
return Attr{key: key, kfield: kany, anyValue: value}
5354
}
5455

56+
// StringSlice
57+
func StringSlice(key string, value []string) Attr {
58+
return Attr{key: key, kfield: kstringslice, stringSliceValue: value}
59+
}
60+
5561
// Error
5662
func Error(err error) Attr {
5763
return Attr{key: "error", kfield: kerr, errValue: err}

const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ const (
2929
kbool
3030
kany
3131
kstring
32+
kstringslice
3233
kerr
3334
)

log.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"runtime"
77
"strconv"
8+
"strings"
89
"sync"
910
"time"
1011
)
@@ -54,18 +55,8 @@ func (l *Logger) Debug(format string, attrs ...Attr) {
5455
l.printf(format, DEBUG, attrs...)
5556
}
5657

57-
// Debugf
58-
func (l *Logger) Debugf(format string, attrs ...Attr) {
59-
l.printf(format, INFO, attrs...)
60-
}
61-
6258
// Info
63-
func (l *Logger) Info(format string) {
64-
l.printf(format, INFO)
65-
}
66-
67-
// Infof
68-
func (l *Logger) Infof(format string, attrs ...Attr) {
59+
func (l *Logger) Info(format string, attrs ...Attr) {
6960
l.printf(format, INFO, attrs...)
7061
}
7162

@@ -118,6 +109,9 @@ func (l *Logger) printf(format string, level Level, attrs ...Attr) {
118109
buf = strconv.AppendBool(buf, attr.boolValue)
119110
case kstring:
120111
buf = strconv.AppendQuote(buf, attr.stringValue)
112+
case kstringslice:
113+
s := strings.Join(attr.stringSliceValue, ",")
114+
buf = strconv.AppendQuote(buf, s)
121115
case kerr:
122116
buf = strconv.AppendQuote(buf, attr.errValue.Error())
123117
}

log_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ func TestLogger_JSON(t *testing.T) {
3535
WithWriter(&buf),
3636
)
3737

38-
l.Infof("hello",
38+
l.Info("hello",
3939
String("user", "alice"),
40+
StringSlice("x", []string{"hello", "world"}),
4041
)
4142

43+
fmt.Println(buf.String())
44+
4245
var result map[string]interface{}
4346
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
4447
t.Fatalf("json.Unmarshal: %v", err)

0 commit comments

Comments
 (0)