Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dpotapov/slogpfx
Prefix `slog` messages based on log attributes
https://github.com/dpotapov/slogpfx
golang logging slog
Last synced: 3 months ago
JSON representation
Prefix `slog` messages based on log attributes
- Host: GitHub
- URL: https://github.com/dpotapov/slogpfx
- Owner: dpotapov
- License: mit
- Created: 2023-09-17T05:29:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-17T06:33:53.000Z (about 1 year ago)
- Last Synced: 2024-05-30T06:59:50.655Z (5 months ago)
- Topics: golang, logging, slog
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-slog - slogpfx
README
## `slogpfx` - Prefix Logging for `slog`
Easily prefix your log messages with attributes from the log record using `slogpfx`.
![demo](demo.png)
### Features
- Customizable prefix formatter.
- Colored output by default.
- Works best with [lmittmann/tint](https://github.com/lmittmann/tint)
and [mattn/go-colorable](https://github.com/mattn/go-colorable) packages (some examples below).### Installation
```bash
go get -u github.com/dpotapov/slogpfx
```### Usage
Here's a quick example to get you started:
```go
h := slog.NewTextHandler(os.Stdout, nil)// Use the prefix based on the attribute "service"
prefixed := slogpfx.NewHandler(h, &slogpfx.HandlerOptions{
PrefixKeys: []string{"service"}
})logger := slog.New(prefixed)
logger.Info("Hello World!")
logger.Info("Hello World!", "service", "billing")
logger.With("service", "database").Error("Connection error")
```Using in conjunction with [lmittmann/tint](https://github.com/lmittmann/tint)
and [mattn/go-colorable](https://github.com/mattn/go-colorable) packages:```go
h := tint.NewHandler(colorable.NewColorable(os.Stdout), nil)// Use the prefix based on the attribute "service"
prefixed := slogpfx.NewHandler(h, &slogpfx.HandlerOptions{
PrefixKeys: []string{"service"},
})logger := slog.New(prefixed)
```### Customization
You can customize the way prefixes are displayed using the `PrefixFormatter` option.
---
Happy Logging! 📜🖋️