zen/core/zlog/source.go

29 lines
508 B
Go
Raw Normal View History

2024-09-03 13:38:45 +07:00
package zlog
import (
"log/slog"
"os"
"strings"
)
type slogSource struct {
*slog.Source
}
func (sl slogSource) LogValue() slog.Value {
wd, _ := os.Getwd()
file, found := strings.CutPrefix(sl.File, wd)
if found {
file = strings.TrimPrefix(file, string(os.PathSeparator))
}
split := strings.Split(sl.Function, string(os.PathSeparator))
function := split[len(split)-1]
return slog.GroupValue(
slog.String("file", file),
slog.Int("line", sl.Line),
slog.String("function", function),
)
}