gpt4 book ai didi

go - 将日志记录中心添加到 grpc

转载 作者:行者123 更新时间:2023-12-01 22:06:39 25 4
gpt4 key购买 nike

我正在尝试向 grpc 服务器添加一些日志记录中间件,我正在按照他们的 github 上的简单示例进行操作。

https://github.com/grpc-ecosystem/go-grpc-middleware/blob/master/logging/logrus/examples_test.go

我这样设置我的服务器选项:

var (
logrusLogger *logrus.Logger
customFunc grpc_logrus.CodeToLevel
)

func main() {

logrusEntry := logrus.NewEntry(logrusLogger)

lorgusOpts := []grpc_logrus.Option{
grpc_logrus.WithLevels(customFunc),
}

grpc_logrus.ReplaceGrpcLogger(logrusEntry)

opt := []grpc.ServerOption{
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc_middleware.WithUnaryServerChain(
grpc_auth.UnaryServerInterceptor(auther.Auth),
grpc_logrus.UnaryServerInterceptor(logrusEntry, lorgusOpts...),
),
}

s := grpc.NewServer(opt...)
if err := s.Serve(lis); err != nil {
log.Fatalf("err %+v", err)
}
}

但是,每当我查看日志时,我都会得到一个 nil 指针。

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x89edde]

goroutine 23 [running]:
github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus.UnaryServerInterceptor.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cac0, 0xb2e640, 0xc00026c390, 0x0, 0x0)
/go/pkg/mod/github.com/grpc-ecosystem/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a5adefa5b0b2a1efafaba6a6aea7b5a3b0a782b4f3ecf0ecf2" rel="noreferrer noopener nofollow">[email protected]</a>/logging/logrus/server_interceptors.go:37 +0x1be
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0x0, 0x0, 0x0, 0xc0000adfc0)
/go/pkg/mod/github.com/grpc-ecosystem/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1b13511b0e0c1f511115181810190b1d0e193c0a4d524e524c" rel="noreferrer noopener nofollow">[email protected]</a>/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware/auth.UnaryServerInterceptor.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cae0, 0x89ceda, 0x9f24e0, 0xc00018cb00, 0xc00018caa0)
/go/pkg/mod/github.com/grpc-ecosystem/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dabdb5f7bda8aab9f7b7b3bebeb6bfadbba8bf9aacebf4e8f4ea" rel="noreferrer noopener nofollow">[email protected]</a>/auth/auth.go:47 +0x108
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc0001a8100, 0x0, 0xc0001c5ac0, 0x40c5b8)
/go/pkg/mod/github.com/grpc-ecosystem/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aea6e4aebbb9aae4a4a0adada5acbea8bbac89bff8e7fbe7f9" rel="noreferrer noopener nofollow">[email protected]</a>/chain.go:25 +0x63
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1(0xb2e640, 0xc00026c390, 0x9df7a0, 0xc0000adfc0, 0xc00018caa0, 0xc00018cac0, 0xc00008eb30, 0x56cd28, 0xa04380, 0xc00026c390)
/go/pkg/mod/github.com/grpc-ecosystem/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b7bffdb7a2a0b3fdbdb9b4b4bcb5a7b1a2b590a6e1fee2fee0" rel="noreferrer noopener nofollow">[email protected]</a>/chain.go:34 +0xd5
qcap/proto/company._CompanyService_SelectCompany_Handler(0x9c43e0, 0xc00000e068, 0xb2e640, 0xc00026c390, 0xc0000ab140, 0xc000228120, 0xb2e640, 0xc00026c390, 0xc0000a20f0, 0x26)
/go/src/qcap/proto/company/service.pb.go:975 +0x14b
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100, 0xc000228570, 0xf082f8, 0x0, 0x0, 0x0)
/go/pkg/mod/google.golang.org/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482f3a382b083e79667a7f6679" rel="noreferrer noopener nofollow">[email protected]</a>/server.go:1024 +0x4f4
google.golang.org/grpc.(*Server).handleStream(0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100, 0x0)
/go/pkg/mod/google.golang.org/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12756062715264233c20253c23" rel="noreferrer noopener nofollow">[email protected]</a>/server.go:1313 +0xd97
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0001dce50, 0xc000001500, 0xb35300, 0xc0001da480, 0xc0001a8100)
/go/pkg/mod/google.golang.org/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a7b2b0a380b6f1eef2f7eef1" rel="noreferrer noopener nofollow">[email protected]</a>/server.go:722 +0xbb
created by google.golang.org/grpc.(*Server).serveStreams.func1
/go/pkg/mod/google.golang.org/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60071210032016514e52574e51" rel="noreferrer noopener nofollow">[email protected]</a>/server.go:720 +0xa1

有人遇到过这种情况吗?

最佳答案

这是 panic ,因为这些变量为零

var (
logrusLogger *logrus.Logger
customFunc grpc_logrus.CodeToLevel
)

改为使用

var (
logrusLogger = logrus.New()
customFunc = func(code codes.Code) logrus.Level {
if code == codes.OK {
return logrus.InfoLevel
}
return logrus.ErrorLevel
}
)

注意:grpc_logrus.CodeToLevel 的底层类型为 func(codecodes.Code) logrus.Level

关于go - 将日志记录中心添加到 grpc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61367409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com