@@ -10,6 +10,7 @@ import (
1010 "context"
1111 "encoding/json"
1212 "log/slog"
13+ "slices"
1314 "sync"
1415 "time"
1516)
@@ -165,8 +166,7 @@ func (h *LoggingHandler) Handle(ctx context.Context, r slog.Record) error {
165166
166167func (h * LoggingHandler ) handle (ctx context.Context , r slog.Record ) error {
167168 // Observe the rate limit.
168- // TODO(jba): use golang.org/x/time/rate. (We can't here because it would require adding
169- // golang.org/x/time to the go.mod file.)
169+ // TODO(jba): use golang.org/x/time/rate.
170170 h .mu .Lock ()
171171 skip := time .Since (h .lastMessageSent ) < h .opts .MinInterval
172172 h .mu .Unlock ()
@@ -175,6 +175,7 @@ func (h *LoggingHandler) handle(ctx context.Context, r slog.Record) error {
175175 }
176176
177177 var err error
178+ var data json.RawMessage
178179 // Make the buffer reset atomic with the record write.
179180 // We are careful here in the unlikely event that the handler panics.
180181 // We don't want to hold the lock for the entire function, because Notify is
@@ -185,6 +186,8 @@ func (h *LoggingHandler) handle(ctx context.Context, r slog.Record) error {
185186 defer h .mu .Unlock ()
186187 h .buf .Reset ()
187188 err = h .handler .Handle (ctx , r )
189+ // Clone the buffer as Bytes() references the internal buffer.
190+ data = json .RawMessage (slices .Clone (h .buf .Bytes ()))
188191 }()
189192 if err != nil {
190193 return err
@@ -197,7 +200,7 @@ func (h *LoggingHandler) handle(ctx context.Context, r slog.Record) error {
197200 params := & LoggingMessageParams {
198201 Logger : h .opts .LoggerName ,
199202 Level : slogLevelToMCP (r .Level ),
200- Data : json . RawMessage ( h . buf . Bytes ()) ,
203+ Data : data ,
201204 }
202205 // We pass the argument context to Notify, even though slog.Handler.Handle's
203206 // documentation says not to.
0 commit comments