feat: build wahrwelt A/D toolkit
This commit is contained in:
1 parent
303438d206
commit
653924d8c6
564 files changed
+36310
-49998
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type LoggingServer struct {
|
||||
Logger *logrus.Entry
|
||||
}
|
||||
|
||||
func NewLoggingServer(serverName string) LoggingServer {
|
||||
return LoggingServer{Logger: logrus.WithField("server", serverName)}
|
||||
}
|
||||
|
||||
func (s *LoggingServer) WrapErrorf(ctx context.Context, code codes.Code, fmt string, values ...any) error {
|
||||
err := status.Errorf(code, fmt, values...)
|
||||
s.GetMethodLogger(ctx).Errorf("%v", err)
|
||||
return err // nolint
|
||||
}
|
||||
|
||||
func (s *LoggingServer) LogRequest(ctx context.Context, r any) {
|
||||
// Данные RPC могут содержать секреты или вывод недоверенной программы.
|
||||
// Записывать только тип запроса, но не его значения.
|
||||
s.GetMethodLogger(ctx).Infof("Request type: %T", r)
|
||||
}
|
||||
|
||||
func (s *LoggingServer) GetMethodLogger(ctx context.Context) *logrus.Entry {
|
||||
if method, ok := grpc.Method(ctx); ok {
|
||||
return s.Logger.WithField("method", method)
|
||||
}
|
||||
return s.Logger
|
||||
}
|
||||
Reference in new issue
Block a user