mirror of
https://github.com/zebrajr/ollama.git
synced 2025-12-06 00:19:51 +01:00
logutil: fix source field (#12279)
This commit is contained in:
parent
fa1c987a29
commit
b225508c9b
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const LevelTrace slog.Level = -8
|
const LevelTrace slog.Level = -8
|
||||||
|
|
@ -29,10 +31,18 @@ func NewLogger(w io.Writer, level slog.Level) *slog.Logger {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type key string
|
||||||
|
|
||||||
func Trace(msg string, args ...any) {
|
func Trace(msg string, args ...any) {
|
||||||
slog.Log(context.TODO(), LevelTrace, msg, args...)
|
TraceContext(context.WithValue(context.TODO(), key("skip"), 1), msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TraceContext(ctx context.Context, msg string, args ...any) {
|
func TraceContext(ctx context.Context, msg string, args ...any) {
|
||||||
slog.Log(ctx, LevelTrace, msg, args...)
|
if logger := slog.Default(); logger.Enabled(ctx, LevelTrace) {
|
||||||
|
skip, _ := ctx.Value(key("skip")).(int)
|
||||||
|
pc, _, _, _ := runtime.Caller(1 + skip)
|
||||||
|
record := slog.NewRecord(time.Now(), LevelTrace, msg, pc)
|
||||||
|
record.Add(args...)
|
||||||
|
logger.Handler().Handle(ctx, record)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user