|
1 |
| -## hypera.dev/lib/slog/pretty |
| 1 | +# hypera.dev/lib/slog/pretty |
2 | 2 |
|
3 |
| -A human-readable and optionally coloured `slog` handler. |
| 3 | +[](https://pkg.go.dev/hypera.dev/lib/slog/pretty#section-documentation) |
| 4 | + |
| 5 | +A human-readable and optionally coloured [**slog.Handler**](https://pkg.go.dev/log/slog#Handler). |
| 6 | + |
| 7 | +The output format is designed to be quick and easy to read, primarily for use in development environments. |
| 8 | +This format is not necessarily recommended for production environments, especially where logs need to be parsed to |
| 9 | +extract data. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +```shell |
| 14 | +go get -u hypera.dev/lib/slog/pretty |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```go |
| 20 | +// Create a new logger |
| 21 | +log := slog.New(pretty.NewHandler(os.Stdout, nil)) |
| 22 | + |
| 23 | +// Set global logger and use custom options |
| 24 | +slog.SetDefault(slog.New( |
| 25 | + pretty.NewHandler(os.Stdout, &pretty.Options{ |
| 26 | + Level: slog.LevelDebug, |
| 27 | + AddSource: true, |
| 28 | + }), |
| 29 | +)) |
| 30 | +``` |
| 31 | + |
| 32 | +## Customisable |
| 33 | + |
| 34 | +It is possible to customise how certain parts of the output are formatted by passing different formatters in |
| 35 | +`pretty.Options`. It is not currently possible to reorder parts of the output, however this feature may be added in |
| 36 | +the future. |
| 37 | + |
| 38 | +### Time formatter |
| 39 | + |
| 40 | +```go |
| 41 | +// Change the time format using the default formatter |
| 42 | +pretty.NewHandler(w, &pretty.Options{ |
| 43 | + TimeFormatter: pretty.DefaultTimeFormatter(time.DateTime), |
| 44 | +}), |
| 45 | + |
| 46 | +// Use a custom time formatter |
| 47 | +pretty.NewHandler(w, &pretty.Options{ |
| 48 | + TimeFormatter: func(buf *pretty.Buffer, t time.Time) { |
| 49 | + buf.AppendTimeFormat(t, time.DateTime) |
| 50 | + }, |
| 51 | +}), |
| 52 | +``` |
| 53 | + |
| 54 | +### Level formatter |
| 55 | + |
| 56 | +```go |
| 57 | +// Use the default level formatter |
| 58 | +pretty.NewHandler(w, &pretty.Options{ |
| 59 | + LevelFormatter: pretty.DefaultLevelFormatter(color), |
| 60 | +}), |
| 61 | + |
| 62 | +// Use a custom level formatter |
| 63 | +pretty.NewHandler(w, &pretty.Options{ |
| 64 | + LevelFormatter: func(buf *pretty.Buffer, l slog.Level) { |
| 65 | + if l == LevelTrace { |
| 66 | + buf.AppendString("TRACE") |
| 67 | + return |
| 68 | + } |
| 69 | + pretty.DefaultLevelFormatter(true), |
| 70 | + }, |
| 71 | +}), |
| 72 | +``` |
| 73 | + |
| 74 | +### Source formatter |
| 75 | + |
| 76 | +```go |
| 77 | +// Use the default source formatter |
| 78 | +pretty.NewHandler(w, &pretty.Options{ |
| 79 | + LevelFormatter: pretty.DefaultSourceFormatter(color), |
| 80 | +}), |
| 81 | + |
| 82 | +// Use a custom source formatter |
| 83 | +pretty.NewHandler(w, &pretty.Options{ |
| 84 | + SourceFormatter: func(buf *pretty.Buffer, src *slog.Source) { |
| 85 | + dir, file := filepath.Split(src.File) |
| 86 | + |
| 87 | + buf.AppendByte('<') |
| 88 | + buf.AppendString(filepath.Join(filepath.Base(dir), file)) |
| 89 | + buf.AppendByte(':') |
| 90 | + buf.AppendInt(int64(src.Line)) |
| 91 | + buf.AppendByte('>') |
| 92 | + }, |
| 93 | +}), |
| 94 | +``` |
| 95 | + |
| 96 | +## Extras |
| 97 | + |
| 98 | +### Automatic color toggle |
| 99 | + |
| 100 | +Colored output is enabled by default and can be disabled by setting `DisableColor: false` in the handler options. |
| 101 | +You can have colors automatically enabled depending on the terminal capabilities with the use of a package |
| 102 | +like [`github.com/mattn/go-isatty`](https://github.com/mattn/go-isatty). |
| 103 | + |
| 104 | +```go |
| 105 | +w := os.Stdout |
| 106 | +pretty.NewHandler(w, &pretty.Options{ |
| 107 | + DisableColor: !isatty.IsTerminal(w.Fd()), |
| 108 | +}), |
| 109 | +``` |
| 110 | + |
| 111 | +### Windows support |
| 112 | + |
| 113 | +Colors may display weirdly on Windows, and can be corrected with use of |
| 114 | +the [`github.com/mattn/go-colorable`](https://github.com/mattn/go-colorable) package. |
| 115 | + |
| 116 | +```go |
| 117 | +w := os.Stdout |
| 118 | +pretty.NewHandler(colorable.NewColorable(w), nil) |
| 119 | +``` |
| 120 | + |
| 121 | +## Acknowledgements |
| 122 | + |
| 123 | +The output format is heavily inspired by [`github.com/charmbracelet/log`](https://github.com/charmbracelet/log) |
| 124 | +and [`github.com/lmittmann/tint`](https://github.com/lmittmann/tint). |
| 125 | + |
| 126 | +`github.com/lmittmann/tint` is a great alternative to this library, however it does not currently support custom time, |
| 127 | +level or source formatters. |
0 commit comments