Skip to content

Instantly share code, notes, and snippets.

@isnikulin
Created March 24, 2025 03:21
Show Gist options
  • Save isnikulin/f6c14aec8b44b860d6405933086bbc2a to your computer and use it in GitHub Desktop.
Save isnikulin/f6c14aec8b44b860d6405933086bbc2a to your computer and use it in GitHub Desktop.
Sample for simple log tracing via zerolog
package main
import (
"context"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type traceContextKeyType int
const currentSpanKey traceContextKeyType = iota
type TracingHook struct{}
func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
ctx := e.GetCtx()
if traceId, ok := ctx.Value(currentSpanKey).(string); ok {
e.Str("trace-id", traceId)
}
}
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log := log.Hook(TracingHook{})
ctx := context.WithValue(context.Background(), currentSpanKey, "1234567890")
log.Info().Ctx(ctx).Msg("Hello, World!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment