Skip to content

Commit 6838c8d

Browse files
authored
Merge pull request #2 from osspkg/dev
fix performance and add adapter for slog
2 parents 418b83f + 99c0a00 commit 6838c8d

14 files changed

+444
-287
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,5 @@ linters:
371371
- exportloopref
372372
- gci
373373
- gosec
374-
- lll
374+
# - lll
375375
fast: false

adapter_slog.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package logx
2+
3+
import (
4+
"context"
5+
"io"
6+
"log/slog"
7+
"os"
8+
)
9+
10+
var slogLevelMap = map[uint32]slog.Level{
11+
LevelError: slog.LevelError,
12+
LevelWarn: slog.LevelWarn,
13+
LevelInfo: slog.LevelInfo,
14+
LevelDebug: slog.LevelDebug,
15+
}
16+
17+
type adapterSlog struct {
18+
level slog.Level
19+
handler func(w io.Writer, level slog.Level) slog.Handler
20+
log *slog.Logger
21+
}
22+
23+
func NewSLogJsonAdapter() Logger {
24+
obj := &adapterSlog{
25+
level: slog.LevelDebug,
26+
handler: func(w io.Writer, level slog.Level) slog.Handler {
27+
return slog.NewJSONHandler(w, &slog.HandlerOptions{
28+
Level: level,
29+
})
30+
},
31+
}
32+
obj.SetOutput(os.Stdout)
33+
return obj
34+
}
35+
36+
func NewSLogStringAdapter() Logger {
37+
obj := &adapterSlog{
38+
level: slog.LevelDebug,
39+
handler: func(w io.Writer, level slog.Level) slog.Handler {
40+
return slog.NewTextHandler(w, &slog.HandlerOptions{
41+
Level: level,
42+
})
43+
},
44+
}
45+
obj.SetOutput(os.Stdout)
46+
return obj
47+
}
48+
49+
func (v *adapterSlog) SetOutput(out io.Writer) {
50+
v.log = slog.New(v.handler(out, v.level))
51+
}
52+
53+
func (v *adapterSlog) SetFormatter(_ Formatter) {}
54+
55+
func (v *adapterSlog) SetLevel(l uint32) {
56+
level, ok := slogLevelMap[l]
57+
if !ok {
58+
level = slog.LevelDebug
59+
}
60+
v.log.Enabled(context.TODO(), level)
61+
}
62+
63+
func (v *adapterSlog) Fatal(message string, args ...interface{}) {
64+
v.log.Error(message, args...)
65+
os.Exit(1)
66+
}
67+
68+
func (v *adapterSlog) Error(message string, args ...interface{}) {
69+
v.log.Error(message, args...)
70+
}
71+
72+
func (v *adapterSlog) Warn(message string, args ...interface{}) {
73+
v.log.Warn(message, args...)
74+
}
75+
76+
func (v *adapterSlog) Info(message string, args ...interface{}) {
77+
v.log.Info(message, args...)
78+
}
79+
80+
func (v *adapterSlog) Debug(message string, args ...interface{}) {
81+
v.log.Debug(message, args...)
82+
}

common.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,22 @@ const (
1616
)
1717

1818
var levels = map[uint32]string{
19-
levelFatal: "FAT",
20-
LevelError: "ERR",
21-
LevelWarn: "WRN",
22-
LevelInfo: "INF",
23-
LevelDebug: "DBG",
24-
}
25-
26-
type Sender interface {
27-
SendMessage(level uint32, call func(v *Message))
28-
Close()
29-
}
30-
31-
// Writer interface
32-
type Writer interface {
33-
Fatal(format string, args ...interface{})
34-
Error(format string, args ...interface{})
35-
Warn(format string, args ...interface{})
36-
Info(format string, args ...interface{})
37-
Debug(format string, args ...interface{})
19+
levelFatal: "FATAL",
20+
LevelError: "ERROR",
21+
LevelWarn: "WARN",
22+
LevelInfo: "INFO",
23+
LevelDebug: "DEBUG",
3824
}
3925

4026
// Logger base interface
4127
type Logger interface {
4228
SetOutput(out io.Writer)
4329
SetFormatter(f Formatter)
4430
SetLevel(v uint32)
45-
GetLevel() uint32
46-
Close()
4731

48-
Writer
32+
Fatal(message string, args ...interface{})
33+
Error(message string, args ...interface{})
34+
Warn(message string, args ...interface{})
35+
Info(message string, args ...interface{})
36+
Debug(message string, args ...interface{})
4937
}

default.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
package logx
77

8-
import "io"
8+
import (
9+
"io"
10+
)
911

10-
var std = New()
12+
var std Logger = New()
13+
14+
func SetDefault(log Logger) {
15+
std = log
16+
}
1117

