hongbo-miao/hongbomiao.com

View on GitHub
api-go/internal/grpc_server/utils/intercept_logger.go

Summary

Maintainability
A
0 mins
Test Coverage
package utils

import (
    "context"
    "fmt"
    "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
    "github.com/rs/zerolog"
)

func InterceptLogger(l zerolog.Logger) logging.Logger {
    return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
        l := l.With().Fields(fields).Logger()

        switch lvl {
        case logging.LevelDebug:
            l.Debug().Msg(msg)
        case logging.LevelInfo:
            l.Info().Msg(msg)
        case logging.LevelWarn:
            l.Warn().Msg(msg)
        case logging.LevelError:
            l.Error().Msg(msg)
        default:
            panic(fmt.Sprintf("unknown level %v", lvl))
        }
    })
}