-
Notifications
You must be signed in to change notification settings - Fork 579
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
Context.Stack() not working #129
Comments
What would be a good use-case for putting a stack in a context? |
If, for performance reasons, we want to enable stack logging only in precise points of fhe application. |
I would imagine the use of Stack with error message, so only on events. |
+1, so maybe we could remove the |
I would favor that yes. As a non-user of this specific feature, I wanted to understand the use first. |
After refactoring, it seems that the good way to achieve what I tried to do (stack trace on events where level >= FatalLevel) |
Do you want to update your PR to remove it? |
Hi. Sorry my claims were false (because I use a fork of zerolog where it works). Adding a hook to add stack traces or not, according to the event's level does not work (for the reasons in the first comment). For instance package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"github.com/pkg/errors"
)
type stackHook struct{}
func (h stackHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
if level >= zerolog.FatalLevel {
e.Stack()
}
}
func main() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Hook(stackHook{})
err := errors.New("my error")
err = errors.WithStack(errors.Wrap(err, "something failed"))
log.Fatal().Err(err).Msg("unrecoverable")
} which seems a legitimate use case, does not work. I'm not sure if it's a feature or a bug. |
The problem is that |
I actually made a PR for this very issue, a while ago... #144 (I'm currently using my own fork of pkgerrors https://github.com/mec07/zerolog/tree/multi-stack/pkgerrors until the PR gets merged in) |
Hi, On my side I step down on this issue as I no longer use Go. Bets regards, |
So my expectation from reading the docs is that by creating a logger with context by calling With().Stack().Logger() I should get a stacktrace when I call log.Err(errors.New("testing 1 2 3")) but that does not happen because when the hooks from the With().Stack() context aren't processed until Msg() is called, which is after the Event.Err() function runs where it's looking for e.stack to be true. Since the logger hook from the context has not been called yet, it's always false. So the only way around it now is to call log.Error().Stack().Err(...) which is not what you necessarily want to do. For example, I only want to enable stacktrace output with errors when a user specifies a --debug flag, so I'd like to create a logger with Stack() context at runtime. I think if you can just update the logic of the Err() function in events.go, that should fix the issue. |
I also have this problem and created a PR a while back: #179 |
I was expecting Stack hook should automatically enable Stack priniting but looks like I have to still call |
Thanks for fixing this! Is this fix released? If so, what version? |
It was released in version v1.21.0: https://github.com/rs/zerolog/releases/tag/v1.21.0 However, there have been many versions released since then too. |
Trying on 1.26.1 (Golang 1.18) and this is still not working here :( Tested the example in readme: func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
err := outer()
log.Error().Stack().Err(err).Msg("")
}
func inner() error {
return errors.New("seems we have an error here")
}
func middle() error {
err := inner()
if err != nil {
return err
}
return nil
}
func outer() error {
err := middle()
if err != nil {
return err
}
return nil
} It logged
|
NVM, I was using Go's default error library instead of pkgErrors' |
It actually works for me on 1.26.1. Are you using golang standard errors
package? If so, you need to switch to pkg/errors for it to work.
…On Thu, May 26, 2022 at 4:00 AM Giulio Denardi ***@***.***> wrote:
Trying on 1.26.1 and this still is not working here :(
Tested the example in readme:
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
err := outer()
log.Error().Stack().Err(err).Msg("")
}
func inner() error {
return errors.New("seems we have an error here")
}
func middle() error {
err := inner()
if err != nil {
return err
}
return nil
}
func outer() error {
err := middle()
if err != nil {
return err
}
return nil
}
It logged
{"level":"error","error":"seems we have an error here","time":1653573551}
—
Reply to this email directly, view it on GitHub
<#129 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQ45M72GNNNCW2ARCD6TETVL57Y3ANCNFSM4GUT456Q>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
What happened:
Using
Context.Stack().Logger()
does not work, the stack is not added to each log entries.What you expected to happen:
The
stack
field should be added to each log entriesHow to reproduce it (as minimally and precisely as possible):
Anything else we need to know?:
I think it comes from the fact that
Context.Stack()
setup anHook
which is called after the event is completely built, soErr
never see thatStack()
was enabled.We can see it because the following example works as expected:
I'm not sure how to fix this. One solution could be to add a
stack
field toLogger
which would be transferred to the event inlogger.newEvent
.The text was updated successfully, but these errors were encountered: