pull/455/head
Gordon 2 years ago committed by Xinwei Xiong(cubxxw-openim)
parent d914eb92d4
commit b82062ff54

@ -8,7 +8,6 @@ package log
import ( import (
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"runtime" "runtime"
"strings" "strings"
@ -24,50 +23,51 @@ func (f *fileHook) Levels() []logrus.Level {
return logrus.AllLevels 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 { //func (f *fileHook) Fire(entry *logrus.Entry) error {
// var s string // entry.Data["FilePath"] = findCaller(6)
// _, b, c, _ := runtime.Caller(10) // utils.GetSelfFuncName()
// 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
// return nil // return nil
//} //}
func findCaller(skip int) string { func (f *fileHook) Fire(entry *logrus.Entry) error {
file := "" var s string
line := 0 _, file, line, _ := runtime.Caller(8)
for i := 0; i < 10; i++ { i := strings.SplitAfter(file, "/")
file, line = getCaller(skip + i) if len(i) > 3 {
if !strings.HasPrefix(file, "log") { s = i[len(i)-3] + i[len(i)-2] + i[len(i)-1] + ":" + utils.IntToString(line)
break
}
} }
return fmt.Sprintf("%s:%d", file, line) entry.Data["FilePath"] = s
return nil
} }
func getCaller(skip int) (string, int) { //func findCaller(skip int) string {
_, file, line, ok := runtime.Caller(skip) // file := ""
if !ok { // line := 0
return "", 0 // for i := 0; i < 10; i++ {
} // file, line = getCaller(skip + i)
fmt.Println("skip:", skip, "file:", file, "line", line) // if !strings.HasPrefix(file, "log") {
n := 0 // break
for i := len(file) - 1; i > 0; i-- { // }
if file[i] == '/' { // }
n++ // return fmt.Sprintf("%s:%d", file, line)
if n >= 2 { //}
file = file[i+1:] //
break //func getCaller(skip int) (string, int) {
} // _, file, line, ok := runtime.Caller(skip)
} //
} // if !ok {
return file, line // 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
//}

Loading…
Cancel
Save