From 24d5f9efacc83b545daad13f17918e7b5fe6b1df Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Thu, 10 Mar 2022 14:14:00 +0100 Subject: [PATCH] Enable Report Caller when log level Debug (#258) Enabling report caller when log level equals debug Also added a Caller prettyfier to avoid full file path --- util/log.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/util/log.go b/util/log.go index a17eaf97e..42e41ef56 100644 --- a/util/log.go +++ b/util/log.go @@ -4,7 +4,10 @@ import ( log "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" "io" + "path" "path/filepath" + "runtime" + "strconv" "time" ) @@ -31,6 +34,15 @@ func InitLog(logLevel string, logPath string) error { logFormatter := new(log.TextFormatter) logFormatter.TimestampFormat = time.RFC3339 // or RFC3339 logFormatter.FullTimestamp = true + logFormatter.CallerPrettyfier = func(frame *runtime.Frame) (function string, file string) { + fileName := path.Base(frame.File) + ":" + strconv.Itoa(frame.Line) + //return frame.Function, fileName + return "", fileName + } + + if level == log.DebugLevel { + log.SetReportCaller(true) + } log.SetFormatter(logFormatter) log.SetLevel(level)