memos/server/route/api/v2/logger_interceptor.go

49 lines
1.3 KiB
Go
Raw Normal View History

2024-02-29 23:54:43 +08:00
package v2
import (
"context"
"log/slog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type LoggerInterceptor struct {
}
func NewLoggerInterceptor() *LoggerInterceptor {
return &LoggerInterceptor{}
}
func (in *LoggerInterceptor) LoggerInterceptor(ctx context.Context, request any, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
resp, err := handler(ctx, request)
in.loggerInterceptorDo(ctx, serverInfo.FullMethod, err)
return resp, err
}
func (*LoggerInterceptor) loggerInterceptorDo(ctx context.Context, fullMethod string, err error) {
st := status.Convert(err)
var logLevel slog.Level
var logMsg string
switch st.Code() {
case codes.OK:
logLevel = slog.LevelInfo
logMsg = "OK"
case codes.Unauthenticated, codes.OutOfRange, codes.PermissionDenied, codes.NotFound:
logLevel = slog.LevelInfo
logMsg = "client error"
case codes.Internal, codes.Unknown, codes.DataLoss, codes.Unavailable, codes.DeadlineExceeded:
logLevel = slog.LevelError
logMsg = "server error"
default:
logLevel = slog.LevelError
logMsg = "unknown error"
}
2024-03-03 23:45:32 +08:00
logAttrs := []slog.Attr{slog.String("method", fullMethod)}
2024-03-03 14:20:59 +08:00
if err != nil {
2024-03-03 23:45:32 +08:00
logAttrs = append(logAttrs, slog.String("error", err.Error()))
2024-03-03 14:20:59 +08:00
}
2024-03-03 23:45:32 +08:00
slog.LogAttrs(ctx, logLevel, logMsg, logAttrs...)
2024-02-29 23:54:43 +08:00
}