Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
finkandreas opened this issue Jul 11, 2023 · 0 comments
Closed

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

finkandreas opened this issue Jul 11, 2023 · 0 comments

Comments

@finkandreas
Copy link
Contributor

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.

finkandreas added a commit to finkandreas/zerolog that referenced this issue Jul 11, 2023
This could be a potential fix for rs#564
@finkandreas finkandreas mentioned this issue Jul 11, 2023
@rs rs closed this as completed in 61485f3 Jul 12, 2023
madkins23 pushed a commit to madkins23/zerolog that referenced this issue Mar 2, 2024
This could be a potential fix for rs#564
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant