MCPcopy
hub / github.com/redpanda-data/console / TestWithFormat

Function TestWithFormat

backend/pkg/logger/logger_test.go:240–286  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

238}
239
240func TestWithFormat(t *testing.T) {
241 tests := []struct {
242 name string
243 format Format
244 message string
245 checkJSON bool
246 }{
247 {
248 name: "explicit json format",
249 format: FormatJSON,
250 message: "test message",
251 checkJSON: true,
252 },
253 {
254 name: "explicit text format",
255 format: FormatText,
256 message: "test message",
257 checkJSON: false,
258 },
259 }
260
261 for _, tt := range tests {
262 t.Run(tt.name, func(t *testing.T) {
263 var buf bytes.Buffer
264 logger := NewSlogLogger(
265 WithOutput(&buf),
266 WithFormat(tt.format),
267 )
268
269 logger.Info(tt.message, slog.String("key", "value"))
270
271 output := buf.String()
272 require.NotEmpty(t, output)
273 assert.Contains(t, output, tt.message)
274
275 if tt.checkJSON {
276 // JSON format should contain quotes around keys and values
277 assert.Contains(t, output, `"msg":"test message"`)
278 assert.Contains(t, output, `"key":"value"`)
279 } else {
280 // Text format should be more human-readable
281 assert.Contains(t, output, "test message")
282 assert.Contains(t, output, "key=value")
283 }
284 })
285 }
286}
287
288func TestDefaultFormat(t *testing.T) {
289 // Test that the default logger uses JSON format

Callers

nothing calls this directly

Calls 4

NewSlogLoggerFunction · 0.85
WithOutputFunction · 0.85
WithFormatFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…