MCPcopy
hub / github.com/grpc/grpc-go / TestLoggerV2PrintFuncDiscardOnlyInfo

Function TestLoggerV2PrintFuncDiscardOnlyInfo

grpclog/internal/loggerv2_test.go:134–162  ·  view source on GitHub ↗

TestLoggerV2PrintFuncDiscardOnlyInfo ensures that logs at the INFO level are discarded when set to io.Discard, while logs at other levels (WARN, ERROR) are still printed. It does this by using a custom error function that raises an error if the logger attempts to print at the INFO level, ensuring ea

(t *testing.T)

Source from the content-addressed store, hash-verified

132// an error if the logger attempts to print at the INFO level, ensuring early
133// return when io.Discard is used.
134func TestLoggerV2PrintFuncDiscardOnlyInfo(t *testing.T) {
135 buffers := []*bytes.Buffer{nil, new(bytes.Buffer), new(bytes.Buffer)}
136 logger := NewLoggerV2(io.Discard, buffers[warningLog], buffers[errorLog], LoggerV2Config{})
137 loggerTp := logger.(*loggerT)
138
139 // test that output doesn't call expensive printf funcs on an io.Discard logger
140 sprintf = makeSprintfErr(t)
141 sprint = makeSprintErr(t)
142 sprintln = makeSprintErr(t)
143
144 loggerTp.output(infoLog, "something")
145
146 sprintf = fmt.Sprintf
147 sprint = fmt.Sprint
148 sprintln = fmt.Sprintln
149
150 loggerTp.output(errorLog, logFuncStr)
151 warnB, err := buffers[warningLog].ReadBytes('\n')
152 if err != nil {
153 t.Fatalf("level %v: %v", warningLog, err)
154 }
155 checkLogContainsFuncStr(t, warnB)
156
157 errB, err := buffers[errorLog].ReadBytes('\n')
158 if err != nil {
159 t.Fatalf("level %v: %v", errorLog, err)
160 }
161 checkLogContainsFuncStr(t, errB)
162}
163
164func TestLoggerV2PrintFuncNoDiscard(t *testing.T) {
165 buffers := []*bytes.Buffer{new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer)}

Callers

nothing calls this directly

Calls 6

makeSprintfErrFunction · 0.85
makeSprintErrFunction · 0.85
checkLogContainsFuncStrFunction · 0.85
outputMethod · 0.80
NewLoggerV2Function · 0.70
FatalfMethod · 0.65

Tested by

no test coverage detected