@wutiange/log-listener-plugin 1.0.1 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ import '../console';
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ require("../console");
7
+ const index_1 = __importDefault(require("../index"));
8
+ describe("重写日志", () => {
9
+ it("log 没有提前设置 url", () => {
10
+ console.log("log 是否正常打印");
11
+ expect(1).toBe(1);
12
+ });
13
+ it("log 提前设置了 url", () => {
14
+ index_1.default.setBaseUrl("http://192.168.118.103");
15
+ console.log("log 是否正常打印");
16
+ expect(1).toBe(1);
17
+ });
18
+ it('warn', () => {
19
+ index_1.default.setBaseUrl("http://192.168.118.103");
20
+ console.warn("warn 是否正常打印");
21
+ expect(1).toBe(1);
22
+ });
23
+ it('error', () => {
24
+ index_1.default.setBaseUrl("http://192.168.118.103");
25
+ console.error("error 是否正常打印");
26
+ expect(1).toBe(1);
27
+ });
28
+ });
29
+ //# sourceMappingURL=console.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.test.js","sourceRoot":"","sources":["../../src/__tests__/console.test.ts"],"names":[],"mappings":";;;;;AAAA,sBAAmB;AACnB,qDAA6B;AAC7B,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;QACxB,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;QACzB,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,eAAe,EAAE,GAAG,EAAE;QACvB,eAAM,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAA;QAC3C,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;QACzB,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE;QACd,eAAM,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAA;QAC3C,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;QAC3B,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;QACf,eAAM,CAAC,UAAU,CAAC,wBAAwB,CAAC,CAAA;QAC3C,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAA;QAC7B,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -0,0 +1,11 @@
1
+ export declare const log: {
2
+ (...data: any[]): void;
3
+ (message?: any, ...optionalParams: any[]): void;
4
+ }, warn: {
5
+ (...data: any[]): void;
6
+ (message?: any, ...optionalParams: any[]): void;
7
+ }, error: {
8
+ (...data: any[]): void;
9
+ (message?: any, ...optionalParams: any[]): void;
10
+ };
11
+ export declare const tempFetch: typeof fetch;
package/dist/common.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.tempFetch = exports.error = exports.warn = exports.log = void 0;
5
+ _a = [console.log, console.warn, console.error], exports.log = _a[0], exports.warn = _a[1], exports.error = _a[2];
6
+ exports.tempFetch = global.fetch;
7
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";;;;AAAa,KAAqB,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,EAA9D,WAAG,UAAE,YAAI,UAAE,aAAK,SAA8C;AAI/D,QAAA,SAAS,GAAG,MAAM,CAAC,KAAqB,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const index_1 = __importDefault(require("./index"));
7
+ import('./common.js').then(common => {
8
+ console.log = (...data) => {
9
+ index_1.default.log(...data);
10
+ common.log(...data);
11
+ };
12
+ console.warn = (...data) => {
13
+ index_1.default.warn(...data);
14
+ common.warn(...data);
15
+ };
16
+ console.error = (...data) => {
17
+ index_1.default.error(...data);
18
+ common.error(...data);
19
+ };
20
+ });
21
+ //# sourceMappingURL=console.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.js","sourceRoot":"","sources":["../src/console.ts"],"names":[],"mappings":";;;;;AAAA,oDAA4B;AAE5B,MAAM,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;IAClC,OAAO,CAAC,GAAG,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;QAC/B,eAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;QACnB,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;IACrB,CAAC,CAAA;IAED,OAAO,CAAC,IAAI,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;QAChC,eAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;QACpB,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;IACtB,CAAC,CAAA;IAED,OAAO,CAAC,KAAK,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;QACjC,eAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA;QACrB,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA;IACvB,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
package/dist/fetch.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const index_1 = __importDefault(require("./index"));
16
+ const utils_1 = require("./utils");
17
+ import('./common.js').then(common => {
18
+ global.fetch = (input, init) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const uniqueId = (Date.now() + Math.random()).toString(16);
20
+ index_1.default.uniqueReq(uniqueId, input, init);
21
+ let isFetchFinished = false;
22
+ let isTimeout = false;
23
+ if (index_1.default.getTimeout() !== null) {
24
+ (0, utils_1.sleep)(index_1.default.getTimeout()).then(() => {
25
+ if (!isFetchFinished) {
26
+ isTimeout = true;
27
+ index_1.default.resTimeout(uniqueId);
28
+ }
29
+ });
30
+ }
31
+ try {
32
+ const response = yield common.tempFetch(input, init);
33
+ isFetchFinished = true;
34
+ if (response instanceof Response && !isTimeout) {
35
+ index_1.default.uniqueRes(uniqueId, response.clone());
36
+ }
37
+ return response;
38
+ }
39
+ catch (error) {
40
+ isFetchFinished = true;
41
+ if (!isTimeout) {
42
+ index_1.default.resResponseError(uniqueId);
43
+ }
44
+ throw new Error(error);
45
+ }
46
+ });
47
+ });
48
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1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,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,eAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAA;YACnC,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;QACxB,CAAC;IACH,CAAC,CAAA,CAAA;AACH,CAAC,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,16 +1,23 @@
1
1
  declare class Logger {
2
2
  private server;
3
3
  private baseData;
4
+ private timeout;
4
5
  setBaseUrl(url: string): void;
5
- setBaseData(data: Record<string, any>): void;
6
- private throwNotSetBaseUrl;
7
- private throwNotSetBaseData;
6
+ setTimeout(timeout: number): void;
7
+ getTimeout(): number;
8
+ setBaseData(data?: Record<string, any>): void;
8
9
  private _log;
10
+ tag(tag: string, ...data: any[]): void;
9
11
  log(...data: any[]): void;
10
12
  warn(...data: any[]): void;
11
13
  error(...data: any[]): void;
14
+ uniqueReq(uniqueId: string, input: RequestInfo | URL, init?: RequestInit): Promise<string>;
15
+ private _res;
16
+ resTimeout(uniqueId: string): Promise<string>;
17
+ resResponseError(uniqueId: string): Promise<string>;
18
+ uniqueRes(uniqueId: string, response?: Response): Promise<string>;
12
19
  req(input: RequestInfo | URL, init?: RequestInit): Promise<string>;
13
- res(id: number, response?: Response, isTimeout?: boolean): Promise<string>;
20
+ res(id: number, response?: Response): Promise<string>;
14
21
  }
