Skip to content

Commit

Permalink
listener.go/dialer.go: Don't log error messages by default.
Browse files Browse the repository at this point in the history
This is more in line with standard practices. In production, these messages would probably not be desirable.
  • Loading branch information
Sandertv committed May 10, 2024
1 parent 6945551 commit 9a29b7c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
15 changes: 15 additions & 0 deletions internal/log.go
Original file line number Diff line number Diff line change
@@ -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 }
9 changes: 5 additions & 4 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package raknet

import (
"fmt"
"github.com/sandertv/go-raknet/internal"
"log/slog"
"maps"
"math"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a29b7c

Please sign in to comment.