From a660203e8ff4c1e2ef49ce632334374cf365efeb Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 14 Jun 2024 05:38:29 -0700 Subject: [PATCH] fix call stack depth bug to show log location correctly --- console_handler.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/console_handler.go b/console_handler.go index a86cd5b..87552c9 100644 --- a/console_handler.go +++ b/console_handler.go @@ -16,7 +16,10 @@ func NewConsoleHandler() *ConsoleHandler { return &ConsoleHandler{} } -func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { +func (c *ConsoleHandler) Handle( + ctx context.Context, + record slog.Record, +) error { timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00") var colorFunc func(format string, a ...interface{}) string @@ -32,16 +35,28 @@ func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error { } // Get the caller information - _, file, line, ok := runtime.Caller(5) + _, file, line, ok := runtime.Caller(4) if !ok { file = "???" line = 0 } - fmt.Println(colorFunc("%s [%s] %s:%d: %s", timestamp, record.Level, file, line, record.Message)) + fmt.Println( + colorFunc( + "%s [%s] %s:%d: %s", + timestamp, + record.Level, + file, + line, + record.Message, + ), + ) return nil } -func (c *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool { +func (c *ConsoleHandler) Enabled( + ctx context.Context, + level slog.Level, +) bool { return true }