The slog API doesn't allow wrapping handlers this way. slog.Handlers hold
state for the current open group.
You'll need a full implementation of a handler to do this.

(Technically it's possible if you record the group/attrs in With/WithGroup
yourself and only call the wrapped handler in Handle, but you lose any
preformatting performance benefits a handler may have implemented.)

- sean


On Mon, Oct 23, 2023 at 2:50 PM Johann Höchtl <johann.hoec...@gmail.com>
wrote:

> As I got zero reply to that answer, let me rephrase it probably more
> easily:
>
> I would like to use slog, no other logging library
> I would like to have an entry called eventID which is at the root level
> and has a unique uuid for every log entry
> I would like to use a group for every application specific key-val pairs
>
> What I trired so far:
>
>    - Use a Handler to add eventID : The key gets added to the logger yet
>    at the group nesting level, not at the root level
>    - Use a ReplaceAttr-Function: I add the key on initiailisation yet
>    leave the value unassigned to Replace it in the ReplaceAttr-Function: The
>    ReplaceAttr-Function sees the  root level addded eventID only untill a call
>    to .WithGroup, then the  eventId is no longer available within the
>    ReplaceAttr-Function
>
>
> Any help?
> Johann Höchtl schrieb am Donnerstag, 5. Oktober 2023 um 17:59:30 UTC+2:
>
>> I created a custom slog Handler which adds an attribute eventId which is
>> a GUID
>>
>> A NewLogger function returns that logger wrapped into a group: Every
>> further key-value provided should go into that group.
>>
>> Unfortunately the eventId also gets promoted into that group although it
>> should be at the parent level.
>>
>> See here the Goplay https://play.golang.com/p/a10f09jMwL3
>>
>> Thank you for your help, Johann
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/16dc9f51-6c8c-4fd9-9942-5e365399e26dn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/16dc9f51-6c8c-4fd9-9942-5e365399e26dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPokE8SKBtqKiUNs9_aieX2mWdxKm4XrwOoYOoe6jV%3DMUA%40mail.gmail.com.

Reply via email to