Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshick committed Nov 26, 2023
1 parent 534c817 commit ef6f2ab
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,49 @@ by, for instance, 'PANIC' being emitted as 'ERROR+8'.

## Examples

Under construction
This section delves into practical use cases to aid the integration of the library into various applications.

### Tracing

Consider a scenario where we aim to append tracing information to every log record generated during a request to one of
our services. Assuming we have incorporated a middleware (either third-party or self-written) that appends a trace-id
and span-id to our context, the next step is to propagate this information to our logger.

With this library, there are essentially two recommended approaches to achieve our goal.

#### The Middleware Approach

If we can register our middleware to execute after the one responsible for adding tracing information, and if this
information remains constant throughout the context's lifetime, we can directly include this data into a sub-logger
within our middleware and attach it to our context:

```
myLogger := logging.FromContext(ctx)
if myLogger != nil {
// if context has no logger attached, obtain logger (e.g. from slog.Default, logging instance or simply create a new one)
myHandler := slog.NewJSONHandler(os.Stdout, nil)
myLogger = slog.New(myHandler)
}
myLogger = myLogger.With("trace-id", ctx.Value("trace-id"), "span-id", ctx.Value("span-id"))
ctx = logging.ContextWithLogger(ctx, myLogger)
```

#### The Callback Handler Approach

If the information within the context might change during its lifetime, opting for the callback handler, despite being
slightly slower, offers a safer solution.

```
myHandler := slog.NewJSONHandler(os.Stdout, nil)
myCallbackHandler := callbackhandler.New(myHandler)
err := myCallbackHandler.RegisterContextCallback(func(ctx context.Context, record *slog.Record) error {
record.Add("span-id", ctx.Value("span-id"), "request-id", ctx.Value("request-id"))
return nil
}, "add-tracing-attributes")
if err != nil {
return err
}
myLogger := slog.New(myCallbackHandler)
// use logger as slog.Default, add it to our logging instance or attach it to a context
slog.SetDefault(myLogger)
```

0 comments on commit ef6f2ab

Please sign in to comment.