send-sls-logger 0.0.21 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/mini.js +64 -64
package/package.json
CHANGED
package/src/mini.js
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
import SlsMiniTracker from "@aliyun-sls/web-track-mini";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
const opts = {
|
|
4
|
+
host: "", // 所在地域的服务入口。例如cn-hangzhou.log.aliyuncs.com
|
|
5
|
+
project: "", // Project名称。
|
|
6
|
+
logstore: "", // Logstore名称。
|
|
7
|
+
time: 5, // 发送日志的时间间隔,默认是10秒。
|
|
8
|
+
count: 10, // 发送日志的数量大小,默认是10条。
|
|
9
|
+
};
|
|
10
|
+
const defaultLogData = {};
|
|
11
|
+
const exclude = [];
|
|
12
|
+
let tracker;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
const formatLogData = (data, type = "log") => {
|
|
15
|
+
const result = {
|
|
16
|
+
type,
|
|
17
|
+
...defaultLogData,
|
|
18
|
+
};
|
|
19
|
+
if (typeof data === "string") {
|
|
20
|
+
return { ...result, data };
|
|
21
|
+
} else if (typeof data === "object") {
|
|
22
|
+
return { ...result, ...data };
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
27
|
+
const send = (type) => {
|
|
28
|
+
return function (err) {
|
|
29
|
+
let data = {};
|
|
30
|
+
if (typeof err === "string") {
|
|
31
|
+
data.msg = err;
|
|
32
|
+
} else if (typeof err === "object") {
|
|
33
|
+
data = { ...err };
|
|
34
|
+
}
|
|
35
|
+
const isExcludeMsg = Object.keys(data).find((key) => {
|
|
36
|
+
return exclude.find((i) => new RegExp(i, "g").test(data[key]));
|
|
37
|
+
});
|
|
38
|
+
if (isExcludeMsg) {
|
|
39
|
+
console.log(data);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const result = formatLogData(data, type);
|
|
43
|
+
if (!opts.host || !opts.project || !opts.logstore) {
|
|
44
|
+
return console.warn("日志初始化需要配置options", result);
|
|
45
|
+
}
|
|
46
|
+
tracker.send(result);
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
const error = send("error");
|
|
51
|
+
const warn = send("warn");
|
|
52
|
+
const log = send("info");
|
|
53
|
+
const info = send("info");
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
55
|
+
export default {
|
|
56
|
+
error,
|
|
57
|
+
warn,
|
|
58
|
+
log,
|
|
59
|
+
info,
|
|
60
|
+
init: (config) => {
|
|
61
|
+
const { defaultData = {}, defaultExclude = [], options = {} } = config;
|
|
62
|
+
Object.assign(defaultLogData, defaultData);
|
|
63
|
+
Object.assign(opts, options);
|
|
64
|
+
if (options.host && options.logstore && options.project) {
|
|
65
|
+
tracker = new SlsMiniTracker(opts);
|
|
66
|
+
}
|
|
67
|
+
exclude.push(...defaultExclude);
|
|
68
|
+
},
|
|
69
|
+
};
|