Skip to content

Commit e65b6dd

Browse files
authored
feat: wire default discard connection logger to protocol configuration (#735)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent a5a62f9 commit e65b6dd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

connection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
102102
protoErrorChan: make(chan error, 10),
103103
handshakeFinishedChan: make(chan interface{}),
104104
doneChan: make(chan interface{}),
105+
// Create a discard logger to throw away logs. We do this so
106+
// we don't have to add guards around every log operation if
107+
// a logger is not configured by the user.
108+
logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
105109
}
106110
// Apply provided options functions
107111
for _, option := range options {
@@ -287,6 +291,7 @@ func (c *Connection) setupConnection() error {
287291
protoOptions := protocol.ProtocolOptions{
288292
ConnectionId: c.id,
289293
Muxer: c.muxer,
294+
Logger: c.logger,
290295
ErrorChan: c.protoErrorChan,
291296
}
292297
if c.useNodeToNodeProto {

protocol/protocol.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2024 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
1919
"bytes"
2020
"fmt"
2121
"io"
22+
"log/slog"
2223
"sync"
2324
"time"
2425

@@ -53,6 +54,7 @@ type ProtocolConfig struct {
5354
ProtocolId uint16
5455
ErrorChan chan error
5556
Muxer *muxer.Muxer
57+
Logger *slog.Logger
5658
Mode ProtocolMode
5759
Role ProtocolRole
5860
MessageHandlerFunc MessageHandlerFunc
@@ -85,6 +87,7 @@ const (
8587
type ProtocolOptions struct {
8688
ConnectionId connection.ConnectionId
8789
Muxer *muxer.Muxer
90+
Logger *slog.Logger
8891
ErrorChan chan error
8992
Mode ProtocolMode
9093
// TODO: remove me
@@ -118,6 +121,7 @@ func New(config ProtocolConfig) *Protocol {
118121
func (p *Protocol) Start() {
119122
p.onceStart.Do(func() {
120123
// Register protocol with muxer
124+
p.Logger().Debug("registering protocol with muxer")
121125
muxerProtocolRole := muxer.ProtocolRoleInitiator
122126
if p.config.Role == ProtocolRoleServer {
123127
muxerProtocolRole = muxer.ProtocolRoleResponder
@@ -156,6 +160,7 @@ func (p *Protocol) Start() {
156160
func (p *Protocol) Stop() {
157161
p.onceStop.Do(func() {
158162
// Unregister protocol from muxer
163+
p.Logger().Debug("unregistering protocol with muxer")
159164
muxerProtocolRole := muxer.ProtocolRoleInitiator
160165
if p.config.Role == ProtocolRoleServer {
161166
muxerProtocolRole = muxer.ProtocolRoleResponder
@@ -167,6 +172,14 @@ func (p *Protocol) Stop() {
167172
})
168173
}
169174

175+
// Logger returns the protocol logger
176+
func (p *Protocol) Logger() *slog.Logger {
177+
if p.config.Logger == nil {
178+
return slog.New(slog.NewJSONHandler(io.Discard, nil))
179+
}
180+
return p.config.Logger
181+
}
182+
170183
// Mode returns the protocol mode
171184
func (p *Protocol) Mode() ProtocolMode {
172185
return p.config.Mode

0 commit comments

Comments
 (0)