send-sls-logger 0.0.45 → 0.0.46
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/lib/init.js +92 -92
- package/package.json +1 -1
package/lib/init.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const opts = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
host: "", // 所在地域的服务入口。例如cn-hangzhou.log.aliyuncs.com
|
|
3
|
+
project: "", // Project名称。
|
|
4
|
+
logstore: "", // Logstore名称。
|
|
5
|
+
time: 5, // 发送日志的时间间隔,默认是10秒。
|
|
6
|
+
count: 10, // 发送日志的数量大小,默认是10条。
|
|
7
7
|
};
|
|
8
8
|
const defaultLogData = {};
|
|
9
9
|
const exclude = [];
|
|
@@ -12,84 +12,84 @@ let userFormatResult;
|
|
|
12
12
|
let debug;
|
|
13
13
|
|
|
14
14
|
const getType = (val) => {
|
|
15
|
-
|
|
15
|
+
return Object.prototype.toString.call(val).toLocaleLowerCase().slice(8, -1);
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
let maxLength = 200;
|
|
19
19
|
const formatDataMaxLength = (data) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
if (getType(data) === "object") {
|
|
21
|
+
return Object.keys(data).reduce((curr, key) => {
|
|
22
|
+
curr[key] = /token/.test(key)
|
|
23
|
+
? data[key]
|
|
24
|
+
: formatDataMaxLength(data[key]);
|
|
25
|
+
return curr;
|
|
26
|
+
}, {});
|
|
27
|
+
} else if (getType(data) === "array") {
|
|
28
|
+
return data.map((i) => formatDataMaxLength(i));
|
|
29
|
+
} else if (getType(data) === "string" && data.length > maxLength) {
|
|
30
|
+
return `${data.slice(0, maxLength)}...${data.length - maxLength}个字已忽略`;
|
|
31
|
+
} else {
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
const formatFunctionObj = (data) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
if (getType(data) === "object") {
|
|
38
|
+
return Object.keys(data).reduce((curr, key) => {
|
|
39
|
+
curr[key] = formatFunctionObj(data[key]);
|
|
40
|
+
return curr;
|
|
41
|
+
}, {});
|
|
42
|
+
} else if (getType(data) === "array") {
|
|
43
|
+
return data.map((i) => formatFunctionObj(i));
|
|
44
|
+
} else if (getType(data) === "function") {
|
|
45
|
+
return data();
|
|
46
|
+
} else {
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
49
|
};
|
|
50
50
|
const formatLogData = (data, type = "log") => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
const result = { ...defaultLogData, type };
|
|
52
|
+
if (typeof data === "string") {
|
|
53
|
+
Object.assign(result, { data });
|
|
54
|
+
} else if (typeof data === "object") {
|
|
55
|
+
Object.assign(result, data);
|
|
56
|
+
}
|
|
57
|
+
return formatFunctionObj(result);
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
const send = (type) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
return function (err, activeFormatResult) {
|
|
62
|
+
let data = {};
|
|
63
|
+
if (typeof err === "string") {
|
|
64
|
+
data.msg = err;
|
|
65
|
+
} else if (typeof err === "object") {
|
|
66
|
+
data = { ...err };
|
|
67
|
+
}
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
const result = formatLogData(data, type);
|
|
70
|
+
const isExcludeMsg = Object.keys(result).find((key) => {
|
|
71
|
+
return exclude.find((i) => new RegExp(i, "g").test(result[key]));
|
|
72
|
+
});
|
|
73
73
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
if (!opts.host || !opts.project || !opts.logstore) {
|
|
75
|
+
return console.warn("日志初始化需要配置options", result);
|
|
76
|
+
}
|
|
77
|
+
let formatResultObj = formatDataMaxLength(result);
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
if (getType(userFormatResult) === "function") {
|
|
80
|
+
formatResultObj = userFormatResult(formatResultObj) || formatResultObj;
|
|
81
|
+
}
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
if (getType(activeFormatResult) === "function") {
|
|
84
|
+
formatResultObj = activeFormatResult(formatResultObj) || formatResultObj;
|
|
85
|
+
}
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
if (isExcludeMsg) {
|
|
88
|
+
debug && console.log(`logger ${type}`, formatResultObj);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
tracker.send(formatResultObj);
|
|
92
|
+
};
|
|
93
93
|
};
|
|
94
94
|
|
|
95
95
|
const error = send("error");
|
|
@@ -98,34 +98,34 @@ const log = send("info");
|
|
|
98
98
|
const info = send("info");
|
|
99
99
|
|
|
100
100
|
const logInit = (Tracker) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
101
|
+
return (config) => {
|
|
102
|
+
const {
|
|
103
|
+
defaultData = {},
|
|
104
|
+
defaultExclude = [],
|
|
105
|
+
options = {},
|
|
106
|
+
strMaxLength = maxLength,
|
|
107
|
+
debug: defaultDebug = debug,
|
|
108
|
+
userFormatResult: defaultUserFormatResult = userFormatResult,
|
|
109
|
+
} = config;
|
|
110
110
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
111
|
+
if (defaultDebug !== undefined) {
|
|
112
|
+
debug = defaultDebug;
|
|
113
|
+
}
|
|
114
|
+
userFormatResult = defaultUserFormatResult;
|
|
115
|
+
Object.assign(defaultLogData, defaultData);
|
|
116
|
+
Object.assign(opts, options);
|
|
117
|
+
if (options.host && options.logstore && options.project) {
|
|
118
|
+
tracker = new Tracker(opts);
|
|
119
|
+
}
|
|
120
|
+
maxLength = strMaxLength;
|
|
121
|
+
exclude.push(...defaultExclude);
|
|
122
|
+
};
|
|
123
123
|
};
|
|
124
124
|
|
|
125
125
|
export default {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
126
|
+
error,
|
|
127
|
+
warn,
|
|
128
|
+
log,
|
|
129
|
+
info,
|
|
130
|
+
logInit,
|
|
131
131
|
};
|