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.
42 lines
971 B
42 lines
971 B
2 years ago
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"git.diulo.com/mogfee/protoc-gen-kit/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) {
|
||
|
fmt.Println("add_log")
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|