I have a logger that logs stack traces to stdout
. I want to get the stack trace that was logged into a string value so I can send it as a debug email.
Here is my current code:
func (l *Logger) withStack(writer io.Writer, err error) error {
err = errors.WithStack(err)
fmt.Fprintf(writer, "%+v
", err)
return err
}
func (l *Logger) Error(err error) error {
// Logs stack trace to `stdout`...
l.withStack(os.Stdout, err)
// Here I want to get the string value of what was logged to `stdout`.
}
But if the stdout
stack trace looks like this:
foo
infrastructure/logger.(*Logger).withStack
/Users/lansana/Projects/Go/src/app/src/infrastructure/logger/logger.go:40
infrastructure/logger.(*Logger).Error
/Users/lansana/Projects/Go/src/app/src/infrastructure/logger/logger.go:50
main.main
/Users/lansana/Projects/Go/src/app/src/microservices/api/main.go:44
runtime.main
/usr/local/Cellar/go/1.9/libexec/src/runtime/proc.go:185
runtime.goexit
/usr/local/Cellar/go/1.9/libexec/src/runtime/asm_amd64.s:2337
...then the result of err.Error()
in the email is just foo
.
I guess that makes sense, because the error itself only contains foo
and not the stack trace itself, but how can I get the exact stack trace as a string value?