Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/PumpkinSeed/slog-context
slog: Context handler
https://github.com/PumpkinSeed/slog-context
context golang slog
Last synced: about 2 months ago
JSON representation
slog: Context handler
- Host: GitHub
- URL: https://github.com/PumpkinSeed/slog-context
- Owner: PumpkinSeed
- License: mit
- Created: 2023-08-15T10:23:54.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-28T11:07:38.000Z (8 months ago)
- Last Synced: 2024-11-13T11:43:23.735Z (2 months ago)
- Topics: context, golang, slog
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 23
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-slog - slog-context - value pairs to log records through context. (Enrichment)
README
# slog: Context handler
![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c)
A Context Handler for [slog](https://pkg.go.dev/log/slog) Go library.
## Install
```sh
go get github.com/PumpkinSeed/slog-context
```## Usage
GoDoc: [https://pkg.go.dev/github.com/PumpkinSeed/slog-context](https://pkg.go.dev/github.com/PumpkinSeed/slog-context)
### Example
```go
package mainimport (
"context"
"log/slog"
"os""github.com/PumpkinSeed/slog-context"
)type Struct struct {
Number int64
String string
}func main() {
// Add slogcontext.Handler to slog
slog.SetDefault(slog.New(slogcontext.NewHandler(slog.NewJSONHandler(os.Stdout, nil))))// Add values to context
ctx := slogcontext.WithValue(context.Background(), "number", 12)
ctx = slogcontext.WithValue(ctx, "string", "data")
ctx = slogcontext.WithValue(ctx, "struct", Struct{
Number: 42,
String: "struct_data",
})// Perform error log
slog.ErrorContext(ctx, "this is an error")
}
```### Output
```json
{"time":"2023-08-15T13:36:50.207418292+02:00","level":"ERROR","msg":"this is an error","number":12,"string":"data","struct":{"Number":42,"String":"struct_data"}}
```