Skip to content

Commit c2aba15

Browse files
committed
go1.24
1 parent 816498a commit c2aba15

File tree

10 files changed

+121
-76
lines changed

10 files changed

+121
-76
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
go: [ '1.23' ]
15+
go: [ '1.24' ]
1616
steps:
1717
- uses: actions/checkout@v3
1818

.gitignore

100644100755
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1+
2+
.tools/
3+
bin/
4+
vendor/
5+
build/
6+
.idea/
7+
.vscode/
8+
coverage.txt
9+
coverage.out
110
*.exe
211
*.exe~
312
*.dll
413
*.so
514
*.dylib
6-
*.test
7-
*.out
815
*.db
9-
*.db-shm
10-
*.db-wal
1116
*.db-journal
12-
*.dev.yaml
1317
*.mmdb
14-
.env
15-
16-
.idea/
17-
.vscode/
18-
.tools/
19-
20-
coverage.txt
21-
coverage.out
22-
23-
bin/
24-
vendor/
25-
build/
26-
18+
*.test
19+
*.out
20+
.env

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: "2"
22

33
run:
4-
go: "1.23"
4+
go: "1.24"
55
timeout: 5m
66
tests: false
77
issues-exit-code: 1

formatter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
package logx
77

88
import (
9-
"bytes"
109
"io"
1110

11+
"go.osspkg.com/ioutils/data"
1212
"go.osspkg.com/ioutils/pool"
1313
)
1414

1515
var newLine = []byte("\n")
1616

