Skip to content

Hooks and sub-loggers can have strange side effects #564

@finkandreas

Description

@finkandreas

This line is relevant:

zerolog/log.go

Line 312 in 9070d49

l.hooks = append(l.hooks, h)

The code seems correct, but append can behave strangely, when it does not have to grow in size, i.e. the returned pointer is the same as the first argument.

Code on playground demonstrating the problem: https://play.golang.com/p/ALsrKvYmt3s
In the first part of the code I show the general problem, with a plain int array, where it is unexpected that b and c both contain a 4, instead of b with a 3.
And the same applies to zerolog hooks. Once they are at a threshold where the capacity of the hooks array is large enough, I can create a sub-logger called logger1 with an additional hook, and it works fine.
Then I create another sub-logger called logger2, with a different hook, and logging still looks fine.
Using now logger1, prints the hook of logger2, which is not the desired behaviour.

I fear that a full deep copy must be done to avoid this kind of behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions