mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-31 08:29:33 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			61 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| /*
 | |
| ** description("get the name and line number of the calling file hook").
 | |
| ** copyright('tuoyun,www.tuoyun.net').
 | |
| ** author("fg,Gordon@tuoyun.net").
 | |
| ** time(2021/3/16 11:26).
 | |
|  */
 | |
| package log
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"github.com/sirupsen/logrus"
 | |
| 	"runtime"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| type fileHook struct{}
 | |
| 
 | |
| func newFileHook() *fileHook {
 | |
| 	return &fileHook{}
 | |
| }
 | |
| 
 | |
| func (f *fileHook) Levels() []logrus.Level {
 | |
| 	return logrus.AllLevels
 | |
| }
 | |
| 
 | |
| func (f *fileHook) Fire(entry *logrus.Entry) error {
 | |
| 	entry.Data["FilePath"] = findCaller(5)
 | |
| 	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
 | |
| 		}
 | |
| 	}
 | |
| 	return fmt.Sprintf("%s:%d", file, line)
 | |
| }
 | |
| 
 | |
| func getCaller(skip int) (string, int) {
 | |
| 	_, file, line, ok := runtime.Caller(skip)
 | |
| 	if !ok {
 | |
| 		return "", 0
 | |
| 	}
 | |
| 
 | |
| 	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
 | |
| }
 |