-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylogger.go
68 lines (61 loc) · 1.05 KB
/
mylogger.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 自定义日志库
package mylogger
import (
"errors"
"fmt"
"path"
"runtime"
"strings"
)
type LogLevel uint16
const (
Invaild LogLevel = iota
DEBUG
INFO
WARNING
ERROR
FATAL
)
func getCallInfo(skip int) (fileName, funcName string, lineNo int) {
// 获取调用日志调用信息,包括行数、文件名、函数等
pc, file, lineNo, ok := runtime.Caller(skip)
if !ok {
fmt.Println("获取行数失败")
return
}
funcName = runtime.FuncForPC(pc).Name()
fileName = path.Base(file)
return
}
func parseLogLevel(s string) (LogLevel, error) {
s = strings.ToLower(s)
switch s {
case "debug":
return DEBUG, nil
case "info":
return INFO, nil
case "warning":
return WARNING, nil
case "error":
return ERROR, nil
default:
err := errors.New("无效的日志级别")
return Invaild, err
}
}
func getLogLevel(level LogLevel) string {
switch level {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARNING:
return "WARNING"
case ERROR:
return "ERROR"
case FATAL:
return "FATAL"
default:
return "DEBUG"
}
}