index.go
5.68 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package lxalilog
import (
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/producer"
"github.com/astaxie/beego/logs"
lconv "github.com/lixu-any/go-tools/conv"
ltime "github.com/lixu-any/go-tools/time"
)
var (
LXAliProducer *producer.Producer
LXAliDefaultConfig MapAlilogConfig //默认配置
)
type MapAlilogConfig struct {
Endpoint string
AccessKeyId string
AppNaame string
AccessKeySecret string
Project string
On string
Debug string
TableError string
TableDebug string
Env string
}
// 初始化阿里云日志
func InitAliLog(config MapAlilogConfig) {
LXAliDefaultConfig = config
if config.On != "1" {
return
}
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = config.Endpoint
producerConfig.AccessKeyID = config.AccessKeyId
producerConfig.AccessKeySecret = config.AccessKeySecret
LXAliProducer = producer.InitProducer(producerConfig)
ch := make(chan os.Signal)
signal.Notify(ch)
LXAliProducer.Start()
//LXAliProducer.SafeClose() // 安全关闭
}
func FormatLog(f interface{}, v ...interface{}) string {
return formatLog(f, v)
}
func formatLog(f interface{}, v ...interface{}) string {
var msg string
switch f := f.(type) {
case string:
msg = f
if len(v) == 0 {
return msg
}
if strings.Contains(msg, "%") && !strings.Contains(msg, "%%") {
//format string
} else {
//do not contain format char
msg += strings.Repeat(" %v", len(v))
}
default:
msg = fmt.Sprint(f)
if len(v) == 0 {
return msg
}
msg += strings.Repeat(" %v", len(v))
}
return fmt.Sprintf(msg, v...)
}
// 写日志到阿里云
func Writelog(s string, topic string, ip string, config map[string]string) (err error) {
if LXAliDefaultConfig.On != "1" {
return
}
if LXAliDefaultConfig.Env != "" {
config["env"] = LXAliDefaultConfig.Env
}
log := producer.GenerateLog(uint32(time.Now().Unix()), config)
err = LXAliProducer.SendLog(LXAliDefaultConfig.Project, s, topic, ip, log)
if err != nil {
return
}
return
}
// 获取正在运行的函数名
func runFuncName(l int) (string, string, int) {
pc, file, line, _ := runtime.Caller(l)
name := runtime.FuncForPC(pc).Name()
split := strings.Split(name, ".")
funname := split[len(split)-1]
return funname, file, line
}
// ErrorCode 错误日志
func ErrorCode(code interface{}, v ...interface{}) {
funname, file, line := runFuncName(3)
logd := make(map[string]string)
logd["fun"] = funname
logd["file"] = file
logd["app"] = LXAliDefaultConfig.AppNaame
logd["line"] = fmt.Sprintf("%d", line)
logd["msg"] = formatLog(code, v)
logd["time"] = lconv.Int64ToStr(ltime.UninxTime())
logs.Error("file::", file, ",line::", line, ",fun::", funname, "[", logd["msg"], "],time::", logd["time"])
if LXAliDefaultConfig.TableError != "" {
go Writelog(LXAliDefaultConfig.TableError, funname, "127.0.0.1", logd)
}
}
// 错误日志
func Errors(f interface{}, v ...interface{}) {
funname, file, line := runFuncName(2)
logd := make(map[string]string)
logd["fun"] = funname
logd["file"] = file
logd["app"] = LXAliDefaultConfig.AppNaame
logd["line"] = fmt.Sprintf("%d", line)
logd["msg"] = formatLog(f, v)
logd["time"] = lconv.Int64ToStr(ltime.UninxTime())
logs.Error("file::", file, ",line::", line, ",fun::", funname, "[", logd["msg"], "],time::", logd["time"])
if LXAliDefaultConfig.TableError != "" {
go Writelog(LXAliDefaultConfig.TableError, funname, "127.0.0.1", logd)
}
}
// 调试日志
func Debug(f interface{}, v ...interface{}) {
funname, file, line := runFuncName(2)
logd := make(map[string]string)
logd["fun"] = funname
logd["file"] = file
logd["app"] = LXAliDefaultConfig.AppNaame
logd["line"] = fmt.Sprintf("%d", line)
logd["msg"] = formatLog(f, v)
logd["time"] = lconv.Int64ToStr(ltime.UninxTime())
logs.Debug("file::", file, ",line::", line, ",fun::", funname, "[", logd["msg"], "],time::", logd["time"])
if LXAliDefaultConfig.Debug == "1" && LXAliDefaultConfig.TableDebug != "" {
go Writelog(LXAliDefaultConfig.TableDebug, funname, "127.0.0.1", logd)
}
}
type LogParams struct {
Project string
PowerSql bool
}
// 查询日志
func GetLog(logname, sql string, starttime, endtime int64, nums int64, p ...LogParams) (lst []map[string]string, err error) {
var dproject = LXAliDefaultConfig.Project
if len(p) > 0 {
if p[0].Project != "" {
dproject = p[0].Project
}
}
// 创建日志服务Client。
client := sls.CreateNormalInterface(LXAliDefaultConfig.Endpoint, LXAliDefaultConfig.AccessKeyId, LXAliDefaultConfig.AccessKeySecret, "")
resp, err := client.GetLogs(dproject, logname, "", starttime, endtime, sql, nums, 0, true)
if err != nil {
return
}
logs := resp.Logs
for i := range logs {
var item = make(map[string]string)
for k, v := range logs[i] {
item[k] = v
}
lst = append(lst, item)
}
return
}
func GetLog1(logname, sql string, starttime, endtime int64, nums int64, pas int64) (lst []map[string]string, err error) {
// 创建日志服务Client。
client := sls.CreateNormalInterface(LXAliDefaultConfig.Endpoint, LXAliDefaultConfig.AccessKeyId, LXAliDefaultConfig.AccessKeySecret, "")
resp, err := client.GetLogs(LXAliDefaultConfig.Project, logname, "", starttime, endtime, sql, nums, pas*nums, true)
if err != nil {
return
}
logs := resp.Logs
for i := range logs {
var item = make(map[string]string)
for k, v := range logs[i] {
item[k] = v
}
lst = append(lst, item)
}
return
}
func GetLogV2(logname, sql string, starttime, endtime int64, nums int64, pas int64) (lst []map[string]string, err error) {
return GetLog1(logname, sql, starttime, endtime, nums, pas)
}