Skip to content

Commit 0ba8b19

Browse files
committed
1
1 parent 64458cf commit 0ba8b19

File tree

4 files changed

+24
-56
lines changed

4 files changed

+24
-56
lines changed

connection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,11 @@ func (conn *Connection) shutdown(forever bool) error {
15621562
conn.cond.Broadcast()
15631563
conn.notify(Shutdown)
15641564

1565+
// Logging the shutdown event
1566+
conn.logEvent(ShutdownEvent{
1567+
baseEvent: newBaseEvent(conn.addr),
1568+
})
1569+
15651570
c := conn.c
15661571
for {
15671572
if (atomic.LoadUint32(&conn.state) != connShutdown) || (c != conn.c) {

connection_events.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ type ShutdownEvent struct {
151151
}
152152

153153
func (e ShutdownEvent) EventName() string { return "shutdown" }
154+
func (e ShutdownEvent) Message() string { return "Server shutdown in progress" }
154155
func (e ShutdownEvent) LogLevel() slog.Level { return slog.LevelInfo }
155156
func (e ShutdownEvent) LogAttrs() []slog.Attr {
156157
attrs := e.baseAttrs()
@@ -171,38 +172,6 @@ func (e ClosedEvent) LogAttrs() []slog.Attr {
171172
return attrs
172173
}
173174

174-
type ConnectionPoolEvent struct {
175-
baseEvent
176-
PoolSize int
177-
ActiveConns int
178-
Event string
179-
}
180-
181-
func (e ConnectionPoolEvent) EventName() string { return "connection_pool_" + e.Event }
182-
func (e ConnectionPoolEvent) Message() string {
183-
switch e.Event {
184-
case "added":
185-
return "Connection added to pool"
186-
case "removed":
187-
return "Connection removed from pool"
188-
case "full":
189-
return "Connection pool is full"
190-
default:
191-
return "Connection pool event: " + e.Event
192-
}
193-
}
194-
func (e ConnectionPoolEvent) LogLevel() slog.Level { return slog.LevelInfo }
195-
func (e ConnectionPoolEvent) LogAttrs() []slog.Attr {
196-
attrs := e.baseAttrs()
197-
attrs = append(attrs,
198-
slog.String("event", e.EventName()),
199-
slog.Int("pool_size", e.PoolSize),
200-
slog.Int("active_connections", e.ActiveConns),
201-
slog.String("pool_event", e.Event),
202-
)
203-
return attrs
204-
}
205-
206175
type TimeoutEvent struct {
207176
baseEvent
208177
RequestId uint32

logger.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tarantool
22

33
import (
44
"context"
5-
"log"
65
"log/slog"
76
)
87

@@ -15,6 +14,11 @@ type SlogLogger struct {
1514
ctx context.Context
1615
}
1716

17+
// NewSlogLogger creates a new SlogLogger that uses the provided slog.Logger.
18+
// If logger is nil, it uses slog.Default().
19+
// Note: slog.Default() logs at Info level by default. To log events with lower levels
20+
// (e.g., Debug), either configure the default logger's level using slog.SetLogLoggerLevel
21+
// or provide a custom logger with the desired level.
1822
func NewSlogLogger(logger *slog.Logger) SlogLogger {
1923
if logger == nil {
2024
logger = slog.Default()
@@ -58,19 +62,3 @@ func (l SlogLogger) Report(event LogEvent, conn *Connection) {
5862

5963
l.logger.LogAttrs(l.ctx, event.LogLevel(), event.Message(), attrs...)
6064
}
61-
62-
type SimpleLogger struct{}
63-
64-
func (l SimpleLogger) Report(event LogEvent, conn *Connection) {
65-
attrs := event.LogAttrs()
66-
67-
log.Printf("[%s] %s [event=%s]", event.LogLevel(), event.Message(), event.EventName())
68-
69-
for _, attr := range attrs {
70-
if attr.Key == "error" {
71-
log.Printf(" Error: %v", attr.Value.Any())
72-
} else if attr.Key == "request_id" {
73-
log.Printf(" Request ID: %v", attr.Value.Any())
74-
}
75-
}
76-
}

tarantool_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"log"
11+
"log/slog"
1112
"math"
1213
"os"
1314
"os/exec"
@@ -2897,24 +2898,29 @@ func TestStream_DoWithClosedConn(t *testing.T) {
28972898
}
28982899

28992900
func TestConnectionBoxSessionPushUnsupported(t *testing.T) {
2900-
old := log.Writer()
2901-
defer log.SetOutput(old)
2902-
2901+
// Creating a buffer for intercepting logs
29032902
var buf bytes.Buffer
2904-
log.SetOutput(&buf)
2903+
// Setting up the slog handler, which writes to the buffer in text format
2904+
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
2905+
Level: slog.LevelWarn,
2906+
})
2907+
logger := slog.New(handler)
29052908

2909+
// Use a copy of the options and replace the logger
29062910
testOpts := opts
2907-
testOpts.Logger = SimpleLogger{}
2911+
testOpts.Logger = NewSlogLogger(logger)
29082912

29092913
conn := test_helpers.ConnectWithValidation(t, dialer, testOpts)
29102914
defer conn.Close()
29112915

2916+
// Calling the function that should generate a push event
29122917
_, err := conn.Do(NewCallRequest("push_func").Args([]interface{}{1})).Get()
29132918
require.NoError(t, err)
29142919

2920+
// Check that the expected message has appeared in the logs
29152921
actualLog := buf.String()
2916-
expectedLog := "box_session_push_unsupported"
2917-
require.Contains(t, actualLog, expectedLog)
2922+
expectedSubstr := "box_session_push_unsupported"
2923+
require.Contains(t, actualLog, expectedSubstr)
29182924
}
29192925

29202926
func TestConnectionProtocolInfoSupported(t *testing.T) {

0 commit comments

Comments
 (0)