Skip to content

Context.Stack() not working #129

Description

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.

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