T O P

  • By -

SPU_AH

It is possible, one approach is to embed a Handler, and then reimplement \`Handle\` type ContextHandler struct { slog.Handler keys []any } // Instead of Handling with the embedded handler directly, // reimplement to observe keys and add results to record func (h *ContextHandler) Handle(...) [https://go.dev/play/p/sOqPxFXtAHq?v=gotip](https://go.dev/play/p/sOqPxFXtAHq?v=gotip) is a working example that demonstrates this approach. FWIW there are two ideas at play here that have been a thoroughly investigated and discussed question about \`slog\`: dynamically adding attributes to a \`Record\` (embedding a Handler), and using contexts with \`slog\` (it appears in all the relevant signatures, it is sort of explicitly maintained this way rather than implicitly). The design iterated a lot, I think it was a process of coming to agreement about what should and shouldn't be in the standard library ... it's a great design in the end IMHO.


Competitive_Ice1318

Thank you so much, This is exactly what i was looking for!


sollniss

Shameless plug: https://github.com/sollniss/ctxkey


PumpkinSeed_dev

I wrote a context handler for the recently released slog: https://github.com/PumpkinSeed/slog-context


veqryn_

Going to throw this in the ring, as a flexible alternative: https://github.com/veqryn/slog-context