From e45e0c0d2bd6e183482994d0f587e0c1015404ed Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Sun, 25 Aug 2024 10:44:33 +0700 Subject: [PATCH] log: added log.source config option --- go/config/default.go | 1 + go/pkg/log/pretty.go | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go/config/default.go b/go/config/default.go index 514d42d..7f8bf4b 100644 --- a/go/config/default.go +++ b/go/config/default.go @@ -31,6 +31,7 @@ var DefaultConfig = Entries{ {"log.file.enable", true, "Enable file logging", false}, {"log.level", "info", `Log level. Possible values: "debug", "info", "warn", "error"`, false}, {"log.output", "stderr", "Log output. Possible values: \"stdout\", \"stderr\"", false}, + {"log.source", true, "Whether to add file location to logs", false}, {"db.driver", "sqlite3", "Database driver", false}, {"db.path", path.Join(xdg.Home, ".local", "share", "bluemage", "data.db"), "Database path", false}, diff --git a/go/pkg/log/pretty.go b/go/pkg/log/pretty.go index e0ef96a..6b19a4f 100644 --- a/go/pkg/log/pretty.go +++ b/go/pkg/log/pretty.go @@ -144,10 +144,13 @@ func (pr *PrettyHandler) Handle(ctx context.Context, record slog.Record) error { if record.PC != 0 && pr.opts.AddSource { frame := caller.From(record.PC).Frame - levelColor.Fprint(buf, frame.File) + cwd, _ := os.Getwd() + f := strings.TrimPrefix(frame.File, cwd) + f = strings.TrimPrefix(f, string(os.PathSeparator)) + levelColor.Fprint(buf, f) levelColor.Fprint(buf, ":") levelColor.Fprint(buf, frame.Line) - levelColor.Fprint(buf, " -- ") + levelColor.Fprint(buf, " :: ") split := strings.Split(frame.Function, string(os.PathSeparator)) fnName := split[len(split)-1] levelColor.Fprint(buf, fnName)