对日志监听器新增文件名和行数

This commit is contained in:
boyce
2019-03-26 14:39:26 +08:00
parent e0ae07388f
commit b0156ce788
4 changed files with 29 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
@@ -108,6 +110,20 @@ func (slf *LogModule) Printf(level uint, format string, v ...interface{}) {
}
if slf.listenFun != nil {
var file string
var line int
var ok bool
_, file, line, ok = runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
parts := strings.Split(file, "/")
if len(parts) > 0 {
file = parts[len(parts)-1]
}
format = LogPrefix[level] + time.Now().Format("2006/1/2 15:04:05") + fmt.Sprintf(" %s:%d:", file, line) + format
slf.listenFun(level, fmt.Sprintf(format, v...))
}
}