|
1 | 1 | # console-slog |
2 | 2 |
|
3 | | -[](https://godoc.org/github.com/phsym/console-slog) [](https://raw.githubusercontent.com/phsym/console-slog/master/LICENSE) [](https://github.com/phsym/zeroslog/actions/workflows/go.yml) |
| 3 | +[](https://pkg.go.dev/github.com/phsym/console-slog) [](https://raw.githubusercontent.com/phsym/console-slog/master/LICENSE) [](https://github.com/phsym/zeroslog/actions/workflows/go.yml) |
4 | 4 |
|
5 | | -A handler for slog that prints colorized logs, similar to zerolog's console writer output |
| 5 | +A handler for slog that prints colorized logs, similar to zerolog's console writer output, but without sacrificing performances. |
6 | 6 |
|
7 | 7 | ## Example |
8 | 8 | ```go |
@@ -40,4 +40,31 @@ When setting `console.HandlerOptions.AddSource` to `true`: |
40 | 40 | ```go |
41 | 41 | console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug, AddSource: true}) |
42 | 42 | ``` |
43 | | - |
| 43 | + |
| 44 | + |
| 45 | +## Performances |
| 46 | +See [benchmark file](./bench_test.go) for details. |
| 47 | + |
| 48 | +The handler itself performs quite well compared to std-lib's handlers. It does no allocation: |
| 49 | +``` |
| 50 | +goos: linux |
| 51 | +goarch: amd64 |
| 52 | +pkg: github.com/phsym/console-slog |
| 53 | +cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz |
| 54 | +BenchmarkHandlers/dummy-4 128931026 8.732 ns/op 0 B/op 0 allocs/op |
| 55 | +BenchmarkHandlers/console-4 849837 1294 ns/op 0 B/op 0 allocs/op |
| 56 | +BenchmarkHandlers/std-text-4 542583 2097 ns/op 4 B/op 2 allocs/op |
| 57 | +BenchmarkHandlers/std-json-4 583784 1911 ns/op 120 B/op 3 allocs/op |
| 58 | +``` |
| 59 | + |
| 60 | +However, the go 1.21.0 `slog.Logger` adds some overhead: |
| 61 | +``` |
| 62 | +goos: linux |
| 63 | +goarch: amd64 |
| 64 | +pkg: github.com/phsym/console-slog |
| 65 | +cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz |
| 66 | +BenchmarkLoggers/dummy-4 1239873 893.2 ns/op 128 B/op 1 allocs/op |
| 67 | +BenchmarkLoggers/console-4 483354 2338 ns/op 128 B/op 1 allocs/op |
| 68 | +BenchmarkLoggers/std-text-4 368828 3141 ns/op 132 B/op 3 allocs/op |
| 69 | +BenchmarkLoggers/std-json-4 393322 2909 ns/op 248 B/op 4 allocs/op |
| 70 | +``` |
0 commit comments