https://github.com/adreasnow/otelzlog
https://github.com/adreasnow/otelzlog
golang otel zerolog
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/adreasnow/otelzlog
- Owner: adreasnow
- License: mit
- Created: 2025-03-21T09:10:51.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-21T09:36:35.000Z (2 months ago)
- Last Synced: 2025-03-21T10:41:41.821Z (2 months ago)
- Topics: golang, otel, zerolog
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# OTEL Wrapper for zerolog
[](https://godoc.org/github.com/adreasnow/otelzlog) [](https://raw.githubusercontent.com/adreasnow/otelzlog/main/LICENSE) [](https://github.com/adreasnow/otelzlog/actions/workflows/test-tag.yaml) [](https://raw.githack.com/wiki/adreasnow/otelzlog/coverage.html)
The otelzlog package provides a hook to use in order to send zerolog events to OTEL logs
## Usage
```go
// Initiliase your OTEL configuration and save your logger to the global otel config with "go.opentelemetry.io/otel/log/global"
...
global.SetLoggerProvider(provider)
...// Initialise the logger with as many writers as you'd like and save it to the conetxt
buf := new(bytes.Buffer)ctx = otelzlog.New(ctx,
zerolog.ConsoleWriter{Out: os.Stdout},
zerolog.ConsoleWriter{Out: buf}
zerolog.TestWriter{T: t},
)// Set up your spans and traces as needed
tracer := otel.Tracer("service-name")
ctx, span = tracer.Start(ctx, "test.segment")
time.sleep(time.Second) // do some work
defer span.End()// Call the logger using log.Ctx(ctx) an pass in the context to the event
log.Ctx(ctx).Info().Ctx(ctx).Str("attribute.1", "attr").Msg("Hello World")// Spans will be extracted from the context and logs will be sent to your otel collector
// Attributes will be added from the logger, but not from the span
```The syntax can be cumbersome, so wrapper functions are a good idea, e.g.:
```go
package logimport (
"context""github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)func Debug(ctx context.Context) *zerolog.Event {
return log.Ctx(ctx).Debug().Ctx(ctx)
}func Warn(ctx context.Context) *zerolog.Event {
return log.Ctx(ctx).Warn().Ctx(ctx)
}...
```