send-sls-logger 0.0.25 → 0.0.27

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/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { WebTrackerBrowserOptions } from "@aliyun-sls/web-track-browser";
2
- import miniLog from "./mini";
3
2
 
4
3
  interface StringObject {
5
4
  [name: string]: string;
@@ -24,5 +23,3 @@ declare namespace slsLog {
24
23
  function info(str: string | StringObject): void;
25
24
  function init(config: initConfig): void;
26
25
  }
27
-
28
- export { miniLog };
package/lib/index.js CHANGED
@@ -1,69 +1,11 @@
1
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");
2
+ import { error, warn, log, info, logInit } from "./init";
3
+ const init = logInit(SlsTracker);
54
4
 
55
5
  export default {
56
6
  error,
57
7
  warn,
58
8
  log,
59
9
  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
- },
10
+ init,
69
11
  };
package/lib/init.js ADDED
@@ -0,0 +1,94 @@
1
+ const opts = {
2
+ host: "", // 所在地域的服务入口。例如cn-hangzhou.log.aliyuncs.com
3
+ project: "", // Project名称。
4
+ logstore: "", // Logstore名称。
5
+ time: 5, // 发送日志的时间间隔,默认是10秒。
6
+ count: 10, // 发送日志的数量大小,默认是10条。
7
+ };
8
+ const defaultLogData = {};
9
+ const exclude = [];
10
+ let tracker;
11
+
12
+ let maxLength = 200;
13
+ const formatDataMaxLength = (data) => {
14
+ if (getType(data) === "object") {
15
+ return Object.keys(data).reduce((curr, key) => {
16
+ curr[key] = formatDataMaxLength(data[key]);
17
+ return curr;
18
+ }, {});
19
+ } else if (getType(data) === "array") {
20
+ return data.map((i) => formatDataMaxLength(i));
21
+ } else if (getType(data) === "string" && data.length > maxLength) {
22
+ return `${data.slice(0, maxLength)}...`;
23
+ } else {
24
+ return data;
25
+ }
26
+ };
27
+
28
+ const formatLogData = (data, type = "log") => {
29
+ const result = {
30
+ type,
31
+ ...defaultLogData,
32
+ };
33
+ if (typeof data === "string") {
34
+ return { ...result, data };
35
+ } else if (typeof data === "object") {
36
+ return { ...result, ...data };
37
+ }
38
+ return result;
39
+ };
40
+
41
+ const send = (type) => {
42
+ return function (err) {
43
+ let data = {};
44
+ if (typeof err === "string") {
45
+ data.msg = err;
46
+ } else if (typeof err === "object") {
47
+ data = { ...err };
48
+ }
49
+ const isExcludeMsg = Object.keys(data).find((key) => {
50
+ return exclude.find((i) => new RegExp(i, "g").test(data[key]));
51
+ });
52
+ if (isExcludeMsg) {
53
+ console.log(data);
54
+ return;
55
+ }
56
+ const result = formatLogData(data, type);
57
+ if (!opts.host || !opts.project || !opts.logstore) {
58
+ return console.warn("日志初始化需要配置options", result);
59
+ }
60
+ tracker.send(formatDataMaxLength(result));
61
+ };
62
+ };
63
+
64
+ const error = send("error");
65
+ const warn = send("warn");
66
+ const log = send("info");
67
+ const info = send("info");
68
+
69
+ const logInit = (Tracker) => {
70
+ return (config) => {
71
+ const {
72
+ defaultData = {},
73
+ defaultExclude = [],
74
+ options = {},
75
+ strMaxLength = 200,
76
+ } = config;
77
+
78
+ Object.assign(defaultLogData, defaultData);
79
+ Object.assign(opts, options);
80
+ if (options.host && options.logstore && options.project) {
81
+ tracker = new Tracker(opts);
82
+ }
83
+ maxLength = strMaxLength;
84
+ exclude.push(...defaultExclude);
85
+ };
86
+ };
87
+
88
+ export default {
89
+ error,
90
+ warn,
91
+ log,
92
+ info,
93
+ logInit,
94
+ };
package/lib/mini.d.ts CHANGED
@@ -12,10 +12,10 @@ interface initConfig {
12
12
  options?: Options;
13
13
  }
14
14
 
15
- export = slsLog;
16
- export as namespace slsLog;
15
+ export = miniLog;
16
+ export as namespace miniLog;
17
17
 
18
- declare namespace slsLog {
18
+ declare namespace miniLog {
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/lib/mini.js CHANGED
@@ -1,69 +1,12 @@
1
1
  import SlsMiniTracker from "@aliyun-sls/web-track-mini";
2
+ import { error, warn, log, info, logInit } from "./init";
2
3
 
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");
4
+ const init = logInit(SlsMiniTracker);
54
5
 
55
6
  export default {
56
7
  error,
57
8
  warn,
58
9
  log,
59
10
  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
- },
11
+ init,
69
12
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "send-sls-logger",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "阿里云sls的logger发送",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"