-
Notifications
You must be signed in to change notification settings - Fork 0
/
logaro_test.go
46 lines (39 loc) · 1.03 KB
/
logaro_test.go
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
package logaro
import (
"bytes"
"encoding/json"
"testing"
"time"
)
func TestGenerateLogger(t *testing.T) {
// Create a buffer to capture the log output
buf := new(bytes.Buffer)
// Create a logger with the buffer as the writer
logger := GenerateLogger()
logger.Writer = json.NewEncoder(buf)
// Log an entry
logger.Log("info", "Test log message", map[string]interface{}{
"key1": "value1",
"key2": 42,
})
// Expected log entry
expectedEntry := LogEntry{
Timestamp: time.Now().Format(time.RFC3339),
Message: "Test log message",
Level: "info",
Fields: map[string]interface{}{
"key1": "value1",
"key2": 42,
},
}
// Unmarshal the captured log output
var capturedEntry LogEntry
err := json.Unmarshal(buf.Bytes(), &capturedEntry)
if err != nil {
t.Fatalf("Error unmarshaling log entry: %s", err)
}
// Compare the captured log entry with the expected entry
if !compareLogEntries(capturedEntry, expectedEntry) {
t.Errorf("Log entry mismatch.\nExpected: %+v\nCaptured: %+v", expectedEntry, capturedEntry)
}
}