Skip to content

Commit 7a9f099

Browse files
committed
textlogger: write unstructured klog output directly
Textlogger can (but doesn't have to) be used to write klog's unstructured output from calls like Infof directly to the output stream. This has two advantages: - traditional output remains exactly the same as before, which is particularly important for multi-line messages because those get quoted otherwise - performance is better because re-encoding the output string is avoided
1 parent 81f97ff commit 7a9f099

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

test/output.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/go-logr/logr"
4040

4141
"k8s.io/klog/v2"
42+
"k8s.io/klog/v2/textlogger"
4243
)
4344

4445
// InitKlog must be called in a test to configure klog for testing.
@@ -577,7 +578,7 @@ func Output(t *testing.T, config OutputConfig) {
577578

578579
if config.AsBackend {
579580
testOutput(t, printWithKlogLine-1, func(buffer *bytes.Buffer) {
580-
klog.SetLogger(config.NewLogger(buffer, 10, test.vmodule))
581+
setLogger(config.NewLogger(buffer, 10, test.vmodule))
581582
printWithKlog(test)
582583
})
583584
return
@@ -765,10 +766,11 @@ func Output(t *testing.T, config OutputConfig) {
765766
for i, test := range tests {
766767
t.Run(test.name, func(t *testing.T) {
767768
var buffer bytes.Buffer
769+
haveWriteKlogBuffer := false
768770
if config.NewLogger == nil {
769771
klog.SetOutput(&buffer)
770772
} else {
771-
klog.SetLogger(config.NewLogger(&buffer, 10, ""))
773+
haveWriteKlogBuffer = setLogger(config.NewLogger(&buffer, 10, ""))
772774
defer klog.ClearLogger()
773775
}
774776
test.logFunc()
@@ -789,6 +791,7 @@ func Output(t *testing.T, config OutputConfig) {
789791
// result, including a trailing newline, to
790792
// Logger.Info.
791793
if config.NewLogger != nil &&
794+
!haveWriteKlogBuffer &&
792795
!strings.HasSuffix(test.name, "S") &&
793796
!strings.HasSuffix(test.name, "SDepth") {
794797
// klog: I output.go:<LINE>] hello world
@@ -851,7 +854,7 @@ func Benchmark(b *testing.B, config OutputConfig) {
851854
}
852855

853856
if config.AsBackend {
854-
klog.SetLogger(config.NewLogger(io.Discard, 10, ""))
857+
setLogger(config.NewLogger(io.Discard, 10, ""))
855858
for i := 0; i < b.N; i++ {
856859
printWithKlog(test)
857860
}
@@ -871,6 +874,17 @@ func Benchmark(b *testing.B, config OutputConfig) {
871874
}
872875
}
873876

877+
func setLogger(logger logr.Logger) bool {
878+
haveWriteKlogBuffer := false
879+
var opts []klog.LoggerOption
880+
if writer, ok := logger.GetSink().(textlogger.KlogBufferWriter); ok {
881+
opts = append(opts, klog.WriteKlogBuffer(writer.WriteKlogBuffer))
882+
haveWriteKlogBuffer = true
883+
}
884+
klog.SetLoggerWithOptions(logger, opts...)
885+
return haveWriteKlogBuffer
886+
}
887+
874888
func copySlice(in []interface{}) []interface{} {
875889
return append([]interface{}{}, in...)
876890
}

textlogger/textlogger.go

+14
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func (l *tlogger) print(err error, s severity.Severity, msg string, kvList []int
134134
l.config.co.output.Write(b.Bytes())
135135
}
136136

137+
func (l *tlogger) WriteKlogBuffer(data []byte) {
138+
l.config.co.output.Write(data)
139+
}
140+
137141
// WithName returns a new logr.Logger with the specified name appended. klogr
138142
// uses '/' characters to separate name elements. Callers should not pass '/'
139143
// in the provided name string, but this library does not actually enforce that.
@@ -152,5 +156,15 @@ func (l *tlogger) WithValues(kvList ...interface{}) logr.LogSink {
152156
return &new
153157
}
154158

159+
// KlogBufferWriter is implemented by the textlogger LogSink.
160+
type KlogBufferWriter interface {
161+
// WriteKlogBuffer takes a pre-formatted buffer prepared by klog and
162+
// writes it unchanged to the output stream. Can be used with
163+
// klog.WriteKlogBuffer when setting a logger through
164+
// klog.SetLoggerWithOptions.
165+
WriteKlogBuffer([]byte)
166+
}
167+
155168
var _ logr.LogSink = &tlogger{}
156169
var _ logr.CallDepthLogSink = &tlogger{}
170+
var _ KlogBufferWriter = &tlogger{}

0 commit comments

Comments
 (0)