Skip to content

Commit

Permalink
all: tslog
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jun 27, 2024
1 parent 216df13 commit 9b36995
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
51 changes: 51 additions & 0 deletions internal/dnsproxytest/tslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dnsproxytest

import (
"context"
"log/slog"
"testing"
)

// TLogHandler is a [slog.Handler] that wraps [testing.TB] logging functions.
//
// TODO(a.garipov): Move to golibs.
type TLogHandler struct {
tb testing.TB
}

// NewTLogHandler creates a [*TLogHandler].
func NewTLogHandler(tb testing.TB) (h *TLogHandler) {
return &TLogHandler{
tb: tb,
}
}

// type check
var _ slog.Handler = (*TLogHandler)(nil)

// Enabled implements the [slog.Handler] interface for *TLogHandler. It
// always returns true.
func (h *TLogHandler) Enabled(_ context.Context, _ slog.Level) (ok bool) { return true }

// Handle implements the [slog.Handler] interface for *TLogHandler.
func (h *TLogHandler) Handle(_ context.Context, r slog.Record) (err error) {
if r.Level >= slog.LevelError {
h.tb.Error(r.Message)
} else {
h.tb.Log(r.Message)
}

return nil
}

// WithAttrs implements the [slog.Handler] interface for *TLogHandler. It
// always returns h.
func (h *TLogHandler) WithAttrs(_ []slog.Attr) (res slog.Handler) {
return h
}

// WithGroup implements the [slog.Handler] interface for *TLogHandler. It
// always returns h.
func (h *TLogHandler) WithGroup(_ string) (res slog.Handler) {
return h
}
4 changes: 2 additions & 2 deletions upstream/doq_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
"github.com/AdguardTeam/dnsproxy/proxyutil"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/logutil/slogutil"
Expand Down Expand Up @@ -380,7 +381,6 @@ func (s *testDoQServer) closeConns() (err error) {
return errors.Join(errs...)
}

// startDoQServer starts a test DoQ server.
// startDoQServer starts a test DoQ server. Note that it adds its own shutdown
// to cleanup of t.
func startDoQServer(t *testing.T, tlsConf *tls.Config, port int) (s *testDoQServer) {
Expand Down Expand Up @@ -413,7 +413,7 @@ func startDoQServer(t *testing.T, tlsConf *tls.Config, port int) (s *testDoQServ
s = &testDoQServer{
addr: listen.Addr().String(),
listener: listen,
logger: slogutil.NewDiscardLogger(),
logger: slog.New(dnsproxytest.NewTLogHandler(t)),
conns: map[quic.EarlyConnection]struct{}{},
connsMu: &sync.Mutex{},
}
Expand Down

0 comments on commit 9b36995

Please sign in to comment.