1
0

Show file:line only for debug log level

This commit is contained in:
dump_stack() 2023-03-22 17:36:04 +00:00
parent d6c678b0cd
commit 0b198f71ca
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

26
main.go
View File

@ -47,6 +47,19 @@ func (loglevel LogLevelFlag) AfterApply() error {
switch loglevel {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return file + ":" + strconv.Itoa(line)
}
log.Logger = log.With().Caller().Logger()
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
@ -70,19 +83,6 @@ func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return file + ":" + strconv.Itoa(line)
}
log.Logger = log.With().Caller().Logger()
rand.Seed(time.Now().UnixNano())
cli := CLI{}