17-
var poolBuffer = pool.New[*bytes.Buffer](func() *bytes.Buffer {
18-
return bytes.NewBuffer(make([]byte, 0, 1024))
17+
var poolBuffer = pool.New[*data.Buffer](func() *data.Buffer {
18+
return data.NewBuffer(1024)
1919
})
2020

2121
type Formatter interface {

formatter_strings.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
package logx
77

88
import (
9-
"bytes"
9+
"encoding"
1010
"fmt"
1111
"io"
1212
"strings"
1313
"time"
14+
15+
"go.osspkg.com/ioutils/data"
1416
)
1517

1618
type FormatString struct {
@@ -25,13 +27,13 @@ func (v *FormatString) SetDelimiter(d byte) {
2527
v.delim = d
2628
}
2729

28-
func (v *FormatString) write(w *bytes.Buffer, key, value interface{}) {
29-
w.WriteByte('"')
30-
w.WriteString(typing(key))
31-
w.WriteString("\"=\"")
32-
w.WriteString(typing(value))
33-
w.WriteByte('"')
34-
w.WriteByte(v.delim)
30+
func (v *FormatString) write(w *data.Buffer, key, value interface{}) {
31+
w.WriteByte('"') //nolint:errcheck
32+
w.WriteString(typing(key)) //nolint:errcheck
33+
w.WriteString("\"=\"") //nolint:errcheck
34+
w.WriteString(typing(value)) //nolint:errcheck
35+
w.WriteByte('"') //nolint:errcheck
36+
w.WriteByte(v.delim) //nolint:errcheck
3537
}
3638

3739
func (v *FormatString) Encode(out io.Writer, m *Message) error {
@@ -53,7 +55,7 @@ func (v *FormatString) Encode(out io.Writer, m *Message) error {
5355
v.write(w, m.Ctx[i], m.Ctx[i+1])
5456
}
5557
}
56-
w.Write(newLine)
58+
w.Write(newLine) //nolint:errcheck
5759
if _, err := w.WriteTo(out); err != nil {
5860
return fmt.Errorf("logx string write: %w", err)
5961
}
@@ -71,6 +73,14 @@ func typing(v interface{}) string {
7173
v = vv.GoString()
7274
case fmt.Stringer:
7375
v = vv.String()
76+
case encoding.TextMarshaler:
77+
if b, err := vv.MarshalText(); err == nil {
78+
v = string(b)
79+
}
80+
case encoding.BinaryMarshaler:
81+
if b, err := vv.MarshalBinary(); err == nil {
82+
v = string(b)
83+
}
7484
case []byte:
7585
v = string(vv)
7686
default:

formatter_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ func TestUnit_FormatString_Encode(t *testing.T) {
5454
}
5555

5656
func TestUnit_debug(t *testing.T) {
57+
t.SkipNow()
58+
5759
var w bytes.Buffer
5860
fj := logx.NewFormatJSON()
59-
fj.Encode(&w, &logx.Message{})
60-
fj.Encode(&w, &logx.Message{})
61+
fj.Encode(&w, &logx.Message{Ctx: []any{"a\"\na", "a\nb\n"}, Map: make(map[string]string)})
62+
fj.Encode(&w, &logx.Message{Message: "a\"\nb\n"})
6163
fj.Encode(&w, &logx.Message{})
6264

6365
fs := logx.NewFormatString()
64-
fs.Encode(&w, &logx.Message{Ctx: []any{"a\na", "a\nb\n"}})
65-
fs.Encode(&w, &logx.Message{Message: "a\nb\n"})
66+
fs.Encode(&w, &logx.Message{Ctx: []any{"a\"\na", "a\nb\n"}})
67+
fs.Encode(&w, &logx.Message{Message: "a\"\nb\n"})
68+
fs.Encode(&w, &logx.Message{})
6669

6770
result := string(w.Bytes())
6871
wait := `{"time":"0001-01-01T00:00:00Z","level":"","msg":""}
@@ -72,5 +75,5 @@ func TestUnit_debug(t *testing.T) {
7275
"time"="0001-01-01T00:00:00Z" "level"="" "msg"="a\nb\n"
7376
`
7477

75-
casecheck.Equal(t, result, wait)
78+
casecheck.Equal(t, wait, result)
7679
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module go.osspkg.com/logx
22

3-
go 1.23.11
3+
go 1.24.0
44

55
require (
6-
github.com/mailru/easyjson v0.9.0
6+
github.com/mailru/easyjson v0.9.1
77
go.osspkg.com/casecheck v0.3.0
8-
go.osspkg.com/ioutils v0.6.2
9-
go.osspkg.com/syncing v0.3.1
8+
go.osspkg.com/ioutils v0.7.0
9+
go.osspkg.com/syncing v0.4.0
1010
)
1111

1212
require github.com/josharian/intern v1.0.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
22
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
3-
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
4-
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
3+
github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8=
4+
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
55
go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
66
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
7-
go.osspkg.com/ioutils v0.6.2 h1:jIY6xpFXeBlsicx1e7ge8fVmoeuulMV7/+rRbBeTYl4=
8-
go.osspkg.com/ioutils v0.6.2/go.mod h1:uMobJ2x8HaBV80FQuGW97PGw1ln04g4xIXudfB93jWs=
9-
go.osspkg.com/syncing v0.3.1 h1:zt5o/X5DQ/GE5OQTKkq1nNWJMg7EcYhw0YiwMGuA0f8=
10-
go.osspkg.com/syncing v0.3.1/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
7+
go.osspkg.com/ioutils v0.7.0 h1:FMQCD1X0tENdxa4cG9Fob++a8PnkgDMDuiN4EeXkCIc=
8+
go.osspkg.com/ioutils v0.7.0/go.mod h1:+W9rQK1Bmn4Wjs+bpC5x5DFoq48gq6WazytzoQsh/lE=
9+
go.osspkg.com/syncing v0.4.0 h1:9ytMfGHd6Ew69D2n/1syj0FTujNfb1KBiUSZ+KsMTqk=
10+
go.osspkg.com/syncing v0.4.0/go.mod h1:/LBmgCAHFW6nQgVDILpEuo6eRCFK1yyFeNbDs4eVNls=

logger_test.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package logx_test
77

88
import (
99
"bytes"
10+
"context"
1011
"fmt"
1112
"io"
1213
"sync"
@@ -23,6 +24,11 @@ type mockWriter struct {
2324
l sync.Mutex
2425
}
2526

27+
type testData struct {
28+
A string
29+
B int
30+
}
31+
2632
func newMockWriter() *mockWriter {
2733
return &mockWriter{
2834
b: bytes.NewBuffer(nil),
@@ -45,16 +51,16 @@ func (v *mockWriter) String() string {
4551
func TestUnit_NewJSON(t *testing.T) {
4652
casecheck.NotNil(t, logx.Default())
4753

48-
wg := syncing.NewGroup()
54+
wg := syncing.NewGroup(context.TODO())
4955
buff := newMockWriter()
5056

5157
logx.SetOutput(buff)
5258
logx.SetLevel(logx.LevelDebug)
5359

54-
wg.Background(func() { logx.Info("async", "id", 1) })
55-
wg.Background(func() { logx.Warn("async", "id", 2) })
56-
wg.Background(func() { logx.Error("async", "id", 3) })
57-
wg.Background(func() { logx.Debug("async", "id", 4) })
60+
wg.Background("", func(_ context.Context) { logx.Info("async", "id", 1) })
61+
wg.Background("", func(_ context.Context) { logx.Warn("async", "id", 2) })
62+
wg.Background("", func(_ context.Context) { logx.Error("async", "id", 3) })
63+
wg.Background("", func(_ context.Context) { logx.Debug("async", "id", 4) })
5864

5965
logx.Info("sync", "id", 1)
6066
logx.Warn("sync", "id", 2)
@@ -65,6 +71,7 @@ func TestUnit_NewJSON(t *testing.T) {
6571
logx.Info("context2", "nil", nil)
6672
logx.Info("context3", "func", func() {})
6773
logx.Info("context4", "err", fmt.Errorf("er1"))
74+
logx.Info("context5", "obj", testData{})
6875

6976
wg.Wait()
7077

@@ -81,10 +88,11 @@ func TestUnit_NewJSON(t *testing.T) {
8188
casecheck.Contains(t, data, `"level":"INFO","msg":"context2","ctx":{"nil":"null"}`)
8289
casecheck.Contains(t, data, `"level":"INFO","msg":"context3","ctx":{"func":"(func())(0x`)
8390
casecheck.Contains(t, data, `"level":"INFO","msg":"context4","ctx":{"err":"er1"}`)
91+
casecheck.Contains(t, data, `"level":"INFO","msg":"context5","ctx":{"obj":"logx_test.testData{A:\"\", B:0}"}`)
8492
}
8593

8694
func TestUnit_NewString(t *testing.T) {
87-
wg := syncing.NewGroup()
95+
wg := syncing.NewGroup(context.TODO())
8896
buff := newMockWriter()
8997

9098
l := logx.New()
@@ -94,10 +102,10 @@ func TestUnit_NewString(t *testing.T) {
94102
l.SetLevel(logx.LevelDebug)
95103
casecheck.Equal(t, logx.LevelDebug, l.GetLevel())
96104

97-
wg.Background(func() { l.Info("async", "id", 1) })
98-
wg.Background(func() { l.Warn("async", "id", 2) })
99-
wg.Background(func() { l.Error("async", "id", 3) })
100-
wg.Background(func() { l.Debug("async", "id", 4) })
105+
wg.Background("", func(_ context.Context) { l.Info("async", "id", 1) })
106+
wg.Background("", func(_ context.Context) { l.Warn("async", "id", 2) })
107+
wg.Background("", func(_ context.Context) { l.Error("async", "id", 3) })
108+
wg.Background("", func(_ context.Context) { l.Debug("async", "id", 4) })
101109

102110
l.Info("sync", "id", 1)
103111
l.Warn("sync", "id", 2)
@@ -108,6 +116,7 @@ func TestUnit_NewString(t *testing.T) {
108116
l.Info("context2", "nil", nil)
109117
l.Info("context3", "func", func() {})
110118
l.Info("context4", "err", fmt.Errorf("er1"))
119+
l.Info("context5", "obj", testData{})
111120

112121
wg.Wait()
113122

@@ -124,10 +133,11 @@ func TestUnit_NewString(t *testing.T) {
124133
casecheck.Contains(t, data, "\"level\"=\"INFO\"\t\"msg\"=\"context2\"\t\"nil\"=\"null\"")
125134
casecheck.Contains(t, data, "\"level\"=\"INFO\"\t\"msg\"=\"context3\"\t\"func\"=\"(func())(0x")
126135
casecheck.Contains(t, data, "\"level\"=\"INFO\"\t\"msg\"=\"context4\"\t\"err\"=\"er1\"")
136+
casecheck.Contains(t, data, "\"level\"=\"INFO\"\t\"msg\"=\"context5\"\t\"obj\"=\"logx_test.testData{A:\"\", B:0}\"")
127137
}
128138

129139
func TestUnit_NewSlog(t *testing.T) {
130-
wg := syncing.NewGroup()
140+
wg := syncing.NewGroup(context.TODO())
131141
buff := newMockWriter()
132142

133143
l := logx.NewSLogJsonAdapter()
@@ -136,10 +146,10 @@ func TestUnit_NewSlog(t *testing.T) {
136146
l.SetOutput(buff)
137147
l.SetLevel(logx.LevelDebug)
138148

139-
wg.Background(func() { l.Info("async", "id", 1) })
140-
wg.Background(func() { l.Warn("async", "id", 2) })
141-
wg.Background(func() { l.Error("async", "id", 3) })
142-
wg.Background(func() { l.Debug("async", "id", 4) })
149+
wg.Background("", func(_ context.Context) { l.Info("async", "id", 1) })
150+
wg.Background("", func(_ context.Context) { l.Warn("async", "id", 2) })
151+
wg.Background("", func(_ context.Context) { l.Error("async", "id", 3) })
152+
wg.Background("", func(_ context.Context) { l.Debug("async", "id", 4) })
143153

144154
l.Info("sync", "id", 1)
145155
l.Warn("sync", "id", 2)
@@ -150,6 +160,7 @@ func TestUnit_NewSlog(t *testing.T) {
150160
l.Info("context2", "nil", nil)
151161
l.Info("context3", "func", func() {})
152162
l.Info("context4", "err", fmt.Errorf("er1"))
163+
l.Info("context5", "obj", testData{})
153164

154165
wg.Wait()
155166

@@ -166,8 +177,24 @@ func TestUnit_NewSlog(t *testing.T) {
166177
casecheck.Contains(t, data, "\"level\":\"INFO\",\"msg\":\"context2\",\"nil\":null")
167178
casecheck.Contains(t, data, "\"level\":\"INFO\",\"msg\":\"context3\",\"func\":\"!ERROR:json: unsupported type: func()\"")
168179
casecheck.Contains(t, data, "\"level\":\"INFO\",\"msg\":\"context4\",\"err\":\"er1\"")
180+
casecheck.Contains(t, data, "\"level\":\"INFO\",\"msg\":\"context5\",\"obj\":{\"A\":\"\",\"B\":0}")
169181
}
170182

183+
/*
184+
goos: linux
185+
goarch: amd64
186+
pkg: go.osspkg.com/logx
187+
cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
188+
BenchmarkNewJSON
189+
BenchmarkNewJSON-24 152106 9702 ns/op 66942 B/op 13 allocs/op
190+
BenchmarkNewString
191+
BenchmarkNewString-24 130413 9213 ns/op 66085 B/op 15 allocs/op
192+
BenchmarkNewSLogJsonAdapter
193+
BenchmarkNewSLogJsonAdapter-24 3046932 484.7 ns/op 201 B/op 4 allocs/op
194+
BenchmarkNewSLogStringAdapter
195+
BenchmarkNewSLogStringAdapter-24 4683210 308.7 ns/op 96 B/op 3 allocs/op
196+
*/
197+
171198
func BenchmarkNewJSON(b *testing.B) {
172199
ll := logx.New()
173200
ll.SetOutput(io.Discard)
@@ -178,7 +205,7 @@ func BenchmarkNewJSON(b *testing.B) {
178205
b.ResetTimer()
179206
b.RunParallel(func(pb *testing.PB) {
180207
for pb.Next() {
181-
ll.Info("sync", "id", 1)
208+
ll.Info("sync", "id", 1, "obj", testData{})
182209
}
183210
})
184211
}
@@ -193,7 +220,7 @@ func BenchmarkNewString(b *testing.B) {
193220
b.ResetTimer()
194221
b.RunParallel(func(pb *testing.PB) {
195222
for pb.Next() {
196-
ll.Info("sync", "id", 1)
223+
ll.Info("sync", "id", 1, "obj", testData{})
197224
}
198225
})
199226
}
@@ -207,7 +234,7 @@ func BenchmarkNewSLogJsonAdapter(b *testing.B) {
207234
b.ResetTimer()
208235
b.RunParallel(func(pb *testing.PB) {
209236
for pb.Next() {
210-
ll.Info("sync", "id", 1)
237+
ll.Info("sync", "id", 1, "obj", testData{})
211238
}
212239
})
213240
}
@@ -221,7 +248,7 @@ func BenchmarkNewSLogStringAdapter(b *testing.B) {
221248
b.ResetTimer()
222249
b.RunParallel(func(pb *testing.PB) {
223250
for pb.Next() {
224-
ll.Info("sync", "id", 1)
251+
ll.Info("sync", "id", 1, "obj", testData{})
225252
}
226253
})
227254
}

0 commit comments

Comments
 (0)