-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
73 lines (57 loc) · 2.21 KB
/
struct_test.go
File metadata and controls
73 lines (57 loc) · 2.21 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
68
69
70
71
72
73
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package olog
import (
"testing"
"go.opentelemetry.io/otel/log/noop"
)
func TestLogger_StructBehavior(t *testing.T) {
logger := New(Options{Provider: noop.NewLoggerProvider(), Name: "test"})
ctx := t.Context()
// Test that the original logger is not modified when creating a new one with With()
originalLogger := logger
withLogger := logger.With("key1", "value1")
// Should be different instances
if originalLogger == withLogger {
t.Error("With() should return a new logger instance")
}
// Original logger should not have attributes
if len(logger.attrs) != 0 {
t.Errorf("Original logger should have no attrs, got %d", len(logger.attrs))
}
// With logger should have attributes
if len(withLogger.attrs) != 1 {
t.Errorf("With logger should have 1 KeyValue attr, got %d", len(withLogger.attrs))
}
// Test chaining With calls
chainedLogger := withLogger.With("key2", "value2")
if len(chainedLogger.attrs) != 2 {
t.Errorf("Chained logger should have 2 KeyValue attrs, got %d", len(chainedLogger.attrs))
}
// Test logging doesn't panic
logger.Info(ctx, "test.event")
withLogger.Info(ctx, "test.event", "with_attr", "value")
chainedLogger.Info(ctx, "test.chained")
}
func TestLogger_AttributeHandling(t *testing.T) {
logger := New(Options{Provider: noop.NewLoggerProvider(), Name: "test"})
// Test that attributes are properly stored
withLogger := logger.With("service", "api", "version", "1.0")
if len(withLogger.attrs) != 2 {
t.Errorf("Expected 2 KeyValue attrs, got %d", len(withLogger.attrs))
}
// Check the key-value pairs
expectedKeys := []string{"service", "version"}
expectedValues := []string{"api", "1.0"}
if len(withLogger.attrs) != len(expectedKeys) {
t.Fatalf("Attr length mismatch: expected %d, got %d", len(expectedKeys), len(withLogger.attrs))
}
for i, expectedKey := range expectedKeys {
if withLogger.attrs[i].Key != expectedKey {
t.Errorf("Attr[%d] key: expected %s, got %s", i, expectedKey, withLogger.attrs[i].Key)
}
if withLogger.attrs[i].Value.AsString() != expectedValues[i] {
t.Errorf("Attr[%d] value: expected %s, got %s", i, expectedValues[i], withLogger.attrs[i].Value.AsString())
}
}
}