-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters