Skip to content

Commit

Permalink
Update readme and benchmark codes
Browse files Browse the repository at this point in the history
  • Loading branch information
phsym committed Aug 16, 2023
1 parent 543bdd0 commit 6a8ddfd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,41 @@
[![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)

A handler for slog that prints colorized logs, similar to zerolog's console writer output

## Example
```go
package main

import (
"errors"
"log/slog"
"os"

"github.com/phsym/console-slog"
)

func main() {
logger := slog.New(
console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug}),
)
slog.SetDefault(logger)
slog.Info("Hello world!", "foo", "bar")
slog.Debug("Debug message")
slog.Warn("Warning message")
slog.Error("Error message", "err", errors.New("the error"))

logger = logger.With("foo", "bar").
WithGroup("the-group").
With("bar", "baz")

logger.Info("group info", "attr", "value")
}
```

![output](./doc/img/output.png)

When setting `console.HandlerOptions.AddSource` to `true`:
```go
console.NewHandler(os.Stderr, &console.HandlerOptions{Level: slog.LevelDebug, AddSource: true})
```
![output-with-source](./doc/img/output-with-source.png)
30 changes: 11 additions & 19 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ var handlers = []struct {
}

var attrs = []slog.Attr{
slog.String("titi", "toto"),
slog.String("tata", "tutu"),
slog.Int("foo", 12),
slog.String("foo", "bar"),
slog.Int("int", 12),
slog.Duration("dur", 3*time.Second),
slog.Bool("bool", true),
slog.Float64("float", 23.7),
Expand All @@ -39,27 +38,20 @@ var attrs = []slog.Attr{
slog.Group("group", slog.String("bar", "baz")),
}

var attrsAny = []any{
slog.String("titi", "toto"),
slog.String("tata", "tutu"),
slog.Int("foo", 12),
slog.Duration("dur", 3*time.Second),
slog.Bool("bool", true),
slog.Float64("float", 23.7),
slog.Time("thetime", time.Now()),
slog.Any("err", errors.New("yo")),
slog.Group("empty"),
slog.Group("group", slog.String("bar", "baz")),
}
var attrsAny = func() (a []any) {
for _, attr := range attrs {
a = append(a, attr)
}
return
}()

func BenchmarkHandlers(b *testing.B) {
ctx := context.Background()
rec := slog.NewRecord(time.Now(), slog.LevelInfo, "yo", 0)
rec := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", 0)
rec.AddAttrs(attrs...)

for _, tc := range handlers {
b.Run(tc.name, func(b *testing.B) {
// l := tc.hdl.WithAttrs([]slog.Attr{slog.String("toto", "tutu")}) //.WithGroup("test")
l := tc.hdl.WithAttrs(attrs).WithGroup("test").WithAttrs(attrs)
// Warm-up
l.Handle(ctx, rec)
Expand All @@ -77,10 +69,10 @@ func BenchmarkLoggers(b *testing.B) {
b.Run(tc.name, func(b *testing.B) {
l := slog.New(tc.hdl).With(attrsAny...).WithGroup("test").With(attrsAny...)
// Warm-up
l.LogAttrs(ctx, slog.LevelInfo, "yo", attrs...)
l.LogAttrs(ctx, slog.LevelInfo, "hello", attrs...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(ctx, slog.LevelInfo, "yo", attrs...)
l.LogAttrs(ctx, slog.LevelInfo, "hello", attrs...)
}
})
}
Expand Down
Binary file added doc/img/output-with-source.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/img/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6a8ddfd

Please sign in to comment.