pull/168/head
Gordon 3 years ago
parent 1bcd2d4531
commit a7c8eda290

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