-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathlogger_test.go
More file actions
67 lines (57 loc) · 2.22 KB
/
logger_test.go
File metadata and controls
67 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//go:build go1.21
package slog
import (
"bytes"
"context"
"log/slog"
"testing"
)
func TestLogger_Log_ConsoleOutput(t *testing.T) {
// Capture the console output
var buf bytes.Buffer
// Create a new JSON handler
handler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug, // Set the log level to Debug to capture all log levels
})
// Create a new logger instance with the handler
loggerInstance := New(handler)
// Log messages
loggerInstance.Log(context.Background(), slog.LevelInfo, "This is an info message via slog.", slog.Any("username", "john_doe"), slog.Int("age", 30))
loggerInstance.Log(context.Background(), slog.LevelError, "This is an error message via slog.", slog.String("module", "user-service"), slog.Int("retry", 3))
loggerInstance.Log(context.Background(), slog.LevelWarn, "This is a warn message via slog.", slog.Int("free_space_mb", 100))
loggerInstance.Log(context.Background(), slog.LevelDebug, "This is a debug message via slog.", slog.String("module", "main"))
// Check the output
output := buf.String()
expectedMessages := []struct {
msg string
contains []string
}{
{"This is an info message via slog.", []string{`"username":"john_doe"`, `"age":30}`}},
{"This is an error message via slog.", []string{`"module":"user-service"`, `"retry":3}`}},
{"This is a warn message via slog.", []string{`"free_space_mb":100}`}},
{"This is a debug message via slog.", []string{`"module":"main"`}},
}
for _, expected := range expectedMessages {
if !bytes.Contains([]byte(output), []byte(expected.msg)) {
t.Errorf("expected log message %q not found in output", expected.msg)
}
for _, attr := range expected.contains {
if !bytes.Contains([]byte(output), []byte(attr)) {
t.Errorf("expected attribute %q not found in output for message %q", attr, expected.msg)
}
}
}
}
func TestNewLogger_ValidSlogHandler(t *testing.T) {
// Test case where handler is a valid slog.Handler
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
logInstance := New(handler)
if logInstance == nil {
t.Fatalf("expected non-nil logInstance, got nil")
}
}