-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
50 lines (44 loc) · 943 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package hm_log
import (
"fmt"
"path"
"runtime"
"time"
)
type LogData struct {
Message string
TimeStr string
LevelStr string
FileName string
FuncName string
LineNo int
IsWarn bool
}
func GetLineInfo() (fileName, funcName string, lineNo int) {
pc, file, line, ok := runtime.Caller(3)
if ok {
fileName = file
funcName = runtime.FuncForPC(pc).Name()
lineNo = line
}
return
}
func MsgInfo(level int, format string, args ...interface{}) *LogData {
now := time.Now()
nowStr := now.Format("2006-01-02 15:04:05.999")
levelStr := LogLevelString(level)
fileName, funcName, lineNo := GetLineInfo()
fileName = path.Base(fileName)
funcName = path.Base(funcName)
msg := fmt.Sprintf(format, args...)
isWarn := level >= WarnLevel && level <= FatalLevel
return &LogData{
Message: msg,
TimeStr: nowStr,
LevelStr: levelStr,
FileName: fileName,
FuncName: funcName,
LineNo: lineNo,
IsWarn: isWarn,
}
}