grpc-go
grpc-go copied to clipboard
xds: switch generic xds client to use slog instead of grpclog
Currently generic xDS and LRS client https://github.com/grpc/grpc-go/tree/master/xds/internal/clients are using grpclog which is not a long term solution as eventually the generic clients need to be moved to a separate repo.
This proposal recommends to use slog.Logger. The logger should be an optional attribute of the lrsclient.Config and xdsclient.Config. If user doesn't provide the logger, a default slog.Logger should be assigned to it
-
Update
xdsclient.Configandlrsclient.Configto add a new Logger field asslog.Loggertype -
Modify
xdsclient.XDSClientandlrsclient.LRSClientinternal components: to use the provided Logger implementation from their respective config structs
type XDSClient struct {
// ... other fields
logger slog.Logger
}
type LRSClient struct {
// ... other fields
logger slog.Logger
}
- In the init method of
xdsclient.goandlrsclient.go, set the default instance of theslog.Logger
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, slog.LevelError)))
- When the xDS and LRS clients are instantiated, they may or may not receive the logger through their
configstructs.
xdsclient.go
func newClient(config *Config, watchExpiryTimeout time.Duration, streamBackoff func(int) time.Duration, target string) (*XDSClient, error) {
.....
.....
if config.logger != nil {
c.logger = config.logger
} else {
c.logger = slog.Default()
}
}
lrsclient.go
func New(config Config) (*LRSClient, error) {
....
....
if config.logger != nil {
c.logger = config.logger
} else {
c.logger = slog.Default()
}
}
- Throughout the client's codebase, existing logging statements (which might currently use grpclog, fmt.Println, or another logging library) would be replaced with calls to the methods of the new logger instance.
if c.logger.Enabled(2) {
c.logger.Info(mgs, )
}
- Internal grpc xds client code should be able to provide an slog.Logger to
xdsclient.XDSClientthat usesgrpc.PrefixLoggerunderneath. This will require to implement a customslog.Handlerthat wraps thegrpc.PrefixLoggerand use that handler inslog.New()when creating the xdsclient instance.
I'm not sure about slog yet - it may be more complex than we need, and it might require that we change grpc over to that same interface, since the xdsclient is used by grpc. For now, maybe we should define an interface that is compatible with slog's logger instead (i.e. has Info/Warn/Error/Enabled methods)?
Modified the issue to just add the new interface that mirrors methods from grpclog.PrefixLogger and user must provide their own implementation of it through the configs.
Looks like we should be accepting a *slog.Logger and not the Handler and not our own interface.
Looks like we should be accepting a
*slog.Loggerand not theHandlerand not our own interface.
Edited