Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ type Attr struct {
key string
kfield kField

intValue int
int16Value int16
int32Value int32
int64Value int64
stringValue string
boolValue bool
intValue int
int16Value int16
int32Value int32
int64Value int64
stringValue string
boolValue bool
stringSliceValue []string

anyValue any

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

// StringSlice
func StringSlice(key string, value []string) Attr {
return Attr{key: key, kfield: kstringslice, stringSliceValue: value}
}

// Error
func Error(err error) Attr {
return Attr{key: "error", kfield: kerr, errValue: err}
Expand Down
1 change: 1 addition & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ const (
kbool
kany
kstring
kstringslice
kerr
)
16 changes: 5 additions & 11 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -54,18 +55,8 @@ func (l *Logger) Debug(format string, attrs ...Attr) {
l.printf(format, DEBUG, attrs...)
}

// Debugf
func (l *Logger) Debugf(format string, attrs ...Attr) {
l.printf(format, INFO, attrs...)
}

// Info
func (l *Logger) Info(format string) {
l.printf(format, INFO)
}

// Infof
func (l *Logger) Infof(format string, attrs ...Attr) {
func (l *Logger) Info(format string, attrs ...Attr) {
l.printf(format, INFO, attrs...)
}

Expand Down Expand Up @@ -118,6 +109,9 @@ func (l *Logger) printf(format string, level Level, attrs ...Attr) {
buf = strconv.AppendBool(buf, attr.boolValue)
case kstring:
buf = strconv.AppendQuote(buf, attr.stringValue)
case kstringslice:
s := strings.Join(attr.stringSliceValue, ",")
buf = strconv.AppendQuote(buf, s)
case kerr:
buf = strconv.AppendQuote(buf, attr.errValue.Error())
}
Expand Down
5 changes: 4 additions & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ func TestLogger_JSON(t *testing.T) {
WithWriter(&buf),
)

l.Infof("hello",
l.Info("hello",
String("user", "alice"),
StringSlice("x", []string{"hello", "world"}),
)

fmt.Println(buf.String())

var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("json.Unmarshal: %v", err)
Expand Down
Loading