15
22
  declare const _default: Logger;
16
23
  export default _default;
package/dist/index.js CHANGED
@@ -16,6 +16,8 @@ const server_1 = __importDefault(require("./server"));
16
16
  class Logger {
17
17
  constructor() {
18
18
  this.server = null;
19
+ this.baseData = {};
20
+ this.timeout = null;
19
21
  }
20
22
  setBaseUrl(url) {
21
23
  if (this.server) {
@@ -25,44 +27,77 @@ class Logger {
25
27
  this.server = new server_1.default(url);
26
28
  }
27
29
  }
28
- setBaseData(data) {
29
- this.baseData = data;
30
- }
31
- throwNotSetBaseUrl() {
32
- if (!this.server) {
33
- throw new Error("请先设置 baseUrl ,用于将数据上传到日志系统中,一般是日志系统打开所在的电脑端口号");
30
+ setTimeout(timeout) {
31
+ var _a;
32
+ if (typeof timeout === 'number') {
33
+ this.timeout = timeout;
34
+ (_a = this.server) === null || _a === void 0 ? void 0 : _a.updateTimeout(this.timeout);
34
35
  }
35
36
  }
36
- throwNotSetBaseData() {
37
- if (!this.server) {
38
- throw new Error("请先设置基础数据,也就是每一条日志都包含的数据");
37
+ getTimeout() {
38
+ if (typeof this.timeout === 'number') {
39
+ return this.timeout;
39
40
  }
41
+ return null;
42
+ }
43
+ setBaseData(data = {}) {
44
+ this.baseData = data;
45
+ }
46
+ _log(level, tag, ...data) {
47
+ var _a;
48
+ const sendData = Object.assign(Object.assign({}, this.baseData), { message: data, tag, level: level !== null && level !== void 0 ? level : 'log', createTime: Date.now() });
49
+ (_a = this.server) === null || _a === void 0 ? void 0 : _a.log(sendData);
40
50
  }
41
- _log(level, ...data) {
42
- this.throwNotSetBaseUrl();
43
- this.throwNotSetBaseData();
44
- const sendData = Object.assign(Object.assign({}, this.baseData), { message: data, level: level !== null && level !== void 0 ? level : 'log', createTime: Date.now() });
45
- this.server.log(sendData);
51
+ tag(tag, ...data) {
52
+ this._log('log', tag, ...data);
46
53
  }
47
54
  log(...data) {
48
- this._log('log', ...data);
55
+ this._log('log', 'default', ...data);
49
56
  }
50
57
  warn(...data) {
51
- this._log('warn', ...data);
58
+ this._log('warn', 'default', ...data);
52
59
  }
53
60
  error(...data) {
54
- this._log('error', ...data);
61
+ this._log('error', 'default', ...data);
55
62
  }
56
- req(input, init) {
57
- var _a;
63
+ uniqueReq(uniqueId, input, init) {
64
+ var _a, _b;
58
65
  return __awaiter(this, void 0, void 0, function* () {
59
- return this.server.network(Object.assign(Object.assign({}, this.baseData), { url: input, method: (_a = init === null || init === void 0 ? void 0 : init.method) !== null && _a !== void 0 ? _a : "get", headers: init === null || init === void 0 ? void 0 : init.headers, body: init === null || init === void 0 ? void 0 : init.body, createTime: Date.now() }));
66
+ return (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { url: input, id: uniqueId, method: (_b = init === null || init === void 0 ? void 0 : init.method) !== null && _b !== void 0 ? _b : "get", headers: init === null || init === void 0 ? void 0 : init.headers, body: init === null || init === void 0 ? void 0 : init.body, createTime: Date.now() }));
60
67
  });
61
68
  }
62
- res(id, response, isTimeout = false) {
69
+ _res(uniqueId, id, response) {
70
+ var _a;
63
71
  return __awaiter(this, void 0, void 0, function* () {
64
72
  const body = yield response.text();
65
- return this.server.network(Object.assign(Object.assign({}, this.baseData), { headers: (response === null || response === void 0 ? void 0 : response.headers)['map'], body, requestId: Number(id), statusCode: response === null || response === void 0 ? void 0 : response.status, endTime: Date.now(), isTimeout, isResponseError: id === undefined || id === null }));
73
+ return (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { headers: (response === null || response === void 0 ? void 0 : response.headers)['map'], body, requestId: uniqueId !== null && uniqueId !== void 0 ? uniqueId : Number(id), statusCode: response === null || response === void 0 ? void 0 : response.status, endTime: Date.now() }));
74
+ });
75
+ }
76
+ resTimeout(uniqueId) {
77
+ var _a;
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ return (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { isTimeout: true, requestId: uniqueId }));
80
+ });
81
+ }
82
+ resResponseError(uniqueId) {
83
+ var _a;
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ return (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { isResponseError: true, requestId: uniqueId }));
86
+ });
87
+ }
88
+ uniqueRes(uniqueId, response) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ return this._res(uniqueId, undefined, response);
91
+ });
92
+ }
93
+ req(input, init) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ return this.uniqueReq(undefined, input, init);
96
+ });
97
+ }
98
+ res(id, response) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ return this._res(undefined, id, response);
66
101
  });
