mirror of
https://github.com/usememos/memos.git
synced 2024-11-16 19:56:11 +08:00
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
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"
|
|
}
|
|
slog.LogAttrs(ctx, logLevel, logMsg, slog.String("method", fullMethod))
|
|
if err != nil {
|
|
slog.LogAttrs(ctx, logLevel, "", slog.String("error", err.Error()))
|
|
}
|
|
}
|