You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

39 lines
942 B

package middleware
import (
"context"
"git.diulo.com/mogfee/protoc-gen-kit/pkg/xlog"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
"time"
)
func Logger(serverName string, log *logrus.Logger) Middleware {
baseEntry := log.WithFields(logrus.Fields{
"serverName": serverName,
})
return func(handler Handler) Handler {
return func(ctx context.Context, a any) (any, error) {
st := time.Now()
entry := baseEntry.WithFields(logrus.Fields{
"used_time": time.Since(st),
"param": a,
})
if md, ok := metadata.FromIncomingContext(ctx); ok {
entry = entry.WithFields(logrus.Fields{
"ip": md.Get("remote_ip")[0],
"full_path": md.Get("full_path")[0],
})
}
ctx = xlog.WithContext(ctx, entry)
resp, err := handler(ctx, a)
if err != nil {
entry.WithContext(ctx).Errorf("%+v", err)
} else {
entry.WithContext(ctx).Info("")
}
return resp, err
}
}
}