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 entries
How to reproduce it (as minimally and precisely as possible):
package main
import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
func myFunc() {
err := errors.New("my error")
err = errors.WithStack(errors.Wrap(err, "something failed"))
log.Error().Err(err).Msg("hello world")
}
func main() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.With().Str("my", "field").Stack().Logger()
myFunc()
}
Anything else we need to know?:
I think it comes from the fact that Context.Stack() setup an Hook which is called after the event is completely built, so Err never see that Stack() was enabled.
We can see it because the following example works as expected:
package main
import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
func myFunc() {
err := errors.New("my error")
err = errors.WithStack(errors.Wrap(err, "something failed"))
log.Error().Stack().Err(err).Msg("hello world")
}
func main() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.With().Str("my", "field").Logger()
myFunc()
}
I'm not sure how to fix this. One solution could be to add a stack field to Logger which would be transferred to the event in logger.newEvent.
What happened:
Using
Context.Stack().Logger()does not work, the stack is not added to each log entries.What you expected to happen:
The
stackfield 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 anHookwhich is called after the event is completely built, soErrnever 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
stackfield toLoggerwhich would be transferred to the event inlogger.newEvent.