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

append stack multiline with zerolog.NewConsoleWriter() #157

Open
dryaf opened this issue Jun 3, 2019 · 3 comments
Open

append stack multiline with zerolog.NewConsoleWriter() #157

dryaf opened this issue Jun 3, 2019 · 3 comments

Comments

@dryaf
Copy link

dryaf commented Jun 3, 2019

while developing and using the zerolog.NewConsoleWriter(), i would like to append the stack in a new line (nicely formatted multiline string) instead of inline as an attribute
(maybe this way i can cmd+click it in intellij)

any hints are very welcome?

@inoam
Copy link

inoam commented Jan 22, 2020

+1
I was trying to hack this as well.
I would generally like the stack to look just like I called fmt.Sprintf("%+v", err), on error with stack trace created by errors package, as shown in this blogpost by: https://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package :

not enough arguments, expected at least 3, got 0
main.parseArgs
        /home/dfc/src/github.com/pkg/errors/_examples/wrap/main.go:12
main.main
        /home/dfc/src/github.com/pkg/errors/_examples/wrap/main.go:18
runtime.main
        /home/dfc/go/src/runtime/proc.go:183
runtime.goexit
        /home/dfc/go/src/runtime/asm_amd64.s:2059

Tried to achieve using custom function on :

  1. zerolog.ErrorStackMarshaler, but this would add a string to the buffer, and newlines will be formatted as "\n".
  2. zerologFormatErrFieldValue / FormatErrFieldName : this would not work as well, as I do not get the error object at this time, only the error message (FormatErrFieldValue) or "error" (FormatErrFieldName).

I would appreciate if there is another solution.

@Cyberhan123
Copy link

@inoam I use a hack method reslove it : )

func ESPackMarshalStack(err error) interface{} {
	type stackTracer interface {
		StackTrace() errors.StackTrace
	}
	e, ok := err.(stackTracer)
	if !ok {
		return nil
	}
	//It's mean when env=dev just print track
	if utils.Env.Dev() {
		for _, frame := range e.StackTrace() {
			fmt.Printf("%+s:%d\r\n", frame, frame)
		}
	} else {
		return pkgerrors.MarshalStack(err)
	}
	return nil
}

@mitar
Copy link
Contributor

mitar commented Feb 23, 2022

You might want #416.

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

4 participants