feat: log function name instead of path
This commit is contained in:
parent
b4bf0314f0
commit
0a9b16a0f6
41
main.go
41
main.go
@ -9,8 +9,9 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/natefinch/lumberjack"
|
"github.com/natefinch/lumberjack"
|
||||||
@ -52,22 +53,36 @@ type CLI struct {
|
|||||||
ContainerRuntime string `enum:"podman,docker" default:"podman"`
|
ContainerRuntime string `enum:"podman,docker" default:"podman"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func last(s []string) string {
|
||||||
|
return s[len(s)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugLevel(pc uintptr, file string, line int) string {
|
||||||
|
function := runtime.FuncForPC(pc).Name()
|
||||||
|
if strings.Contains(function, ".") {
|
||||||
|
function = last(strings.Split(function, "."))
|
||||||
|
}
|
||||||
|
return function
|
||||||
|
}
|
||||||
|
|
||||||
|
func traceLevel(pc uintptr, file string, line int) string {
|
||||||
|
function := runtime.FuncForPC(pc).Name()
|
||||||
|
if strings.Contains(function, "/") {
|
||||||
|
function = last(strings.Split(function, "/"))
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%s:%d", file, function, line)
|
||||||
|
}
|
||||||
|
|
||||||
type LogLevelFlag string
|
type LogLevelFlag string
|
||||||
|
|
||||||
func (loglevel LogLevelFlag) AfterApply() error {
|
func (loglevel LogLevelFlag) AfterApply() error {
|
||||||
switch loglevel {
|
switch loglevel {
|
||||||
case "debug", "trace":
|
case "debug":
|
||||||
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
zerolog.CallerMarshalFunc = debugLevel
|
||||||
short := file
|
log.Logger = log.With().Caller().Logger()
|
||||||
for i := len(file) - 1; i > 0; i-- {
|
|
||||||
if file[i] == '/' {
|
case "trace":
|
||||||
short = file[i+1:]
|
zerolog.CallerMarshalFunc = traceLevel
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file = short
|
|
||||||
return file + ":" + strconv.Itoa(line)
|
|
||||||
}
|
|
||||||
log.Logger = log.With().Caller().Logger()
|
log.Logger = log.With().Caller().Logger()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user