|
| 1 | +package util_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/pitabwire/util" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCustomHandler(t *testing.T) { |
| 14 | + // Create a custom handler creator that uses JSON format instead of the default tint handler |
| 15 | + jsonHandlerCreator := func(writer io.Writer, opts *util.LogOptions) slog.Handler { |
| 16 | + return slog.NewJSONHandler(writer, &slog.HandlerOptions{ |
| 17 | + Level: opts.Level, |
| 18 | + AddSource: opts.AddSource, |
| 19 | + }) |
| 20 | + } |
| 21 | + |
| 22 | + // Create options with the custom handler creator |
| 23 | + options := util.DefaultLogOptions(). |
| 24 | + WithHandlerCreator(jsonHandlerCreator). |
| 25 | + WithLevel(slog.LevelDebug) |
| 26 | + |
| 27 | + // Create a new logger with the custom handler |
| 28 | + logger := util.NewLogger(context.Background(), options) |
| 29 | + defer logger.Release() // Return to pool when done |
| 30 | + |
| 31 | + // Log some messages |
| 32 | + logger.Info("This will be logged in JSON format") |
| 33 | + logger.Debug("Debug message in JSON format", "key", "value") |
| 34 | + |
| 35 | + // Output: |
| 36 | + // (JSON-formatted log output) |
| 37 | +} |
| 38 | + |
| 39 | +func TestDirectHandlerUsage(t *testing.T) { |
| 40 | + // Create a text handler |
| 41 | + textHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
| 42 | + Level: slog.LevelInfo, |
| 43 | + }) |
| 44 | + |
| 45 | + // Create options with the handler directly set |
| 46 | + options := util.DefaultLogOptions(). |
| 47 | + WithHandler(textHandler). |
| 48 | + WithStackTrace(true) |
| 49 | + |
| 50 | + // Create a new logger with the direct handler |
| 51 | + logger := util.NewLogger(context.Background(), options) |
| 52 | + defer logger.Release() // Return to pool when done |
| 53 | + |
| 54 | + // Log some messages |
| 55 | + logger.Info("This will be logged in text format") |
| 56 | + logger.Error("This will include a stack trace") |
| 57 | + |
| 58 | + // Output: |
| 59 | + // (Text-formatted log output) |
| 60 | +} |
| 61 | + |
| 62 | +// TestTelemetryHandler tests using the OpenTelemetry handler |
| 63 | +func TestTelemetryHandler(t *testing.T) { |
| 64 | + // Create options with telemetry enabled |
| 65 | + options := util.DefaultLogOptions(). |
| 66 | + WithTracing(true) |
| 67 | + |
| 68 | + // Create a new logger with telemetry |
| 69 | + logger := util.NewLogger(t.Context(), options) |
| 70 | + defer logger.Release() // Return to pool when done |
| 71 | + |
| 72 | + // Log some messages with trace context |
| 73 | + logger.Info("This message will include OpenTelemetry trace context") |
| 74 | +} |
| 75 | + |
| 76 | +// TestCustomOutputWriter tests using a custom output writer |
| 77 | +func TestCustomOutputWriter(t *testing.T) { |
| 78 | + // Create a buffer to capture logs |
| 79 | + var buf io.Writer = os.Stderr |
| 80 | + |
| 81 | + // Create options with custom output |
| 82 | + options := util.DefaultLogOptions(). |
| 83 | + WithOutput(buf) |
| 84 | + |
| 85 | + // Create a new logger with custom output |
| 86 | + logger := util.NewLogger(t.Context(), options) |
| 87 | + defer logger.Release() // Return to pool when done |
| 88 | + |
| 89 | + // Log some messages |
| 90 | + logger.Info("This message will be written to the custom writer") |
| 91 | +} |
0 commit comments