logxi
logxi copied to clipboard
How to disable stack trace in wrn and err level ?
Hello, i need to disable stack trace in WRN and ERROR level, how to do this ?
If you generally want to supress stack traces on specific logger calls you can always pass the error message instead of the entire error:
log.Error("Oh noe", "error", err) // will generate a stacktrace
log.Error("Oh noe", "error", err.Error()) // will not generate a stacktrace
unfortunately log.Error always generate stack trace :(, your method worked only with warning.
Please consider adding an option to disable automatic stacktraces with the HappyDevFormatter. The traces generated by debug.Stack are quite useless.
Consider this program:
package main
import (
"errors"
"github.com/mgutz/logxi/v1"
)
func main() {
err := f()
log.Info("fatal", "err", err)
}
func f() error {
return g()
}
func g() error {
return errors.New("fail")
}
The automatic trace is something like
$ go run main.go
21:17:02.595604 ERR ~ fatal err: fail
goroutine 1 [running]:
runtime/debug.Stack(0x10, 0xc420045a68, 0x4bf5c0)
/usr/local/go/src/runtime/debug/stack.go:24 +0x79
github.com/mgutz/logxi/v1.(*HappyDevFormatter).getLevelContext(0xc42000e900, 0x3, 0xc420014c90, 0x0, 0x0, 0x0, 0x0, 0xc42000ac58, 0x5)
/tmp/tmp.j9F8IpiHwM/main.go:21 +0x137
This mentions neither f, nor g. There are packages that provide much more relevant traces, such as github.com/pkg/errors. When I log these traces explicitly the automatic traces are confusing.
$ go run main.go
21:16:31.119270 ERR ~ fatal err: fail
trace: main.g
/tmp/tmp.j9F8IpiHwM/main.go:26
main.f
/tmp/tmp.j9F8IpiHwM/main.go:22
main.main
/tmp/tmp.j9F8IpiHwM/main.go:11
runtime.main
/usr/local/go/src/runtime/proc.go:183
runtime.goexit
/usr/local/go/src/runtime/asm_amd64.s:2086
goroutine 1 [running]:
runtime/debug.Stack(0x10, 0xc4200459f0, 0x4c0d10)
/usr/local/go/src/runtime/debug/stack.go:24 +0x79
github.com/mgutz/logxi/v1.(*HappyDevFormatter).getLevelContext(0xc42000e900, 0x3, 0xc420014d80, 0x0, 0x0, 0x0, 0x0, 0xc42000ac58, 0x5)
/tmp/tmp.j9F8IpiHwM/main.go:18 +0x31f
var disableCallstack bool
already exists. Can we just export that?