mirror of
https://github.com/slackhq/nebula.git
synced 2026-05-15 20:37:36 +02:00
Switch to slog, remove logrus (#1672)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type ContextualError struct {
|
||||
@@ -28,12 +28,12 @@ func ContextualizeIfNeeded(msg string, err error) error {
|
||||
}
|
||||
|
||||
// LogWithContextIfNeeded is a helper function to log an error line for an error or ContextualError
|
||||
func LogWithContextIfNeeded(msg string, err error, l *logrus.Logger) {
|
||||
func LogWithContextIfNeeded(msg string, err error, l *slog.Logger) {
|
||||
switch v := err.(type) {
|
||||
case *ContextualError:
|
||||
v.Log(l)
|
||||
default:
|
||||
l.WithError(err).Error(msg)
|
||||
l.Error(msg, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,10 +51,19 @@ func (ce *ContextualError) Unwrap() error {
|
||||
return ce.RealError
|
||||
}
|
||||
|
||||
func (ce *ContextualError) Log(lr *logrus.Logger) {
|
||||
if ce.RealError != nil {
|
||||
lr.WithFields(ce.Fields).WithError(ce.RealError).Error(ce.Context)
|
||||
} else {
|
||||
lr.WithFields(ce.Fields).Error(ce.Context)
|
||||
// Log emits ce as a single error-level log line with Fields and RealError
|
||||
// promoted to top-level attributes, producing a flat shape callers can grep
|
||||
// or parse without walking into a nested object.
|
||||
func (ce *ContextualError) Log(l *slog.Logger) {
|
||||
attrs := make([]slog.Attr, 0, len(ce.Fields)+1)
|
||||
for k, v := range ce.Fields {
|
||||
attrs = append(attrs, slog.Any(k, v))
|
||||
}
|
||||
if ce.RealError != nil {
|
||||
attrs = append(attrs, slog.Any("error", ce.RealError))
|
||||
}
|
||||
// LogAttrs is intentional: attrs is built from a map[string]any so it has
|
||||
// no pair-form equivalent.
|
||||
//nolint:sloglint
|
||||
l.LogAttrs(context.Background(), slog.LevelError, ce.Context, attrs...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user