grpc-go
grpc-go copied to clipboard
grpclog: change logger to avoid Printf when the logger is io.Discard
Changes the logger to not call Printf when the logger is io.Discard.
Also changes the logger constructor to use io.Discard
instead of io.MultiWriter(io.Discard, io.Discard)
, which is necessary to detect all levels being discarded.
This is a significant performance improvement for disabled logs, see https://github.com/grpc/grpc-go/issues/7463 for details.
Notes:
- This is similar to how the std does it, see log.go
- I was hoping to use the std
log.Printf
itself, but I don't think that's possible, because the JSON option needs to Printf and then escape the result after interpolating, which I don't think is possible with what's exposed by the std (possibly sans some overly complex io.Writer interceptors). - It could use
log.Printf
for the non-JSON, but there's not much point if we can't use it for the JSON, and it would incur an unnecessary atomic call. - The constructor
io.Discard
logic could be made faster (e.g. with more nested if's), and could do smarter things around the discard/multiwriter. I wrote it this way for readability, and because I don't think the performance is significant. But I'm happy to change it.
Fixes #7463
RELEASE NOTES:
- grpclog: Avoid expensive printf calls for io.Discard loggers.