-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogger_test.go
More file actions
61 lines (52 loc) · 1.57 KB
/
Copy pathlogger_test.go
File metadata and controls
61 lines (52 loc) · 1.57 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
package utils
import (
"bytes"
"encoding/json"
"github.com/stringintech/go-bitcoinkernel/kernel"
"log/slog"
"strings"
"testing"
)
func TestLogger(t *testing.T) {
// Enable LogKernel category to capture the block decode failure log
kernel.AddLogLevelCategory(kernel.LogKernel, kernel.LogLevelDebug)
kernel.EnableLogCategory(kernel.LogKernel)
defer kernel.DisableLogCategory(kernel.LogKernel)
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
options := kernel.LoggingOptions{
LogTimestamps: true,
LogTimeMicros: true,
LogThreadNames: true,
LogSourceLocations: true,
AlwaysPrintCategoryLevel: true,
}
viewer, err := NewLogger(handler, options)
if err != nil {
t.Fatalf("Failed to create log viewer: %v", err)
}
defer viewer.Destroy()
_, err = kernel.NewBlock([]byte{0xab})
if err == nil {
t.Error("Expected error for invalid block data")
}
// Parse the last JSON entry and assert message
logOutput := buf.String()
lines := strings.Split(strings.TrimSpace(logOutput), "\n")
if len(lines) == 0 {
t.Fatal("No log output captured")
}
lastLine := lines[len(lines)-1]
var logEntry map[string]interface{}
if err := json.Unmarshal([]byte(lastLine), &logEntry); err != nil {
t.Fatalf("Failed to parse last JSON log entry: %v", err)
}
expectedMessage := "Block decode failed."
if msg, ok := logEntry["msg"]; !ok {
t.Error("Log entry missing 'msg' field")
} else if msg != expectedMessage {
t.Errorf("Expected message '%s', got '%s'", expectedMessage, msg)
}
}