|
| 1 | +package dbos |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log/slog" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLogger(t *testing.T) { |
| 11 | + |
| 12 | + t.Run("Default logger", func(t *testing.T) { |
| 13 | + err := Launch() // Launch with default logger |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("Failed to launch with default logger: %v", err) |
| 16 | + } |
| 17 | + t.Cleanup(func() { |
| 18 | + Shutdown() |
| 19 | + }) |
| 20 | + |
| 21 | + if logger == nil { |
| 22 | + t.Fatal("Logger is nil") |
| 23 | + } |
| 24 | + |
| 25 | + // Test logger access |
| 26 | + logger.Info("Test message from default logger") |
| 27 | + |
| 28 | + }) |
| 29 | + t.Run("Custom logger", func(t *testing.T) { |
| 30 | + |
| 31 | + // Test with custom slog logger |
| 32 | + var buf bytes.Buffer |
| 33 | + slogLogger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{ |
| 34 | + Level: slog.LevelDebug, |
| 35 | + })) |
| 36 | + |
| 37 | + // Add some context to the slog logger |
| 38 | + slogLogger = slogLogger.With("service", "dbos-test", "environment", "test") |
| 39 | + |
| 40 | + err := Launch(WithLogger(slogLogger)) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Failed to launch with custom logger: %v", err) |
| 43 | + } |
| 44 | + t.Cleanup(func() { |
| 45 | + Shutdown() |
| 46 | + }) |
| 47 | + |
| 48 | + if logger == nil { |
| 49 | + t.Fatal("Logger is nil") |
| 50 | + } |
| 51 | + |
| 52 | + // Test that we can use the logger and it maintains context |
| 53 | + logger.Info("Test message from custom logger", "test_key", "test_value") |
| 54 | + |
| 55 | + // Check that our custom logger was used and captured the output |
| 56 | + logOutput := buf.String() |
| 57 | + if !strings.Contains(logOutput, "service=dbos-test") { |
| 58 | + t.Errorf("Expected log output to contain service=dbos-test, got: %s", logOutput) |
| 59 | + } |
| 60 | + if !strings.Contains(logOutput, "environment=test") { |
| 61 | + t.Errorf("Expected log output to contain environment=test, got: %s", logOutput) |
| 62 | + } |
| 63 | + if !strings.Contains(logOutput, "test_key=test_value") { |
| 64 | + t.Errorf("Expected log output to contain test_key=test_value, got: %s", logOutput) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
0 commit comments