From b82062ff547f77e5b6b05baa2135dec4d5b6403c Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Sat, 10 Sep 2022 00:15:31 +0800 Subject: [PATCH] log test --- pkg/common/log/file_line_hk.go | 82 +++++++++++++++++----------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/pkg/common/log/file_line_hk.go b/pkg/common/log/file_line_hk.go index 23d46c271..fd6047a2f 100644 --- a/pkg/common/log/file_line_hk.go +++ b/pkg/common/log/file_line_hk.go @@ -8,7 +8,6 @@ package log import ( "Open_IM/pkg/utils" - "fmt" "github.com/sirupsen/logrus" "runtime" "strings" @@ -24,50 +23,51 @@ func (f *fileHook) Levels() []logrus.Level { return logrus.AllLevels } -func (f *fileHook) Fire(entry *logrus.Entry) error { - entry.Data["FilePath"] = findCaller(6) - utils.GetSelfFuncName() - return nil -} - //func (f *fileHook) Fire(entry *logrus.Entry) error { -// var s string -// _, b, c, _ := runtime.Caller(10) -// i := strings.SplitAfter(b, "/") -// if len(i) > 3 { -// s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(c) -// } -// entry.Data["FilePath"] = s +// entry.Data["FilePath"] = findCaller(6) +// utils.GetSelfFuncName() // return nil //} -func findCaller(skip int) string { - file := "" - line := 0 - for i := 0; i < 10; i++ { - file, line = getCaller(skip + i) - if !strings.HasPrefix(file, "log") { - break - } +func (f *fileHook) Fire(entry *logrus.Entry) error { + var s string + _, file, line, _ := runtime.Caller(8) + i := strings.SplitAfter(file, "/") + if len(i) > 3 { + s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line) } - return fmt.Sprintf("%s:%d", file, line) + entry.Data["FilePath"] = s + return nil } -func getCaller(skip int) (string, int) { - _, file, line, ok := runtime.Caller(skip) - if !ok { - return "", 0 - } - fmt.Println("skip:", skip, "file:", file, "line", line) - n := 0 - for i := len(file) - 1; i > 0; i-- { - if file[i] == '/' { - n++ - if n >= 2 { - file = file[i+1:] - break - } - } - } - return file, line -} +//func findCaller(skip int) string { +// file := "" +// line := 0 +// for i := 0; i < 10; i++ { +// file, line = getCaller(skip + i) +// if !strings.HasPrefix(file, "log") { +// break +// } +// } +// return fmt.Sprintf("%s:%d", file, line) +//} +// +//func getCaller(skip int) (string, int) { +// _, file, line, ok := runtime.Caller(skip) +// +// if !ok { +// return "", 0 +// } +// fmt.Println("skip:", skip, "file:", file, "line", line) +// n := 0 +// for i := len(file) - 1; i > 0; i-- { +// if file[i] == '/' { +// n++ +// if n >= 2 { +// file = file[i+1:] +// break +// } +// } +// } +// return file, line +//}