https://github.com/ttys3/slogx
Simple slog wrapper for easy life, with opentelemetry tracing support
https://github.com/ttys3/slogx
console golang opentelemetry slog structured-logging trace-id tracing
Last synced: 4 months ago
JSON representation
Simple slog wrapper for easy life, with opentelemetry tracing support
- Host: GitHub
- URL: https://github.com/ttys3/slogx
- Owner: ttys3
- Created: 2022-12-28T02:28:43.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-10T22:01:54.000Z (4 months ago)
- Last Synced: 2025-03-13T09:54:16.258Z (4 months ago)
- Topics: console, golang, opentelemetry, slog, structured-logging, trace-id, tracing
- Language: Go
- Homepage:
- Size: 70.3 KB
- Stars: 29
- Watchers: 1
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# slogx
> `log/slog` stdlib requires go version >= 1.21.0
slog handler with opentelemetry tracing support and simple init helper method
trace_id in log

added Apex log cli handler like handler (impl ref https://github.com/apex/log/blob/master/handlers/cli/cli.go)

## usage
```go
package mainimport (
"context"
"github.com/ttys3/slogx"
"github.com/ttys3/tracing-go"
"go.opentelemetry.io/otel"
"log/slog"
"io"
)func main() {
// init a new slog json handler at info level with output to stderr
// and wrap it within a tracing handler
logger := slogx.New(slogx.WithTracing())
slog.SetDefault(logger)ctx := context.Background()
// set up a recording tracer, non-recording span will not get a trace_id
tpShutdown, err := tracing.InitProvider(ctx, tracing.WithStdoutTrace())
if err != nil {
panic(err)
}
defer tpShutdown(context.Background())ctx, newSpan := otel.Tracer("my-tracer-name").Start(ctx, "hello.Slog")
defer newSpan.End()
log := slog.With("user_id", 123456)
// no trace_id in log
log.Info("hello world")// has trace_id in log
log.With("foo", "bar").ErrorContext(ctx, "have a nice day", "err", io.ErrClosedPipe)
log.ErrorContext(ctx, "example error", "err", io.ErrClosedPipe)
}
```## related projects
slog.Handler that writes tinted logs
https://github.com/lmittmann/tint/Example ConsoleHandler for slog Logger
https://gist.github.com/wijayaerick/de3de10c47a79d5310968ba5ff101a19