1218
// Default logger
1319
func Default() Logger {
@@ -23,21 +29,11 @@ func SetFormatter(f Formatter) {
2329
std.SetFormatter(f)
2430
}
2531

26-
// SetLevel change log level
32+
// SetLevel change Log level
2733
func SetLevel(v uint32) {
2834
std.SetLevel(v)
2935
}
3036

31-
// GetLevel getting log level
32-
func GetLevel() uint32 {
33-
return std.GetLevel()
34-
}
35-
36-
// Close waiting for all messages to finish recording
37-
func Close() {
38-
std.Close()
39-
}
40-
4137
// Info message
4238
func Info(format string, args ...interface{}) {
4339
std.Info(format, args...)

formatter.go

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,93 +7,17 @@ package logx
77

88
import (
99
"bytes"
10-
"encoding/json"
11-
"fmt"
12-
"strings"
13-
"time"
10+
"io"
1411

1512
"go.osspkg.com/ioutils/pool"
1613
)
1714

18-
type Formatter interface {
19-
Encode(m *Message) ([]byte, error)
20-
}
21-
2215
var newLine = []byte("\n")
2316

24-
// //////////////////////////////////////////////////////////////////////////////
25-
26-
type FormatJSON struct{}
27-
28-
func NewFormatJSON() *FormatJSON {
29-
return &FormatJSON{}
30-
}
31-
32-
func (*FormatJSON) Encode(m *Message) ([]byte, error) {
33-
m.CtxToMap()
34-
b, err := json.Marshal(m)
35-
if err != nil {
36-
return nil, err
37-
}
38-
return append(b, '\n'), nil
39-
}
40-
41-
// //////////////////////////////////////////////////////////////////////////////
42-
43-
var poolBuff = pool.New[*bytes.Buffer](func() *bytes.Buffer {
17+
var poolBuffer = pool.New[*bytes.Buffer](func() *bytes.Buffer {
4418
return bytes.NewBuffer(make([]byte, 0, 1024))
4519
})
4620

47-
type FormatString struct {
48-
delim string
49-
}
50-
51-
func NewFormatString() *FormatString {
52-
return &FormatString{delim: "\t"}
53-
}
54-
55-
func (v *FormatString) SetDelimiter(d string) {
56-
v.delim = d
57-
}
58-
59-
func (v *FormatString) Encode(m *Message) ([]byte, error) {
60-
buff := poolBuff.Get()
61-
defer func() {
62-
poolBuff.Put(buff)
63-
}()
64-
65-
fmt.Fprintf(buff, "time=%s%slvl=%s%smsg=%#v",
66-
m.Time.Format(time.RFC3339), v.delim, m.Level, v.delim, m.Message)
67-
68-
if count := len(m.Ctx); count > 0 {
69-
if count%2 != 0 {
70-
m.Ctx = append(m.Ctx, nil)
71-
count++
72-
}
73-
for i := 0; i < count; i = i + 2 {
74-
fmt.Fprintf(buff, "%s%s=\"%s\"", v.delim, typing(m.Ctx[i]), typing(m.Ctx[i+1]))
75-
}
76-
}
77-
buff.Write(newLine)
78-
79-
return append(make([]byte, 0, buff.Len()), buff.Bytes()...), nil
80-
}
81-
82-
func typing(v interface{}) (s string) {
83-
if v == nil {
84-
s = "null"
85-
return
86-
}
87-
switch vv := v.(type) {
88-
case error:
89-
s = vv.Error()
90-
case fmt.GoStringer:
91-
s = vv.GoString()
92-
case fmt.Stringer:
93-
s = vv.String()
94-
default:
95-
s = fmt.Sprintf("%#v", v)
96-
}
97-
s = strings.Trim(s, "\"")
98-
return
21+
type Formatter interface {
22+
Encode(w io.Writer, m *Message) error
9923
}

formatter_json.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package logx
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
9+
"go.osspkg.com/ioutils/pool"
10+
)
11+
12+
type FormatJSON struct{}
13+
14+
func NewFormatJSON() *FormatJSON {
15+
return &FormatJSON{}
16+
}
17+
18+
func (*FormatJSON) Encode(out io.Writer, m *Message) error {
19+
m.CtxToMap()
20+
21+
w := poolJson.Get()
22+
defer func() {
23+
poolJson.Put(w)
24+
}()
25+
26+
if err := w.Encoder.Encode(m); err != nil {
27+
return fmt.Errorf("logx json encode: %w", err)
28+
}
29+
w.Buffer.Write(newLine)
30+
if _, err := w.Buffer.WriteTo(out); err != nil {
31+
return fmt.Errorf("logx json write: %w", err)
32+
}
33+
return nil
34+
}
35+
36+
var poolJson = pool.New[*jsonWriter](func() *jsonWriter {
37+
obj := &jsonWriter{
38+
Buffer: bytes.NewBuffer(nil),
39+
}
40+
obj.Encoder = json.NewEncoder(obj.Buffer)
41+
return obj
42+
})
43+
44+
type jsonWriter struct {
45+
Buffer *bytes.Buffer
46+
Encoder *json.Encoder
47+
}
48+
49+
func (v *jsonWriter) Reset() {
50+
v.Buffer.Reset()
51+
}

0 commit comments

Comments
 (0)