| 8 | ) |
| 9 | |
| 10 | func TestNewWritesStructuredLogsToFile(t *testing.T) { |
| 11 | logFile := filepath.Join(t.TempDir(), "logs", "agh.log") |
| 12 | |
| 13 | log, closeFn, err := New(WithLevel("debug"), WithFile(logFile), WithMirrorToStderr(false)) |
| 14 | if err != nil { |
| 15 | t.Fatalf("New() error = %v", err) |
| 16 | } |
| 17 | |
| 18 | log.Info("hello", "component", "test") |
| 19 | |
| 20 | if err := closeFn(); err != nil { |
| 21 | t.Fatalf("closeFn() error = %v", err) |
| 22 | } |
| 23 | |
| 24 | data, err := os.ReadFile(logFile) |
| 25 | if err != nil { |
| 26 | t.Fatalf("ReadFile() error = %v", err) |
| 27 | } |
| 28 | if !strings.Contains(string(data), `"msg":"hello"`) { |
| 29 | t.Fatalf("log file = %q, want hello message", string(data)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestNewWithFileRotation(t *testing.T) { |
| 34 | t.Parallel() |