log: added log.source config option

This commit is contained in:
Tigor Hutasuhut 2024-08-25 10:44:33 +07:00
parent 1ce878fdfb
commit e45e0c0d2b
2 changed files with 6 additions and 2 deletions

View file

@ -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},

View file

@ -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)