mirror of
https://github.com/slackhq/nebula.git
synced 2026-05-16 04:47:38 +02:00
Switch to slog, remove logrus (#1672)
This commit is contained in:
@@ -1,29 +1,73 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/slackhq/nebula/logging"
|
||||
)
|
||||
|
||||
func NewLogger() *logrus.Logger {
|
||||
l := logrus.New()
|
||||
|
||||
// NewLogger returns a *slog.Logger suitable for use in tests. Output goes to
|
||||
// io.Discard by default; set TEST_LOGS=1 (info), 2 (debug), or 3 (trace) to
|
||||
// stream output to stderr for local debugging.
|
||||
func NewLogger() *slog.Logger {
|
||||
v := os.Getenv("TEST_LOGS")
|
||||
if v == "" {
|
||||
l.SetOutput(io.Discard)
|
||||
return l
|
||||
return slog.New(slog.DiscardHandler)
|
||||
}
|
||||
|
||||
level := slog.LevelInfo
|
||||
switch v {
|
||||
case "2":
|
||||
l.SetLevel(logrus.DebugLevel)
|
||||
level = slog.LevelDebug
|
||||
case "3":
|
||||
l.SetLevel(logrus.TraceLevel)
|
||||
default:
|
||||
l.SetLevel(logrus.InfoLevel)
|
||||
level = logging.LevelTrace
|
||||
}
|
||||
|
||||
return l
|
||||
return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level}))
|
||||
}
|
||||
|
||||
// NewLoggerWithOutput returns a *slog.Logger whose text output is captured by
|
||||
// w. Timestamps are suppressed so tests can assert on exact output without
|
||||
// baking the current time into expected strings.
|
||||
func NewLoggerWithOutput(w io.Writer) *slog.Logger {
|
||||
return slog.New(&stripTimeHandler{inner: slog.NewTextHandler(w, nil)})
|
||||
}
|
||||
|
||||
// NewLoggerWithOutputAndLevel is NewLoggerWithOutput with an explicit level
|
||||
// so tests can exercise Enabled-gated paths.
|
||||
func NewLoggerWithOutputAndLevel(w io.Writer, level slog.Level) *slog.Logger {
|
||||
return slog.New(&stripTimeHandler{inner: slog.NewTextHandler(w, &slog.HandlerOptions{Level: level})})
|
||||
}
|
||||
|
||||
// NewJSONLoggerWithOutput returns a *slog.Logger emitting JSON to w with
|
||||
// timestamps suppressed, for tests that pin the JSON shape.
|
||||
func NewJSONLoggerWithOutput(w io.Writer, level slog.Level) *slog.Logger {
|
||||
return slog.New(&stripTimeHandler{inner: slog.NewJSONHandler(w, &slog.HandlerOptions{Level: level})})
|
||||
}
|
||||
|
||||
// stripTimeHandler zeros each record's time before delegating so slog's
|
||||
// built-in handlers skip emitting the time attribute. Used to avoid
|
||||
// timestamp-dependent assertions in tests without resorting to ReplaceAttr.
|
||||
type stripTimeHandler struct {
|
||||
inner slog.Handler
|
||||
}
|
||||
|
||||
func (h *stripTimeHandler) Enabled(ctx context.Context, l slog.Level) bool {
|
||||
return h.inner.Enabled(ctx, l)
|
||||
}
|
||||
|
||||
func (h *stripTimeHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
r.Time = time.Time{}
|
||||
return h.inner.Handle(ctx, r)
|
||||
}
|
||||
|
||||
func (h *stripTimeHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return &stripTimeHandler{inner: h.inner.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h *stripTimeHandler) WithGroup(name string) slog.Handler {
|
||||
return &stripTimeHandler{inner: h.inner.WithGroup(name)}
|
||||
}
|
||||
|
||||
47
test/tun.go
47
test/tun.go
@@ -1,47 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/netip"
|
||||
|
||||
"github.com/slackhq/nebula/routing"
|
||||
)
|
||||
|
||||
type NoopTun struct{}
|
||||
|
||||
func (NoopTun) RoutesFor(addr netip.Addr) routing.Gateways {
|
||||
return routing.Gateways{}
|
||||
}
|
||||
|
||||
func (NoopTun) Activate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (NoopTun) Networks() []netip.Prefix {
|
||||
return []netip.Prefix{}
|
||||
}
|
||||
|
||||
func (NoopTun) Name() string {
|
||||
return "noop"
|
||||
}
|
||||
|
||||
func (NoopTun) Read([]byte) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (NoopTun) Write([]byte) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (NoopTun) SupportsMultiqueue() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (NoopTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
||||
return nil, errors.New("unsupported")
|
||||
}
|
||||
|
||||
func (NoopTun) Close() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user