-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrus.go
53 lines (45 loc) · 1.33 KB
/
logrus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
pb "github.com/morix1500/sample-go-grpc-middleware/proto"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"net"
"os"
"time"
)
type HelloService struct{}
func (h HelloService) Hello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
return &pb.HelloResponse{
Message: "Hello, " + in.Name,
}, nil
}
func main() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(os.Stdout)
logrus.SetFormatter(&logrus.JSONFormatter{})
logger := logrus.WithFields(logrus.Fields{})
opts := []grpc_logrus.Option{
grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
return "grpc.time_ns", duration.Nanoseconds()
}),
}
grpc_logrus.ReplaceGrpcLogger(logger)
s := grpc.NewServer(
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logger, opts...),
),
)
pb.RegisterHelloServiceServer(s, HelloService{})
lis, err := net.Listen("tcp", ":5000")
if err != nil {
panic(err)
}
if err := s.Serve(lis); err != nil {
panic(err)
}
}