Skip to content

Commit 3e3605c

Browse files
author
New year
committed
conn: change design of tarantool logger
- Used slog. - Added events of logger. Fixed #504
1 parent 6a24a64 commit 3e3605c

File tree

1 file changed

+110
-37
lines changed

1 file changed

+110
-37
lines changed

connection.go

Lines changed: 110 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"log"
11+
"log/slog"
1212
"math"
1313
"net"
1414
"runtime"
@@ -52,17 +52,17 @@ const (
5252
Closed
5353

5454
// LogReconnectFailed is logged when reconnect attempt failed.
55-
LogReconnectFailed ConnLogKind = iota + 1
55+
// LogReconnectFailed ConnLogKind = iota + 1
5656
// LogLastReconnectFailed is logged when last reconnect attempt failed,
5757
// connection will be closed after that.
58-
LogLastReconnectFailed
58+
// LogLastReconnectFailed
5959
// LogUnexpectedResultId is logged when response with unknown id was received.
6060
// Most probably it is due to request timeout.
6161
LogUnexpectedResultId
6262
// LogWatchEventReadFailed is logged when failed to read a watch event.
63-
LogWatchEventReadFailed
64-
// LogBoxSessionPushUnsupported is logged when response type turned IPROTO_CHUNK.
65-
LogBoxSessionPushUnsupported
63+
// LogWatchEventReadFailed
64+
// LogBoxSessionPushUnsupported is logged when response type turned IPROTO_CHUNK.
65+
// LogBoxSessionPushUnsupported
6666
)
6767

6868
// ConnEvent is sent throw Notify channel specified in Opts.
@@ -80,50 +80,123 @@ type connWatchEvent struct {
8080

8181
var epoch = time.Now()
8282

83+
type ConnLogEvent interface{}
84+
85+
// Events of logger
86+
type LogReconnectFailed struct {
87+
Reconnects uint
88+
MaxReconnects uint
89+
Err error
90+
}
91+
92+
type LogLastReconnectFailed struct {
93+
Err error
94+
}
95+
type LogUnexpectedResultID struct {
96+
Header Header
97+
}
98+
type LogWatchEventReadFailed struct {
99+
Err error
100+
}
101+
type LogBoxSessionPushUnsupported struct {
102+
Header Header
103+
}
104+
83105
// Logger is logger type expected to be passed in options.
84106
type Logger interface {
85-
Report(event ConnLogKind, conn *Connection, v ...interface{})
107+
Report(event ConnLogEvent, conn *Connection)
108+
}
109+
110+
type defaultSlogLogger struct {
111+
l *slog.Logger
112+
}
113+
114+
func NewDefaultSlogLogger(l *slog.Logger) Logger {
115+
if l == nil {
116+
l = slog.Default()
117+
}
118+
return &defaultSlogLogger{l: l}
86119
}
87120

88121
type defaultLogger struct{}
89122

90-
func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interface{}) {
91-
switch event {
123+
func attrsToAny(attrs []slog.Attr) []any {
124+
out := make([]any, 0, len(attrs)*2)
125+
for _, a := range attrs {
126+
out = append(out, a.Key, a.Value.Any())
127+
}
128+
return out
129+
}
130+
131+
func (d *defaultSlogLogger) Report(event ConnLogEvent, conn *Connection) {
132+
level, msg, attrs := eventToSlog(event, conn)
133+
args := attrsToAny(attrs)
134+
ctx := context.Background()
135+
switch level {
136+
case slog.LevelError:
137+
d.l.Error(msg, args...)
138+
case slog.LevelWarn:
139+
d.l.Warn(msg, args...)
140+
case slog.LevelInfo:
141+
d.l.Info(msg, args...)
142+
case slog.LevelDebug:
143+
d.l.Debug(msg, args...)
144+
default:
145+
d.l.Log(ctx, level, msg, args...)
146+
}
147+
}
148+
149+
func eventToSlog(event ConnLogEvent, conn *Connection) (slog.Level, string, []slog.Attr) {
150+
addr := "<nil>"
151+
if conn != nil && conn.Addr() != nil {
152+
addr = fmt.Sprintf("%s", conn.Addr())
153+
}
154+
155+
switch e := event.(type) {
92156
case LogReconnectFailed:
93-
reconnects := v[0].(uint)
94-
err := v[1].(error)
95-
addr := conn.Addr()
96-
if addr == nil {
97-
log.Printf("tarantool: connect (%d/%d) failed: %s",
98-
reconnects, conn.opts.MaxReconnects, err)
99-
} else {
100-
log.Printf("tarantool: reconnect (%d/%d) to %s failed: %s",
101-
reconnects, conn.opts.MaxReconnects, addr, err)
157+
msg := "tarantool: reconnect failed"
158+
attrs := []slog.Attr{
159+
slog.Int("reconnects", int(e.Reconnects)),
160+
slog.Int("max_reconnects", int(e.MaxReconnects)),
161+
slog.String("addr", addr),
162+
}
163+
if e.Err != nil {
164+
attrs = append(attrs, slog.String("error", e.Err.Error()))
102165
}
166+
return slog.LevelWarn, msg, attrs
167+
103168
case LogLastReconnectFailed:
104-
err := v[0].(error)
105-
addr := conn.Addr()
106-
if addr == nil {
107-
log.Printf("tarantool: last connect failed: %s, giving it up",
108-
err)
109-
} else {
110-
log.Printf("tarantool: last reconnect to %s failed: %s, giving it up",
111-
addr, err)
169+
msg := "tarantool: last reconnect failed, giving it up"
170+
attrs := []slog.Attr{slog.String("addr", addr)}
171+
if e.Err != nil {
172+
attrs = append(attrs, slog.String("error", e.Err.Error()))
173+
}
174+
return slog.LevelError, msg, attrs
175+
176+
case LogUnexpectedResultID:
177+
msg := "tarantool: unexpected response request id (probably cancelled request)"
178+
attrs := []slog.Attr{
179+
slog.String("addr", addr),
180+
slog.Uint64("request_id", e.Header.RequestId),
112181
}
113-
case LogUnexpectedResultId:
114-
header := v[0].(Header)
115-
log.Printf("tarantool: connection %s got unexpected request ID (%d) in response "+
116-
"(probably cancelled request)",
117-
conn.Addr(), header.RequestId)
182+
return slog.LevelWarn, msg, attrs
183+
118184
case LogWatchEventReadFailed:
119-
err := v[0].(error)
120-
log.Printf("tarantool: unable to parse watch event: %s", err)
185+
msg := "tarantool: unable to parse watch event"
186+
attrs := []slog.Attr{}
187+
if e.Err != nil {
188+
attrs = append(attrs, slog.String("error", e.Err.Error()))
189+
}
190+
return slog.LevelWarn, msg, attrs
191+
121192
case LogBoxSessionPushUnsupported:
122-
header := v[0].(Header)
123-
log.Printf("tarantool: unsupported box.session.push() for request %d", header.RequestId)
193+
msg := "tarantool: unsupported box.session.push()"
194+
attrs := []slog.Attr{slog.Uint64("request_id", e.Header.RequestId), slog.String("addr", addr)}
195+
return slog.LevelInfo, msg, attrs
196+
124197
default:
125-
args := append([]interface{}{"tarantool: unexpected event ", event, conn}, v...)
126-
log.Print(args...)
198+
// unknown event: log type and value
199+
return slog.LevelInfo, "tarantool: unexpected event", []slog.Attr{slog.Any("event", event), slog.String("addr", addr)}
127200
}
128201
}
129202

0 commit comments

Comments
 (0)