Documentation
¶
Overview ¶
Package logrus provides a small adapter required to use logrus in logging gRPC middlewares. Please see examples for examples of use.
Example (InitializationWithCustomLevels) ¶
package main
import (
grpclogrus "github.com/grpc-ecosystem/go-grpc-middleware/providers/logrus/v2"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)
var customFunc logging.CodeToLevel
func main() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := logrus.New()
// Shared options for the logger, with a custom gRPC code to log level function.
opts := []logging.Option{
logging.WithLevels(customFunc),
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
)
}
Example (InitializationWithDurationFieldOverride) ¶
package main
import (
grpclogrus "github.com/grpc-ecosystem/go-grpc-middleware/providers/logrus/v2"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
)
var customDurationToFields logging.DurationToFields
func main() {
// Logger is used, allowing pre-definition of certain fields by the user.
logger := logrus.New()
// Shared options for the logger, with a custom duration to log field function.
opts := []logging.Option{
logging.WithDurationField(customDurationToFields),
}
// Create a server, make sure we put the tags context before everything else.
_ = grpc.NewServer(
middleware.WithUnaryServerChain(
logging.UnaryServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
middleware.WithStreamServerChain(
logging.StreamServerInterceptor(grpclogrus.InterceptorLogger(logger), opts...),
),
)
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
logrus.FieldLogger
}
Logger is a logrus logging adapter compatible with logging middlewares.
func InterceptorLogger ¶
func InterceptorLogger(logger logrus.FieldLogger) *Logger
InterceptorLogger converts logrus logger to Logger adapter.
Click to show internal directories.
Click to hide internal directories.