67
102
  }
68
103
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA6B;AAG7B,MAAM,MAAM;IAAZ;QAEU,WAAM,GAAkB,IAAI,CAAA;IA2EtC,CAAC;IAxEC,UAAU,CAAC,GAAW;QACpB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;QAC5B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,GAAG,CAAC,CAAA;QAC/B,CAAC;IACH,CAAC;IAED,WAAW,CAAC,IAAyB;QACnC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;IACtB,CAAC;IAEO,kBAAkB;QACxB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;QAClE,CAAC;IACH,CAAC;IAEO,mBAAmB;QACzB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;QAC5C,CAAC;IACH,CAAC;IAEO,IAAI,CAAC,KAAa,EAAE,GAAG,IAAW;QACxC,IAAI,CAAC,kBAAkB,EAAE,CAAA;QACzB,IAAI,CAAC,mBAAmB,EAAE,CAAA;QAC1B,MAAM,QAAQ,mCACT,IAAI,CAAC,QAAQ,KAChB,OAAO,EAAE,IAAI,EACb,KAAK,EAAE,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,KAAK,EACrB,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GACvB,CAAA;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC3B,CAAC;I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
4
+ private timeout;
5
+ constructor(url: string, timeout?: number);
6
+ updateTimeout(timeout?: number): void;
5
7
  private send;
