send-sls-logger 0.0.18 → 0.0.20

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/index.d.ts CHANGED
@@ -12,10 +12,10 @@ interface initConfig {
12
12
  options?: Options;
13
13
  }
14
14
 
15
- export = logger;
16
- export as namespace logger;
15
+ export = slsLog;
16
+ export as namespace slsLog;
17
17
 
18
- declare namespace logger {
18
+ declare namespace slsLog {
19
19
  function error(str: string | StringObject): void;
20
20
  function warn(str: string | StringObject): void;
21
21
  function log(str: string | StringObject): void;
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "send-sls-logger",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "阿里云sls的logger发送",
5
- "main": "index.js",
5
+ "main": "src/index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
package/src/index.js ADDED
@@ -0,0 +1,69 @@
1
+ import SlsTracker from "@aliyun-sls/web-track-browser";
2
+
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
+
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
+
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
+
50
+ const error = send("error");
51
+ const warn = send("warn");
52
+ const log = send("info");
53
+ const info = send("info");
54
+
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 SlsTracker(opts);
66
+ }
67
+ exclude.push(...defaultExclude);
68
+ },
69
+ };
@@ -1,4 +1,3 @@
1
- import SlsTracker from "@aliyun-sls/web-track-browser";
2
1
  import SlsMiniTracker from "@aliyun-sls/web-track-mini";
3
2
 
4
3
  const opts = {
@@ -63,11 +62,7 @@ export default {
63
62
  Object.assign(defaultLogData, defaultData);
64
63
  Object.assign(opts, options);
65
64
  if (options.host && options.logstore && options.project) {
66
- if (options.isMiniApp) {
67
- tracker = new SlsMiniTracker(opts);
68
- } else {
69
- tracker = new SlsTracker(opts);
70
- }
65
+ tracker = new SlsMiniTracker(opts);
71
66
  }
72
67
  exclude.push(...defaultExclude);
73
68
  },