code_log.go 4.32 KB
Newer Older
“李磊”'s avatar
“李磊” committed
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package l

import (
	"errors"
	"fmt"
)

/*
本包ERROR函数使用
1.在首位传入错误码,函数自动使用首位的错误码
2.在使用WrapError,WrapErrorf,GetError,GetError这些函数会返回一个携带错误码的error(ErrorCode)
3.可以不在函数中传入错误码,可传入一个携带错误码的error(ErrorCode),以下函数会自动解析传入函数中的error中的错误码
4.如果以下函数不传入错误码,也不传入携带错误码的error(ErrorCode),会正常打印普通error信息
*/

func Error(msgs ...interface{}) {
	if logger.logLevel > LevelError {
		return
	}
	code, firstArg := getErrCode(msgs...)
	if code != "" {
		msgs = append(msgs, code)
	}
	if firstArg && len(msgs) >= 2 {
		logMsg("ERROR", msgs[1:]...)
		return
	}
	logMsg("ERROR", msgs...)
}

func Errorf(msgs ...interface{}) {
	if logger.logLevel > LevelError {
		return
	}
	code, firstArg := getErrCode(msgs...)
	switch len(msgs) {
	case 0:
		logMsg("ERROR", msgs...)
	case 1:
		if code != "" && !firstArg {
			msgs = append(msgs, code)
		}
		logMsg("ERROR", msgs...)
	case 2:
		if firstArg {
			msgs = append(msgs, code)
			logMsg("ERROR", msgs[1:]...)
			return
		}
		if _, ok := msgs[0].(string); ok {
			logMsg("ERROR", fmt.Sprintf(msgs[0].(string), msgs[1:]...), code)
		}
	default:
		if firstArg {
			if _, ok := msgs[1].(string); ok {
				logMsg("ERROR", fmt.Sprintf(msgs[1].(string), msgs[2:]...), code)
			}
			return
		}
		if _, ok := msgs[0].(string); ok {
			logMsg("ERROR", fmt.Sprintf(msgs[0].(string), msgs[1:]...), code)
		}
	}
}

func WrapError(msgs ...interface{}) error {
	code, firstArg := getErrCode(msgs...)
	fmtStr := getFmtMsg(msgs...)
	if firstArg {
		fmtStr = getFmtMsg(msgs[1:]...)
	}
	if logger.logLevel <= LevelError {
		logMsg("ERROR", fmtStr, code)
	}
	return ErrorCode{
		code,
		errors.New(fmtStr),
	}
}

func WrapErrorf(msgs ...interface{}) error {
	code, firstArg := getErrCode(msgs...)
	var fmtStr string
	switch len(msgs) {
	case 0:
		fmtStr = ""
	case 1:
		if firstArg {
			fmtStr = ""
			break
		}
		fmtStr = fmt.Sprintf("%v", msgs[0])
	case 2:
		if firstArg {
			fmtStr = fmt.Sprintf("%v", msgs[1])
			break
		}
		if _, ok := msgs[0].(string); ok {
			fmtStr = fmt.Sprintf(msgs[0].(string), msgs[1:]...)
		}
	default:
		if firstArg {
			if _, ok := msgs[1].(string); ok {
				fmtStr = fmt.Sprintf(msgs[1].(string), msgs[2:]...)
			}
			break
		}
		if _, ok := msgs[0].(string); ok {
			fmtStr = fmt.Sprintf(msgs[0].(string), msgs[1:]...)
		}
	}
	if logger.logLevel <= LevelError {
		logMsg("ERROR", fmtStr, code)
	}
	return ErrorCode{
		code,
		errors.New(fmtStr),
	}
}

func GetError(msgs ...interface{}) error {
	code, firstArg := getErrCode(msgs...)
	if code == "" {
		return wrapError(msgs...)
	}
	fmtStr := getFmtMsg(msgs...)
	if firstArg {
		fmtStr = getFmtMsg(msgs[1:]...)
	}
	return ErrorCode{
		code,
		errors.New(fmtStr),
	}
}

func GetErrorf(msgs ...interface{}) error {
	code, firstArg := getErrCode(msgs...)
	var fmtStr string
	switch len(msgs) {
	case 0:
		fmtStr = ""
	case 1:
		if firstArg {
			fmtStr = ""
			break
		}
		fmtStr = fmt.Sprintf("%v", msgs[0])
	case 2:
		if firstArg {
			fmtStr = fmt.Sprintf("%v", msgs[1])
			break
		}
		if _, ok := msgs[0].(string); ok {
			fmtStr = fmt.Sprintf(msgs[0].(string), msgs[1:]...)
		}
	default:
		if firstArg {
			if _, ok := msgs[1].(string); ok {
				fmtStr = fmt.Sprintf(msgs[1].(string), msgs[2:]...)
			}
			break
		}
		if _, ok := msgs[0].(string); ok {
			fmtStr = fmt.Sprintf(msgs[0].(string), msgs[1:]...)
		}
	}
	return ErrorCode{
		code,
		errors.New(fmtStr),
	}
}

// 获取错误码,优先获取手动输入的错误码,其次获取解析error中的错误码
func getErrCode(msgs ...interface{}) (errCode ErrCode, firstArg bool) {
	for i, v := range msgs {
		if code, ok := v.(ErrCode); ok && i == 0 {
			errCode = code
			firstArg = true
			break
		}
		if _, ok := v.(ErrorCode); !ok {
			continue
		}
		errCode = v.(ErrorCode).Code
	}
	return errCode, firstArg
}

func getFmtMsg(msgs ...interface{}) string {
	fmtStr := ""
	for i, msg := range msgs {
		if i == 0 {
			fmtStr = fmt.Sprintf("%+v", msg)
		} else {
			fmtStr = fmt.Sprintf("%s %+v", fmtStr, msg)
		}
	}
	return fmtStr
}