Skip to content

Commit b0d3bbe

Browse files
committed
Fix alloc and add bench results in readme
1 parent 6a8ddfd commit b0d3bbe

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# console-slog
22

3-
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/phsym/console-slog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/phsym/console-slog/master/LICENSE) [![Build](https://github.com/phsym/console-slog/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/phsym/zeroslog/actions/workflows/go.yml)
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/phsym/console-slog.svg)](https://pkg.go.dev/github.com/phsym/console-slog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/phsym/console-slog/master/LICENSE) [![Build](https://github.com/phsym/console-slog/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/phsym/zeroslog/actions/workflows/go.yml)
44

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.
66

77
## Example
88
```go
@@ -40,4 +40,31 @@ When setting `console.HandlerOptions.AddSource` to `true`:
4040
```go
4141
console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug, AddSource: true})
4242
```
43-
![output-with-source](./doc/img/output-with-source.png)
43+
![output-with-source](./doc/img/output-with-source.png)
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+
```

handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var bufferPool = &sync.Pool{
12-
New: func() any { return buffer{} },
12+
New: func() any { return new(buffer) },
1313
}
1414

1515
var cwd, _ = os.Getwd()
@@ -60,7 +60,7 @@ func (h *ConsoleHandler) Enabled(_ context.Context, l slog.Level) bool {
6060

6161
// Handle implements slog.Handler.
6262
func (h *ConsoleHandler) Handle(_ context.Context, rec slog.Record) error {
63-
buf := bufferPool.Get().(buffer)
63+
buf := bufferPool.Get().(*buffer)
6464

6565
buf.writeTimestamp(rec.Time)
6666
buf.writeLevel(rec.Level)

0 commit comments

Comments
 (0)