diff --git a/connection.go b/connection.go index 7c7b3b2c..b6519134 100644 --- a/connection.go +++ b/connection.go @@ -102,6 +102,10 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) { protoErrorChan: make(chan error, 10), handshakeFinishedChan: make(chan interface{}), doneChan: make(chan interface{}), + // Create a discard logger to throw away logs. We do this so + // we don't have to add guards around every log operation if + // a logger is not configured by the user. + logger: slog.New(slog.NewJSONHandler(io.Discard, nil)), } // Apply provided options functions for _, option := range options { @@ -287,6 +291,7 @@ func (c *Connection) setupConnection() error { protoOptions := protocol.ProtocolOptions{ ConnectionId: c.id, Muxer: c.muxer, + Logger: c.logger, ErrorChan: c.protoErrorChan, } if c.useNodeToNodeProto { diff --git a/protocol/protocol.go b/protocol/protocol.go index 9a9c8315..829443b6 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import ( "bytes" "fmt" "io" + "log/slog" "sync" "time" @@ -53,6 +54,7 @@ type ProtocolConfig struct { ProtocolId uint16 ErrorChan chan error Muxer *muxer.Muxer + Logger *slog.Logger Mode ProtocolMode Role ProtocolRole MessageHandlerFunc MessageHandlerFunc @@ -85,6 +87,7 @@ const ( type ProtocolOptions struct { ConnectionId connection.ConnectionId Muxer *muxer.Muxer + Logger *slog.Logger ErrorChan chan error Mode ProtocolMode // TODO: remove me @@ -118,6 +121,7 @@ func New(config ProtocolConfig) *Protocol { func (p *Protocol) Start() { p.onceStart.Do(func() { // Register protocol with muxer + p.Logger().Debug("registering protocol with muxer") muxerProtocolRole := muxer.ProtocolRoleInitiator if p.config.Role == ProtocolRoleServer { muxerProtocolRole = muxer.ProtocolRoleResponder @@ -156,6 +160,7 @@ func (p *Protocol) Start() { func (p *Protocol) Stop() { p.onceStop.Do(func() { // Unregister protocol from muxer + p.Logger().Debug("unregistering protocol with muxer") muxerProtocolRole := muxer.ProtocolRoleInitiator if p.config.Role == ProtocolRoleServer { muxerProtocolRole = muxer.ProtocolRoleResponder @@ -167,6 +172,14 @@ func (p *Protocol) Stop() { }) } +// Logger returns the protocol logger +func (p *Protocol) Logger() *slog.Logger { + if p.config.Logger == nil { + return slog.New(slog.NewJSONHandler(io.Discard, nil)) + } + return p.config.Logger +} + // Mode returns the protocol mode func (p *Protocol) Mode() ProtocolMode { return p.config.Mode