6
8
  updateUrl(url: string): void;
7
9
  log(data: Record<string, any>): Promise<string>;
package/dist/server.js CHANGED
@@ -9,23 +9,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ const utils_1 = require("./utils");
12
13
  class Server {
13
- constructor(url) {
14
+ constructor(url, timeout = 3000) {
14
15
  this.baseUrl = '';
15
16
  this.port = 27751;
16
17
  this.updateUrl(url);
18
+ this.timeout = timeout;
19
+ }
20
+ updateTimeout(timeout = 3000) {
21
+ this.timeout = timeout;
17
22
  }
18
23
  send(path, data) {
19
24
  return __awaiter(this, void 0, void 0, function* () {
20
25
  try {
21
- const result = yield fetch(`${this.baseUrl}:${this.port}/${path}`, {
22
- method: 'POST',
23
- headers: {
24
- 'Content-Type': 'application/json;charset=utf-8'
25
- },
26
- body: JSON.stringify(data)
27
- });
28
- return result.text();
26
+ if (!this.baseUrl) {
27
+ return null;
28
+ }
29
+ const common = yield import('./common.js');
30
+ const result = yield Promise.race([
31
+ common.tempFetch(`${this.baseUrl}:${this.port}/${path}`, {
32
+ method: 'POST',
33
+ headers: {
34
+ 'Content-Type': 'application/json;charset=utf-8'
35
+ },
36
+ body: JSON.stringify(data)
37
+ }),
38
+ (0, utils_1.sleep)(this.timeout, true)
39
+ ]);
40
+ if (result instanceof Response) {
41
+ return result.text();
42
+ }
43
+ return null;
29
44
  }
30
45
  catch (error) {
31
46
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,MAAM,MAAM;IAGV,YAAY,GAAW;QAFf,YAAO,GAAG,EAAE,CAAC;QACb,SAAI,GAAG,KAAK,CAAA;QAElB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IACrB,CA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D,IAAI,CAAC;gBACH,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;oBAClB,OAAO,IAAI,CAAA;gBACb,CAAC;gBACD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAA;gBAC1C,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC;oBAChC,MAAM,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,EAAE,EAAE;wBACvD,MAAM,EAAE,MAAM;wBACd,OAAO,EAAE;4BACP,cAAc,EAAE,gCAAgC;yBACjD;wBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;qBAC3B,CAAC;oBACF,IAAA,aAAK,EAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC;iBAC1B,CAAC,CAAA;gBACF,IAAI,MAAM,YAAY,QAAQ,EAAE,CAAC;oBAC/B,OAAO,MAAM,CAAC,IAAI,EAAE,CAAA;gBACtB,CAAC;gBACD,OAAO,IAAI,CAAA;YACb,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;KAAA;IAED,SAAS,CAAC,GAAW;QACnB,IAAI,CAAC,OAAO,GAAG,GAAG,CAAA;IACpB,CAAC;IAEK,GAAG,CAAC,IAAyB;;YACjC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QAC/B,CAAC;KAAA;IAEK,OAAO,CAAC,IAAyB;;YACrC,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;QACnC,CAAC;KAAA;CACF;AAED,kBAAe,MAAM,CAAA"}
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number, isReject?: boolean): Promise<unknown>;
package/dist/utils.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sleep = void 0;
4
+ function sleep(ms, isReject = false) {
5
+ return new Promise((resolve, reject) => {
6
+ setTimeout(isReject ? () => reject({
7
+ code: 11001,
8
+ key: '@wutiange/log-listener-plugin%%timeout',
9
+ msg: 'Timeout'
10
+ }) : resolve, ms);
11
+ });
12
+ }
13
+ exports.sleep = sleep;
14
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,KAAK,CAAC,EAAU,EAAE,WAAoB,KAAK;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;YACjC,IAAI,EAAE,KAAK;YACX,GAAG,EAAE,wCAAwC;YAC7C,GAAG,EAAE,SAAS;SACf,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;AACJ,CAAC;AARD,sBAQC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wutiange/log-listener-plugin",
3
- "version": "1.0.1",
3
+ "version": "1.1.10",
4
4
  "description": "这是log-record的插件,如果需要使用这个日志监听,那么就需要把这个插件安装到项目中",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,10 +10,22 @@
10
10
  "private": false,
11
11
  "scripts": {
12
12
  "build": "tsc",
13
- "dev": "ts-node ./src/index.ts"
13
+ "dev": "ts-node ./src/index.ts",
14
+ "test": "jest"
14
15
  },
15
16
  "devDependencies": {
17
+ "@jest/globals": "^29.7.0",
18
+ "@types/jest": "^29.5.12",
19
+ "jest": "^29.7.0",
20
+ "ts-jest": "^29.1.2",
16
21
  "ts-node": "^10.9.2",
17
22
  "typescript": "^5.3.3"
18
- }
23
+ },
24
+ "files": [
25
+ "src",
26
+ "package.json",
27
+ "README.md",
28
+ "LICENSE",
29
+ "dist"
30
+ ]
19
31
  }
@@ -0,0 +1,26 @@
1
+ import '../console'
2
+ import logger from '../index'
3
+ describe("重写日志", () => {
4
+ it("log 没有提前设置 url", () => {
5
+ console.log("log 是否正常打印")
6
+ expect(1).toBe(1)
7
+ })
8
+
9
+ it("log 提前设置了 url", () => {
10
+ logger.setBaseUrl("http://192.168.118.103")
11
+ console.log("log 是否正常打印")
12
+ expect(1).toBe(1)
13
+ })
14
+
15
+ it('warn', () => {
16
+ logger.setBaseUrl("http://192.168.118.103")
17
+ console.warn("warn 是否正常打印")
18
+ expect(1).toBe(1)
19
+ })
20
+
21
+ it('error', () => {
22
+ logger.setBaseUrl("http://192.168.118.103")
23
+ console.error("error 是否正常打印")
24
+ expect(1).toBe(1)
25
+ })
26
+ })
package/src/common.ts ADDED
@@ -0,0 +1,5 @@
1
+ export const [log, warn, error] = [console.log, console.warn, console.error]
2
+
3
+
4
+ // @ts-ignore
5
+ export const tempFetch = global.fetch as typeof fetch
package/src/console.ts ADDED
@@ -0,0 +1,19 @@
1
+ import logger from './index'
2
+
3
+ import('./common.js').then(common => {
4
+ console.log = (...data: any[]) => {
5
+ logger.log(...data)
6
+ common.log(...data)
7
+ }
8
+
9
+ console.warn = (...data: any[]) => {
10
+ logger.warn(...data)
11
+ common.warn(...data)
12
+ }
13
+
14
+ console.error = (...data: any[]) => {
15
+ logger.error(...data)
16
+ common.error(...data)
17
+ }
18
+ })
19
+
package/src/fetch.ts ADDED
@@ -0,0 +1,35 @@
1
+ import logger from './index'
2
+ import { sleep } from './utils'
3
+
4
+ import('./common.js').then(common => {
5
+ // @ts-ignore
6
+ global.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
7
+ const uniqueId = (Date.now() + Math.random()).toString(16)
8
+ logger.uniqueReq(uniqueId, input, init)
9
+ let isFetchFinished = false
10
+ let isTimeout = false
11
+ if (logger.getTimeout() !== null) {
12
+ sleep(logger.getTimeout()).then(() => {
13
+ if (!isFetchFinished) {
14
+ isTimeout = true
15
+ logger.resTimeout(uniqueId)
16
+ }
17
+ })
18
+ }
19
+
20
+ try {
21
+ const response = await common.tempFetch(input, init)
22
+ isFetchFinished = true
23
+ if (response instanceof Response && !isTimeout) {
24
+ logger.uniqueRes(uniqueId, response.clone())
25
+ }
26
+ return response
27
+ } catch (error) {
28
+ isFetchFinished = true
29
+ if (!isTimeout) {
30
+ logger.resResponseError(uniqueId)
31
+ }
32
+ throw new Error(error)
33
+ }
34
+ }
35
+ })
package/src/index.ts CHANGED
@@ -4,7 +4,8 @@ import Server from "./server"
4
4
  class Logger {
5
5
 
6
6
  private server: Server | null = null
7
- private baseData: Record<string, any>
7
+ private baseData: Record<string, any> = {}
8
+ private timeout: number | null = null
8
9
 
9
10
  setBaseUrl(url: string) {
10
11
  if (this.server) {
@@ -14,50 +15,56 @@ class Logger {
14
15
  }
15
16
  }
16
17
 
17
- setBaseData(data: Record<string, any>) {
18
- this.baseData = data
18
+ setTimeout(timeout: number) {
19
+ if (typeof timeout === 'number') {
20
+ this.timeout = timeout
21
+ this.server?.updateTimeout(this.timeout)
22
+ }
19
23
  }
20
24
 
21
- private throwNotSetBaseUrl() {
22
- if (!this.server) {
23
- throw new Error("请先设置 baseUrl ,用于将数据上传到日志系统中,一般是日志系统打开所在的电脑端口号")
25
+ getTimeout() {
26
+ if (typeof this.timeout === 'number') {
27
+ return this.timeout
24
28
  }
29
+ return null
25
30
  }
26
31
 
27
- private throwNotSetBaseData() {
28
- if (!this.server) {
29
- throw new Error("请先设置基础数据,也就是每一条日志都包含的数据")
30
- }
32
+ setBaseData(data: Record<string, any> = {}) {
33
+ this.baseData = data
31
34
  }
32
35
 
33
- private _log(level: string, ...data: any[]) {
34
- this.throwNotSetBaseUrl()
35
- this.throwNotSetBaseData()
36
+ private _log(level: string, tag: string, ...data: any[]) {
36
37
  const sendData = {
37
38
  ...this.baseData,
38
39
  message: data,
40
+ tag,
39
41
  level: level ?? 'log',
40
42
  createTime: Date.now()
41
43
  }
42
- this.server.log(sendData)
44
+ this.server?.log(sendData)
45
+ }
46
+
47
+ tag(tag: string, ...data: any[]) {
48
+ this._log('log', tag, ...data)
43
49
  }
44
50
 
45
51
  log(...data: any[]) {
46
- this._log('log', ...data)
52
+ this._log('log', 'default', ...data)
47
53
  }
48
54
 
49
55
  warn(...data: any[]) {
50
- this._log('warn', ...data)
56
+ this._log('warn', 'default', ...data)
51
57
  }
52
58
 
53
59
  error(...data: any[]) {
54
- this._log('error', ...data)
60
+ this._log('error', 'default', ...data)
55
61
  }
56
62
 
57
- async req(input: RequestInfo | URL, init?: RequestInit) {
58
- return this.server.network({
63
+ async uniqueReq(uniqueId: string, input: RequestInfo | URL, init?: RequestInit) {
64
+ return this.server?.network({
59
65
  ...this.baseData,
60
66
  url: input,
67
+ id: uniqueId,
61
68
  method: init?.method ?? "get",
62
69
  headers: init?.headers,
63
70
  body: init?.body,
@@ -65,19 +72,45 @@ class Logger {
65
72
  })
66
73
  }
67
74
 
68
- async res(id: number, response?: Response, isTimeout = false) {
75
+ private async _res(uniqueId?: string, id?: number, response?: Response) {
69
76
  const body = await response.text()
70
- return this.server.network({
77
+ return this.server?.network({
71
78
  ...this.baseData,
72
79
  headers: (response?.headers as Record<string, any>)['map'],
73
80
  body,
74
- requestId: Number(id),
81
+ requestId: uniqueId ?? Number(id),
75
82
  statusCode: response?.status,
76
83
  endTime: Date.now(),
77
- isTimeout,
78
- isResponseError: id === undefined || id === null
79
84
  })
80
85
  }
86
+
87
+ async resTimeout(uniqueId: string) {
88
+ return this.server?.network({
89
+ ...this.baseData,
90
+ isTimeout: true,
91
+ requestId: uniqueId,
92
+ })
93
+ }
94
+
95
+ async resResponseError(uniqueId: string) {
96
+ return this.server?.network({
97
+ ...this.baseData,
98
+ isResponseError: true,
99
+ requestId: uniqueId,
100
+ })
101
+ }
102
+
103
+ async uniqueRes(uniqueId: string, response?: Response) {
104
+ return this._res(uniqueId, undefined, response)
105
+ }
106
+
107
+ async req(input: RequestInfo | URL, init?: RequestInit) {
108
+ return this.uniqueReq(undefined, input, init)
109
+ }
110
+
111
+ async res(id: number, response?: Response) {
112
+ return this._res(undefined, id, response)
113
+ }
81
114
  }
82
115
 
83
116
 
package/src/server.ts CHANGED
@@ -1,20 +1,40 @@
1
+ import { sleep } from "./utils";
2
+
1
3
  class Server {
2
4
  private baseUrl = '';
3
5
  private port = 27751
4
- constructor(url: string) {
6
+ private timeout: number;
7
+
8
+
9
+ constructor(url: string, timeout: number = 3000) {
5
10
  this.updateUrl(url)
11
+ this.timeout = timeout
12
+ }
13
+
14
+ updateTimeout(timeout = 3000) {
15
+ this.timeout = timeout
6
16
  }
7
17
 
8
18
  private async send(path: string, data: Record<string, any>) {
9
19
  try {
10
- const result = await fetch(`${this.baseUrl}:${this.port}/${path}`, {
11
- method: 'POST',
12
- headers: {
13
- 'Content-Type': 'application/json;charset=utf-8'
14
- },
15
- body: JSON.stringify(data)
16
- })
17
- return result.text()
20
+ if (!this.baseUrl) {
21
+ return null
22
+ }
23
+ const common = await import('./common.js')
24
+ const result = await Promise.race([
25
+ common.tempFetch(`${this.baseUrl}:${this.port}/${path}`, {
26
+ method: 'POST',
27
+ headers: {
28
+ 'Content-Type': 'application/json;charset=utf-8'
29
+ },
30
+ body: JSON.stringify(data)
31
+ }),
32
+ sleep(this.timeout, true)
33
+ ])
34
+ if (result instanceof Response) {
35
+ return result.text()
36
+ }
37
+ return null
18
38
  } catch (error) {
19
39
  return null
20
40
  }
package/src/utils.ts ADDED
@@ -0,0 +1,9 @@
1
+ export function sleep(ms: number, isReject: boolean = false) {
2
+ return new Promise((resolve, reject) => {
3
+ setTimeout(isReject ? () => reject({
4
+ code: 11001,
5
+ key: '@wutiange/log-listener-plugin%%timeout',
6
+ msg: 'Timeout'
7
+ }) : resolve, ms)
8
+ })
9
+ }
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "declaration": true,
5
- "noImplicitAny": true,
6
- "removeComments": true,
7
- "preserveConstEnums": true,
8
- "outDir": "dist",
9
- "sourceMap": true,
10
- "esModuleInterop": true,
11
- "target": "es6"
12
- },
13
- "exclude": ["node_modules", "**/*.spec.ts"],
14
- "include": ["src/**/*"]
15
- }