dev: improve log position
This commit is contained in:
parent
a73906b74f
commit
4ceada4701
94
log.go
94
log.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -101,7 +102,6 @@ func (log *Logger) SetLogLevelByName(newlv string) (oldlv string) {
|
|||||||
return ErrLogLevel
|
return ErrLogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (log *Logger) Print(prefix string, v interface{}) {
|
func (log *Logger) Print(prefix string, v interface{}) {
|
||||||
var str = ""
|
var str = ""
|
||||||
if pkg, err := json.Marshal(v); err != nil {
|
if pkg, err := json.Marshal(v); err != nil {
|
||||||
@ -168,7 +168,6 @@ func (log *Logger) Println(v ...interface{}){
|
|||||||
log.logwrite(DEBUG, 3, "%s", fmt.Sprintln(v...))
|
log.logwrite(DEBUG, 3, "%s", fmt.Sprintln(v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (log *Logger) GetLogidStr(format string) string {
|
func (log *Logger) GetLogidStr(format string) string {
|
||||||
if logidCreator != nil {
|
if logidCreator != nil {
|
||||||
return fmt.Sprintf(format, logidCreator.GetLogid())
|
return fmt.Sprintf(format, logidCreator.GetLogid())
|
||||||
@ -183,7 +182,7 @@ func (log *Logger) logwrite(typ LogLevel, calldepth int, format string, v ...int
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
idstr = log.GetLogidStr("[%s]")
|
idstr = log.GetLogidStr("[%s]")
|
||||||
prestr = idstr // + "[" + log.module + "] "
|
prestr = formatHeader(calldepth) + "" + idstr + "$ "
|
||||||
)
|
)
|
||||||
|
|
||||||
format = strings.Trim(format, "\n")
|
format = strings.Trim(format, "\n")
|
||||||
@ -227,6 +226,93 @@ func (log *Logger) logwrite(typ LogLevel, calldepth int, format string, v ...int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatHeader(calldepth int) string {
|
||||||
|
const MAX_LENGTH = 35 /* + 22 */
|
||||||
|
// log time
|
||||||
|
now := time.Now()
|
||||||
|
year, month, day := now.Date()
|
||||||
|
hour, min, sec := now.Clock()
|
||||||
|
nowstr := fmt.Sprintf("%4d%02d%02dT%02d:%02d:%02d.%03d ",
|
||||||
|
year, month, day, hour, min, sec, now.Nanosecond()/1e6)
|
||||||
|
|
||||||
|
// log position
|
||||||
|
_, file, line, ok := runtime.Caller(calldepth + 1)
|
||||||
|
if !ok {
|
||||||
|
file = "???"
|
||||||
|
line = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 1: too short path, return directly
|
||||||
|
i, header, linestr := 0, "", fmt.Sprintf("%d", line)
|
||||||
|
if len(file)+len(linestr) < MAX_LENGTH {
|
||||||
|
header = file + ":" + linestr
|
||||||
|
return nowstr + header
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 2: filename long enough, just return filename
|
||||||
|
for i = len(file) - 1; i >= 0; i-- {
|
||||||
|
if file[i] == '/' {
|
||||||
|
header = file[i:] + ":" + linestr
|
||||||
|
i--
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(header) >= MAX_LENGTH && i > 0 {
|
||||||
|
header = "…" + header[:4] + "…" + header[len(header)-MAX_LENGTH+6:]
|
||||||
|
return nowstr + header
|
||||||
|
} else if len(header) >= MAX_LENGTH && i <= 0 {
|
||||||
|
header = header[:4] + "…" + header[len(header)-MAX_LENGTH+5:]
|
||||||
|
return nowstr + header
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 3: try complex middle path with 'width=6'
|
||||||
|
tempheader := formatHeaderPath(file, i, 6) + header
|
||||||
|
if len(tempheader) > MAX_LENGTH {
|
||||||
|
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
|
||||||
|
return nowstr + tempheader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 4: try complex middle path with 'width=8'
|
||||||
|
tempheader = formatHeaderPath(file, i, 8) + header
|
||||||
|
if len(tempheader) > MAX_LENGTH {
|
||||||
|
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
|
||||||
|
return nowstr + tempheader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 5: try complex middle path with 'width=10'
|
||||||
|
tempheader = formatHeaderPath(file, i, 10) + header
|
||||||
|
if len(tempheader) > MAX_LENGTH {
|
||||||
|
tempheader = "…" + tempheader[len(tempheader)-MAX_LENGTH+1:]
|
||||||
|
return nowstr + tempheader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 6: fallback
|
||||||
|
return nowstr + tempheader
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatHeaderPath(file string, i, width int) (paths string) {
|
||||||
|
ii, iw := i, i
|
||||||
|
for i := ii; i >= 0; i-- {
|
||||||
|
if file[i] == '/' {
|
||||||
|
if iw-i > width {
|
||||||
|
// dirname lenght more then width
|
||||||
|
paths = file[i:i+width] + "." + paths
|
||||||
|
} else {
|
||||||
|
// dirname lenght less then width
|
||||||
|
paths = file[i:iw+1] + paths
|
||||||
|
}
|
||||||
|
//fmt.Println("w", iw-i, "width", width, "paths", paths)
|
||||||
|
if width > 2 {
|
||||||
|
// decrease width
|
||||||
|
width--
|
||||||
|
}
|
||||||
|
iw = i - 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
func needWriteLogToFile() bool {
|
func needWriteLogToFile() bool {
|
||||||
if cls, ok := os.LookupEnv("QOOLOG_CLOSE_FILELOG"); ok && cls != "N" && cls != "n" {
|
if cls, ok := os.LookupEnv("QOOLOG_CLOSE_FILELOG"); ok && cls != "N" && cls != "n" {
|
||||||
return false
|
return false
|
||||||
@ -262,7 +348,7 @@ func initlog() {
|
|||||||
logDirsOptional = []string{"./log", "/var/log"}
|
logDirsOptional = []string{"./log", "/var/log"}
|
||||||
logSplitTag = "20060102" //20060102150405
|
logSplitTag = "20060102" //20060102150405
|
||||||
baseWriter = NewBaseWriter(nil, os.Stdout, nil)
|
baseWriter = NewBaseWriter(nil, os.Stdout, nil)
|
||||||
gologFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
|
gologFlags = 0 /* log.Ldate | log.Lmicroseconds | log.Lshortfile */
|
||||||
)
|
)
|
||||||
|
|
||||||
// Step 2.1 add stdout to base writer if need
|
// Step 2.1 add stdout to base writer if need
|
||||||
|
Loading…
Reference in New Issue
Block a user