diff --git a/dial.go b/dial.go index 4597bf4..ecddb5e 100644 --- a/dial.go +++ b/dial.go @@ -88,9 +88,9 @@ func DialContext(ctx context.Context, address string) (*Conn, error) { // Dialer allows dialing a RakNet connection with specific configuration, such // as the protocol version of the connection and the logger used. type Dialer struct { - // ErrorLog is a logger that errors from packet decoding are logged to. It - // may be set to a logger that simply discards the messages. The default - // value is slog.Default(). + // ErrorLog is a logger that errors from packet decoding are logged to. By + // default, ErrorLog is set to a new slog.Logger with a slog.Handler that + // is always disabled. Error messages are thus not logged by default. ErrorLog *slog.Logger // UpstreamDialer is a dialer that will override the default dialer for @@ -211,7 +211,7 @@ func (dialer Dialer) DialTimeout(address string, timeout time.Duration) (*Conn, // context.Context is closed. func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, error) { if dialer.ErrorLog == nil { - dialer.ErrorLog = slog.Default() + dialer.ErrorLog = slog.New(internal.DiscardHandler{}) } conn, err := dialer.dial(ctx, address) diff --git a/handler.go b/handler.go index 4d8f28a..a55fe0d 100644 --- a/handler.go +++ b/handler.go @@ -129,7 +129,7 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest2(b []byte, addr n case <-h.l.closed: _ = conn.Close() case <-t.C: - // It took too long to complete this connection. We closed it and go + // It took too long to complete this connection. We close it and go // back to accepting. _ = conn.Close() } diff --git a/internal/log.go b/internal/log.go new file mode 100644 index 0000000..ce9b121 --- /dev/null +++ b/internal/log.go @@ -0,0 +1,15 @@ +package internal + +import ( + "context" + "log/slog" +) + +// DiscardHandler implements a slog.Handler that is always disabled. Each of its +// methods return immediately without any code running. +type DiscardHandler struct{} + +func (d DiscardHandler) Enabled(context.Context, slog.Level) bool { return false } +func (d DiscardHandler) Handle(context.Context, slog.Record) error { return nil } +func (d DiscardHandler) WithAttrs([]slog.Attr) slog.Handler { return d } +func (d DiscardHandler) WithGroup(string) slog.Handler { return d } diff --git a/listener.go b/listener.go index 9b0825d..a40a372 100644 --- a/listener.go +++ b/listener.go @@ -2,6 +2,7 @@ package raknet import ( "fmt" + "github.com/sandertv/go-raknet/internal" "log/slog" "maps" "math" @@ -19,9 +20,9 @@ type UpstreamPacketListener interface { // ListenConfig may be used to pass additional configuration to a Listener. type ListenConfig struct { - // ErrorLog is a logger that errors from packet decoding are logged to. It - // may be set to a logger that simply discards the messages. The default - // value is slog.Default(). + // ErrorLog is a logger that errors from packet decoding are logged to. By + // default, ErrorLog is set to a new slog.Logger with a slog.Handler that + // is always disabled. Error messages are thus not logged by default. ErrorLog *slog.Logger // UpstreamPacketListener adds an abstraction for net.ListenPacket. @@ -79,7 +80,7 @@ var listenerID = rand.Int64() // as the used log and/or the accepted protocol. func (conf ListenConfig) Listen(address string) (*Listener, error) { if conf.ErrorLog == nil { - conf.ErrorLog = slog.Default() + conf.ErrorLog = slog.New(internal.DiscardHandler{}) } if conf.BlockDuration == 0 { conf.BlockDuration = time.Second * 10