Created
March 24, 2025 03:21
-
-
Save isnikulin/f6c14aec8b44b860d6405933086bbc2a to your computer and use it in GitHub Desktop.
Sample for simple log tracing via zerolog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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