Repository: DiceDB/dice
File: internal/logger/zerolog.go (Go)

Code before:

// ZerologHandler is a custom handler that adapts slog to zerolog
type ZerologHandler struct {
	logger zerolog.Logger
}

// newZerologHandler creates a new ZerologHandler
func newZerologHandler(logger zerolog.Logger) *ZerologHandler {
	return &ZerologHandler{logger: logger}
}

// Handle implements the slog.Handler interface
func (h *ZerologHandler) Handle(ctx context.Context, record slog.Record) error {
	event := h.logger.WithLevel(mapLevel(record.Level))

	record.Attrs(func(attr slog.Attr) bool {
		switch attr.Value.Kind() {
		case slog.KindString:
			event = event.Str(attr.Key, attr.Value.String())
		case slog.KindInt64:
			event = event.Int64(attr.Key, attr.Value.Int64())
		case slog.KindFloat64:
			event = event.Float64(attr.Key, attr.Value.Float64())
		case slog.KindBool:
			event = event.Bool(attr.Key, attr.Value.Bool())
		case slog.KindAny:
			event = event.Interface(attr.Key, attr.Value.Any())
		default:
			// Log unknown types generically
			event = event.Interface(attr.Key, attr.Value.Any())
		}
		return true
	})

	event.Msg(record.Message)
	return nil
}

// mapLevel maps slog levels to zerolog levels
func mapLevel(level slog.Level) zerolog.Level {
	switch {
	case level == slog.LevelDebug:
		return zerolog.DebugLevel
	case level == slog.LevelInfo:
		return zerolog.InfoLevel
	case level == slog.LevelWarn:
		return zerolog.WarnLevel
	case level == slog.LevelError:
		return zerolog.ErrorLevel
	default:
		return zerolog.InfoLevel

Review comment (refactor):
`default` and `slog.KindAny` is doing exact same thing which results in redundant code.

we can simplify it by removing `slog.KindAny` case and rely on `default`.

Code after:

// ZerologHandler is a custom handler that adapts slog to zerolog
type ZerologHandler struct {
	logger *zerolog.Logger
}

// newZerologHandler creates a new ZerologHandler
func newZerologHandler(logger *zerolog.Logger) *ZerologHandler {
	return &ZerologHandler{
		logger: logger,
	}
}

// Handle implements the slog.Handler interface
//
//nolint:gocritic // The slog.Record struct triggers hugeParam but we don't control the interface (it's a standard library one)
func (h *ZerologHandler) Handle(ctx context.Context, record slog.Record) error {
	event := h.logger.WithLevel(mapLevel(record.Level))

	record.Attrs(func(attr slog.Attr) bool {
		switch attr.Value.Kind() {
		case slog.KindString:
			event = event.Str(attr.Key, attr.Value.String())
		case slog.KindInt64:
			event = event.Int64(attr.Key, attr.Value.Int64())
		case slog.KindFloat64:
			event = event.Float64(attr.Key, attr.Value.Float64())
		case slog.KindBool:
			event = event.Bool(attr.Key, attr.Value.Bool())
		default:
			// Log unknown types generically
			event = event.Interface(attr.Key, attr.Value.Any())
		}
		return true
	})

	event.Msg(record.Message)
	return nil
}

// mapLevel maps slog levels to zerolog levels
func mapLevel(level slog.Level) zerolog.Level {
	switch {
	case level == slog.LevelDebug:
		return zerolog.DebugLevel
	case level == slog.LevelWarn:
		return zerolog.WarnLevel
	case level == slog.LevelError:
		return zerolog.ErrorLevel
	default:
		return zerolog.InfoLevel