https://github.com/romantomjak/devslog
log/slog handler that formats records where message is followed by each of it's attributes on seperate lines
https://github.com/romantomjak/devslog
go slog slog-handler
Last synced: about 1 month ago
JSON representation
log/slog handler that formats records where message is followed by each of it's attributes on seperate lines
- Host: GitHub
- URL: https://github.com/romantomjak/devslog
- Owner: romantomjak
- License: mit
- Created: 2025-03-12T20:20:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-12T23:43:02.000Z (over 1 year ago)
- Last Synced: 2025-08-19T09:58:19.195Z (10 months ago)
- Topics: go, slog, slog-handler
- Language: Go
- Homepage: https://pkg.go.dev/github.com/romantomjak/devslog
- Size: 206 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# devslog

Package devslog provides a [log/slog](https://pkg.go.dev/log/slog) handler
that formats records where message is followed by each of it's attributes
on seperate lines.
It implements the `slog.Handler` interface to format the record's message.
The default `slog.TextHandler` output adds year/month/date which I rarely find
useful when working on code locally. This is what `slog.TextHandler` default
output looks like:
```sh
2022/11/08 15:28:26 INFO hello count=3 method=GET request=http://example.com
```
This package turns the above message into this:
```sh
15:28:26 INFO hello
↳ count: 3
↳ method: GET
↳ request: http://example.com
```
The devslog handler can be set as the default logger with:
```go
devslog.SetDefault(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
```
The top-level slog functions `slog.Info`, `slog.Debug`, etc will all use this
handler to format the records. `SetDefault` also updates the default logger
used by the [log](https://pkg.go.dev/log) package, so that existing applications
that use `log.Printf` and related functions will send log records to the logger's
handler without needing to be rewritten.