Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 3 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 (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-27T21:15:57.000Z (8 months ago)
- Last Synced: 2024-06-11T14:28:45.966Z (8 months ago)
- Topics: console, golang, opentelemetry, slog, structured-logging, trace-id, tracing
- Language: Go
- Homepage:
- Size: 72.3 KB
- Stars: 28
- Watchers: 3
- Forks: 2
- Open Issues: 5
-
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
![image](https://github.com/ttys3/slogx/assets/41882455/281b00e7-fd7e-4e4c-b3e7-ea3f7ab27066)
added Apex log cli handler like handler (impl ref https://github.com/apex/log/blob/master/handlers/cli/cli.go)
![apex cli log like handler](https://user-images.githubusercontent.com/41882455/259750348-3f9b85ff-1403-482a-acfe-a028c7551185.png)
## 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