lupine.api 1.1.58 → 1.1.59
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/README.md +3 -3
- package/admin/admin-about.tsx +12 -16
- package/admin/admin-config.tsx +47 -44
- package/admin/admin-css.tsx +3 -3
- package/admin/admin-db.tsx +75 -75
- package/admin/admin-frame-helper.tsx +364 -364
- package/admin/admin-frame.tsx +164 -164
- package/admin/admin-index.tsx +65 -65
- package/admin/admin-login.tsx +111 -111
- package/admin/admin-menu-edit.tsx +637 -637
- package/admin/admin-menu-list.tsx +87 -87
- package/admin/admin-page-edit.tsx +564 -564
- package/admin/admin-page-list.tsx +83 -83
- package/admin/admin-performance.tsx +28 -28
- package/admin/admin-release.tsx +427 -426
- package/admin/admin-resources.tsx +382 -382
- package/admin/admin-shell.tsx +89 -89
- package/admin/admin-table-data.tsx +146 -146
- package/admin/admin-table-list.tsx +230 -230
- package/admin/admin-test-animations.tsx +395 -395
- package/admin/admin-test-component.tsx +823 -808
- package/admin/admin-test-edit.tsx +319 -319
- package/admin/admin-test-themes.tsx +56 -56
- package/admin/admin-tokens.tsx +338 -338
- package/admin/design/admin-design.tsx +174 -174
- package/admin/design/block-grid.tsx +36 -36
- package/admin/design/block-grid1.tsx +21 -21
- package/admin/design/block-paragraph.tsx +19 -19
- package/admin/design/block-title.tsx +19 -19
- package/admin/design/design-block-box.tsx +140 -140
- package/admin/design/drag-data.tsx +24 -24
- package/admin/index.ts +9 -9
- package/admin/package.json +15 -15
- package/admin/tsconfig.json +127 -127
- package/dev/copy-folder.js +32 -32
- package/dev/cp-index-html.js +69 -69
- package/dev/file-utils.js +12 -12
- package/dev/index.js +18 -19
- package/dev/package.json +12 -12
- package/dev/plugin-ifelse.js +168 -168
- package/dev/plugin-ifelse.test.js +37 -37
- package/dev/run-cmd.js +14 -14
- package/dev/send-request.js +12 -12
- package/package.json +55 -55
- package/src/admin-api/admin-api-helper.ts +210 -205
- package/src/admin-api/admin-api.ts +65 -65
- package/src/admin-api/admin-auth.ts +152 -146
- package/src/admin-api/admin-config.ts +94 -84
- package/src/admin-api/admin-csv.ts +94 -94
- package/src/admin-api/admin-db.ts +269 -269
- package/src/admin-api/admin-menu.ts +135 -135
- package/src/admin-api/admin-page.ts +135 -135
- package/src/admin-api/admin-performance.ts +128 -128
- package/src/admin-api/admin-release.ts +703 -700
- package/src/admin-api/admin-resources.ts +318 -318
- package/src/admin-api/admin-token-helper.ts +82 -79
- package/src/admin-api/admin-tokens.ts +90 -90
- package/src/admin-api/index.ts +2 -2
- package/src/admin-api/web-config-api.ts +19 -19
- package/src/api/api-cache.ts +103 -103
- package/src/api/api-helper.ts +44 -44
- package/src/api/api-module.ts +67 -60
- package/src/api/api-router.ts +177 -177
- package/src/api/api-shared-storage.ts +64 -64
- package/src/api/async-storage.ts +5 -5
- package/src/api/debug-service.ts +56 -56
- package/src/api/encode-html.ts +27 -27
- package/src/api/handle-status.ts +75 -75
- package/src/api/index.ts +15 -16
- package/src/api/mini-web-socket.ts +270 -270
- package/src/api/server-content-type.ts +82 -82
- package/src/api/server-render.ts +235 -215
- package/src/api/shell-service.ts +74 -74
- package/src/api/simple-storage.ts +80 -80
- package/src/api/static-server.ts +128 -125
- package/src/api/to-client-delivery.ts +26 -26
- package/src/app/app-cache.ts +55 -55
- package/src/app/app-helper.ts +62 -62
- package/src/app/app-message.ts +109 -109
- package/src/app/app-shared-storage.ts +363 -363
- package/src/app/app-start.ts +136 -136
- package/src/app/cleanup-exit.ts +16 -16
- package/src/app/host-to-path.ts +38 -38
- package/src/app/index.ts +11 -11
- package/src/app/process-dev-requests.ts +130 -130
- package/src/app/web-listener.ts +294 -294
- package/src/app/web-processor.ts +47 -42
- package/src/app/web-server.ts +100 -100
- package/src/common-js/web-env.js +104 -104
- package/src/index.ts +7 -7
- package/src/lang/api-lang-en.ts +26 -26
- package/src/lang/api-lang-zh-cn.ts +27 -27
- package/src/lang/index.ts +2 -2
- package/src/lang/lang-helper.ts +76 -76
- package/src/lang/lang-props.ts +6 -6
- package/src/lib/db/db-helper.ts +23 -23
- package/src/lib/db/db-mysql.ts +249 -250
- package/src/lib/db/db-sqlite.ts +101 -101
- package/src/lib/db/db.spec.ts +28 -28
- package/src/lib/db/db.ts +325 -325
- package/src/lib/db/index.ts +5 -5
- package/src/lib/index.ts +3 -3
- package/src/lib/logger.spec.ts +214 -214
- package/src/lib/logger.ts +281 -281
- package/src/lib/runtime-require.ts +37 -37
- package/src/lib/utils/cookie-util.ts +34 -34
- package/src/lib/utils/crypto.ts +58 -58
- package/src/lib/utils/date-utils.ts +317 -317
- package/src/lib/utils/deep-merge.ts +37 -37
- package/src/lib/utils/delay.ts +12 -12
- package/src/lib/utils/file-setting.ts +55 -55
- package/src/lib/utils/format-bytes.ts +11 -11
- package/src/lib/utils/fs-utils.ts +158 -158
- package/src/lib/utils/get-env.ts +27 -27
- package/src/lib/utils/index.ts +12 -12
- package/src/lib/utils/is-type.ts +48 -48
- package/src/lib/utils/load-env.ts +14 -14
- package/src/lib/utils/pad.ts +6 -6
- package/src/models/api-base.ts +5 -5
- package/src/models/api-module-props.ts +10 -11
- package/src/models/api-router-props.ts +26 -26
- package/src/models/app-cache-props.ts +33 -33
- package/src/models/app-data-props.ts +10 -10
- package/src/models/app-helper-props.ts +6 -6
- package/src/models/app-shared-storage-props.ts +38 -38
- package/src/models/app-start-props.ts +18 -18
- package/src/models/async-storage-props.ts +13 -13
- package/src/models/db-config.ts +30 -30
- package/src/models/host-to-path-props.ts +12 -12
- package/src/models/index.ts +16 -16
- package/src/models/json-object.ts +8 -8
- package/src/models/locals-props.ts +36 -36
- package/src/models/logger-props.ts +84 -84
- package/src/models/simple-storage-props.ts +13 -14
- package/src/models/to-client-delivery-props.ts +6 -6
- package/tsconfig.json +115 -115
- package/dev/plugin-gen-versions.js +0 -20
package/src/lib/logger.spec.ts
CHANGED
|
@@ -1,214 +1,214 @@
|
|
|
1
|
-
import { Logger, LogLevels } from './logger';
|
|
2
|
-
const cluster = require('cluster');
|
|
3
|
-
const vm = require('vm');
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
jest.mock('fs');
|
|
7
|
-
jest.mock('cluster');
|
|
8
|
-
|
|
9
|
-
describe('Test logger', () => {
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
jest.resetAllMocks();
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('test init without recreating a file', async () => {
|
|
15
|
-
fs.existsSync.mockReturnValue(false);
|
|
16
|
-
fs.openSync.mockReturnValue(1);
|
|
17
|
-
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
18
|
-
|
|
19
|
-
const logger = new Logger('test1');
|
|
20
|
-
logger.init({
|
|
21
|
-
folder: './log',
|
|
22
|
-
filename: 'log%index%.log',
|
|
23
|
-
maxSize: 1024,
|
|
24
|
-
maxCount: 5,
|
|
25
|
-
outToFile: true,
|
|
26
|
-
outToConsole: true,
|
|
27
|
-
level: LogLevels.debug,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
expect(fs.existsSync).toHaveBeenCalled();
|
|
31
|
-
expect(fs.openSync).toHaveBeenCalled();
|
|
32
|
-
expect(fs.fstatSync).toHaveBeenCalled();
|
|
33
|
-
expect(fs.existsSync.mock.calls[0][0]).toBe('./log');
|
|
34
|
-
expect(fs.mkdirSync.mock.calls[0][0]).toBe('./log');
|
|
35
|
-
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('test init, with recreating log file', async () => {
|
|
39
|
-
fs.existsSync.mockReturnValue(true);
|
|
40
|
-
fs.openSync.mockReturnValue(1);
|
|
41
|
-
fs.fstatSync.mockReturnValue({ size: 1025 });
|
|
42
|
-
|
|
43
|
-
const logger = new Logger('test2');
|
|
44
|
-
logger.init({
|
|
45
|
-
folder: './log',
|
|
46
|
-
filename: 'log%index%.log',
|
|
47
|
-
maxSize: 10,
|
|
48
|
-
maxCount: 5,
|
|
49
|
-
outToFile: true,
|
|
50
|
-
outToConsole: true,
|
|
51
|
-
level: LogLevels.debug,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
logger.debug('a'.repeat(11));
|
|
55
|
-
|
|
56
|
-
expect(fs.closeSync).toHaveBeenCalled();
|
|
57
|
-
expect(fs.openSync).toHaveBeenCalled();
|
|
58
|
-
expect(fs.fstatSync).toHaveBeenCalled();
|
|
59
|
-
expect(fs.closeSync.mock.calls[0][0]).toBe(1);
|
|
60
|
-
|
|
61
|
-
expect(fs.renameSync.mock.calls[0][0]).toBe('log/log3.log');
|
|
62
|
-
expect(fs.renameSync.mock.calls[3][0]).toBe('log/log0.log');
|
|
63
|
-
|
|
64
|
-
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
65
|
-
expect(fs.openSync.mock.calls[1][0]).toBe('log/log0.log');
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('test init, with recreating log file after logs', async () => {
|
|
69
|
-
fs.existsSync.mockReturnValue(true);
|
|
70
|
-
fs.openSync.mockReturnValue(1);
|
|
71
|
-
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
72
|
-
|
|
73
|
-
const logger = new Logger('test2');
|
|
74
|
-
logger.init({
|
|
75
|
-
folder: './log',
|
|
76
|
-
filename: 'log%index%.log',
|
|
77
|
-
maxSize: 10,
|
|
78
|
-
maxCount: 5,
|
|
79
|
-
outToFile: true,
|
|
80
|
-
outToConsole: true,
|
|
81
|
-
level: LogLevels.debug,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
logger.debug('a'.repeat(9));
|
|
85
|
-
|
|
86
|
-
expect(fs.openSync).toHaveBeenCalled();
|
|
87
|
-
expect(fs.fstatSync).toHaveBeenCalled();
|
|
88
|
-
|
|
89
|
-
fs.fstatSync.mockReturnValue({ size: 11 });
|
|
90
|
-
logger.debug('a'.repeat(2));
|
|
91
|
-
|
|
92
|
-
expect(fs.closeSync).toHaveBeenCalled();
|
|
93
|
-
expect(fs.closeSync.mock.calls[1][0]).toBe(1);
|
|
94
|
-
expect(fs.renameSync.mock.calls[0][0]).toBe('log/log3.log');
|
|
95
|
-
expect(fs.renameSync.mock.calls[3][0]).toBe('log/log0.log');
|
|
96
|
-
|
|
97
|
-
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
98
|
-
expect(fs.openSync.mock.calls[1][0]).toBe('log/log0.log');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('test log format', async () => {
|
|
102
|
-
fs.existsSync.mockReturnValue(true);
|
|
103
|
-
fs.openSync.mockReturnValue(1);
|
|
104
|
-
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
105
|
-
|
|
106
|
-
const logger = new Logger('test2');
|
|
107
|
-
logger.init({
|
|
108
|
-
folder: './log',
|
|
109
|
-
filename: 'log%index%.log',
|
|
110
|
-
maxSize: 1024,
|
|
111
|
-
maxCount: 5,
|
|
112
|
-
outToFile: true,
|
|
113
|
-
outToConsole: true,
|
|
114
|
-
level: LogLevels.debug,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
logger.debug(
|
|
118
|
-
'log test. numer: %d, string: %s, json: %j, [%%], Pretty-print: %O',
|
|
119
|
-
123,
|
|
120
|
-
's456',
|
|
121
|
-
{ k: 'key', v: 789 },
|
|
122
|
-
{ k2: 'key2', v2: 901 }
|
|
123
|
-
);
|
|
124
|
-
console.log('test writing log: [' + fs.writeFileSync.mock.calls[0][1].toString() + ']');
|
|
125
|
-
expect(
|
|
126
|
-
fs.writeFileSync.mock.calls[0][1]
|
|
127
|
-
.toString()
|
|
128
|
-
.endsWith(
|
|
129
|
-
`test2: log test. numer: 123, string: s456, json: {"k":"key","v":789}, [%], Pretty-print: { k2: 'key2', v2: 901 }\r\n`
|
|
130
|
-
)
|
|
131
|
-
).toBe(true);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('test different log level', async () => {
|
|
135
|
-
fs.existsSync.mockReturnValue(true);
|
|
136
|
-
fs.openSync.mockReturnValue(1);
|
|
137
|
-
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
138
|
-
|
|
139
|
-
const logger = new Logger('test2');
|
|
140
|
-
logger.init({
|
|
141
|
-
folder: './log',
|
|
142
|
-
filename: 'log%index%.log',
|
|
143
|
-
maxSize: 1024,
|
|
144
|
-
maxCount: 5,
|
|
145
|
-
outToFile: true,
|
|
146
|
-
outToConsole: true,
|
|
147
|
-
level: LogLevels.info,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
logger.debug('a'.repeat(10));
|
|
151
|
-
logger.info('b'.repeat(10));
|
|
152
|
-
logger.warn('c'.repeat(10));
|
|
153
|
-
logger.error('d'.repeat(10));
|
|
154
|
-
logger.fatal('e'.repeat(10));
|
|
155
|
-
console.log('test writing log: [' + fs.writeFileSync.mock.calls[0][1].toString() + ']');
|
|
156
|
-
expect(fs.writeFileSync.mock.calls[0][1].toString().endsWith('test2: bbbbbbbbbb\r\n')).toBe(true);
|
|
157
|
-
expect(fs.writeFileSync.mock.calls[3][1].toString().endsWith('test2: eeeeeeeeee\r\n')).toBe(true);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('test logs in cluster, messages should be sent to Master', async () => {
|
|
161
|
-
fs.existsSync.mockReturnValue(true);
|
|
162
|
-
fs.openSync.mockReturnValue(1);
|
|
163
|
-
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
164
|
-
|
|
165
|
-
const process = require('process');
|
|
166
|
-
const mockSend = jest.spyOn(process, 'send').mockImplementation(() => {});
|
|
167
|
-
|
|
168
|
-
const logger = new Logger('test2');
|
|
169
|
-
logger.init({
|
|
170
|
-
folder: './log',
|
|
171
|
-
filename: 'log%index%.log',
|
|
172
|
-
maxSize: 1024,
|
|
173
|
-
maxCount: 5,
|
|
174
|
-
outToFile: true,
|
|
175
|
-
outToConsole: true,
|
|
176
|
-
level: LogLevels.debug,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// change isPrimary to false, so Logger will send message to Master
|
|
180
|
-
cluster.isPrimary = false;
|
|
181
|
-
logger.debug('isPrimary, %d, %s', 1, '2');
|
|
182
|
-
cluster.isPrimary = true;
|
|
183
|
-
|
|
184
|
-
expect(mockSend).toHaveBeenCalledWith({
|
|
185
|
-
color: 204,
|
|
186
|
-
id: 'LogWriter',
|
|
187
|
-
level: 'DEBUG',
|
|
188
|
-
messageList: ['isPrimary, %d, %s', 1, '2'],
|
|
189
|
-
namespace: 'test2',
|
|
190
|
-
pid: process.pid,
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('test no file output', async () => {
|
|
195
|
-
fs.existsSync.mockReturnValue(true);
|
|
196
|
-
fs.openSync.mockReturnValue(1);
|
|
197
|
-
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
198
|
-
|
|
199
|
-
const logger = new Logger('test2');
|
|
200
|
-
logger.init({
|
|
201
|
-
folder: './log',
|
|
202
|
-
filename: 'log%index%.log',
|
|
203
|
-
maxSize: 1024,
|
|
204
|
-
maxCount: 5,
|
|
205
|
-
outToFile: false,
|
|
206
|
-
outToConsole: true,
|
|
207
|
-
level: LogLevels.debug,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
logger.debug('debug');
|
|
211
|
-
|
|
212
|
-
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
213
|
-
});
|
|
214
|
-
});
|
|
1
|
+
import { Logger, LogLevels } from './logger';
|
|
2
|
+
const cluster = require('cluster');
|
|
3
|
+
const vm = require('vm');
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
jest.mock('fs');
|
|
7
|
+
jest.mock('cluster');
|
|
8
|
+
|
|
9
|
+
describe('Test logger', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
jest.resetAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('test init without recreating a file', async () => {
|
|
15
|
+
fs.existsSync.mockReturnValue(false);
|
|
16
|
+
fs.openSync.mockReturnValue(1);
|
|
17
|
+
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
18
|
+
|
|
19
|
+
const logger = new Logger('test1');
|
|
20
|
+
logger.init({
|
|
21
|
+
folder: './log',
|
|
22
|
+
filename: 'log%index%.log',
|
|
23
|
+
maxSize: 1024,
|
|
24
|
+
maxCount: 5,
|
|
25
|
+
outToFile: true,
|
|
26
|
+
outToConsole: true,
|
|
27
|
+
level: LogLevels.debug,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
expect(fs.existsSync).toHaveBeenCalled();
|
|
31
|
+
expect(fs.openSync).toHaveBeenCalled();
|
|
32
|
+
expect(fs.fstatSync).toHaveBeenCalled();
|
|
33
|
+
expect(fs.existsSync.mock.calls[0][0]).toBe('./log');
|
|
34
|
+
expect(fs.mkdirSync.mock.calls[0][0]).toBe('./log');
|
|
35
|
+
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('test init, with recreating log file', async () => {
|
|
39
|
+
fs.existsSync.mockReturnValue(true);
|
|
40
|
+
fs.openSync.mockReturnValue(1);
|
|
41
|
+
fs.fstatSync.mockReturnValue({ size: 1025 });
|
|
42
|
+
|
|
43
|
+
const logger = new Logger('test2');
|
|
44
|
+
logger.init({
|
|
45
|
+
folder: './log',
|
|
46
|
+
filename: 'log%index%.log',
|
|
47
|
+
maxSize: 10,
|
|
48
|
+
maxCount: 5,
|
|
49
|
+
outToFile: true,
|
|
50
|
+
outToConsole: true,
|
|
51
|
+
level: LogLevels.debug,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
logger.debug('a'.repeat(11));
|
|
55
|
+
|
|
56
|
+
expect(fs.closeSync).toHaveBeenCalled();
|
|
57
|
+
expect(fs.openSync).toHaveBeenCalled();
|
|
58
|
+
expect(fs.fstatSync).toHaveBeenCalled();
|
|
59
|
+
expect(fs.closeSync.mock.calls[0][0]).toBe(1);
|
|
60
|
+
|
|
61
|
+
expect(fs.renameSync.mock.calls[0][0]).toBe('log/log3.log');
|
|
62
|
+
expect(fs.renameSync.mock.calls[3][0]).toBe('log/log0.log');
|
|
63
|
+
|
|
64
|
+
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
65
|
+
expect(fs.openSync.mock.calls[1][0]).toBe('log/log0.log');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('test init, with recreating log file after logs', async () => {
|
|
69
|
+
fs.existsSync.mockReturnValue(true);
|
|
70
|
+
fs.openSync.mockReturnValue(1);
|
|
71
|
+
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
72
|
+
|
|
73
|
+
const logger = new Logger('test2');
|
|
74
|
+
logger.init({
|
|
75
|
+
folder: './log',
|
|
76
|
+
filename: 'log%index%.log',
|
|
77
|
+
maxSize: 10,
|
|
78
|
+
maxCount: 5,
|
|
79
|
+
outToFile: true,
|
|
80
|
+
outToConsole: true,
|
|
81
|
+
level: LogLevels.debug,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
logger.debug('a'.repeat(9));
|
|
85
|
+
|
|
86
|
+
expect(fs.openSync).toHaveBeenCalled();
|
|
87
|
+
expect(fs.fstatSync).toHaveBeenCalled();
|
|
88
|
+
|
|
89
|
+
fs.fstatSync.mockReturnValue({ size: 11 });
|
|
90
|
+
logger.debug('a'.repeat(2));
|
|
91
|
+
|
|
92
|
+
expect(fs.closeSync).toHaveBeenCalled();
|
|
93
|
+
expect(fs.closeSync.mock.calls[1][0]).toBe(1);
|
|
94
|
+
expect(fs.renameSync.mock.calls[0][0]).toBe('log/log3.log');
|
|
95
|
+
expect(fs.renameSync.mock.calls[3][0]).toBe('log/log0.log');
|
|
96
|
+
|
|
97
|
+
expect(fs.openSync.mock.calls[0][0]).toBe('log/log0.log');
|
|
98
|
+
expect(fs.openSync.mock.calls[1][0]).toBe('log/log0.log');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('test log format', async () => {
|
|
102
|
+
fs.existsSync.mockReturnValue(true);
|
|
103
|
+
fs.openSync.mockReturnValue(1);
|
|
104
|
+
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
105
|
+
|
|
106
|
+
const logger = new Logger('test2');
|
|
107
|
+
logger.init({
|
|
108
|
+
folder: './log',
|
|
109
|
+
filename: 'log%index%.log',
|
|
110
|
+
maxSize: 1024,
|
|
111
|
+
maxCount: 5,
|
|
112
|
+
outToFile: true,
|
|
113
|
+
outToConsole: true,
|
|
114
|
+
level: LogLevels.debug,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
logger.debug(
|
|
118
|
+
'log test. numer: %d, string: %s, json: %j, [%%], Pretty-print: %O',
|
|
119
|
+
123,
|
|
120
|
+
's456',
|
|
121
|
+
{ k: 'key', v: 789 },
|
|
122
|
+
{ k2: 'key2', v2: 901 }
|
|
123
|
+
);
|
|
124
|
+
console.log('test writing log: [' + fs.writeFileSync.mock.calls[0][1].toString() + ']');
|
|
125
|
+
expect(
|
|
126
|
+
fs.writeFileSync.mock.calls[0][1]
|
|
127
|
+
.toString()
|
|
128
|
+
.endsWith(
|
|
129
|
+
`test2: log test. numer: 123, string: s456, json: {"k":"key","v":789}, [%], Pretty-print: { k2: 'key2', v2: 901 }\r\n`
|
|
130
|
+
)
|
|
131
|
+
).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('test different log level', async () => {
|
|
135
|
+
fs.existsSync.mockReturnValue(true);
|
|
136
|
+
fs.openSync.mockReturnValue(1);
|
|
137
|
+
fs.fstatSync.mockReturnValue({ size: 9 });
|
|
138
|
+
|
|
139
|
+
const logger = new Logger('test2');
|
|
140
|
+
logger.init({
|
|
141
|
+
folder: './log',
|
|
142
|
+
filename: 'log%index%.log',
|
|
143
|
+
maxSize: 1024,
|
|
144
|
+
maxCount: 5,
|
|
145
|
+
outToFile: true,
|
|
146
|
+
outToConsole: true,
|
|
147
|
+
level: LogLevels.info,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
logger.debug('a'.repeat(10));
|
|
151
|
+
logger.info('b'.repeat(10));
|
|
152
|
+
logger.warn('c'.repeat(10));
|
|
153
|
+
logger.error('d'.repeat(10));
|
|
154
|
+
logger.fatal('e'.repeat(10));
|
|
155
|
+
console.log('test writing log: [' + fs.writeFileSync.mock.calls[0][1].toString() + ']');
|
|
156
|
+
expect(fs.writeFileSync.mock.calls[0][1].toString().endsWith('test2: bbbbbbbbbb\r\n')).toBe(true);
|
|
157
|
+
expect(fs.writeFileSync.mock.calls[3][1].toString().endsWith('test2: eeeeeeeeee\r\n')).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('test logs in cluster, messages should be sent to Master', async () => {
|
|
161
|
+
fs.existsSync.mockReturnValue(true);
|
|
162
|
+
fs.openSync.mockReturnValue(1);
|
|
163
|
+
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
164
|
+
|
|
165
|
+
const process = require('process');
|
|
166
|
+
const mockSend = jest.spyOn(process, 'send').mockImplementation(() => {});
|
|
167
|
+
|
|
168
|
+
const logger = new Logger('test2');
|
|
169
|
+
logger.init({
|
|
170
|
+
folder: './log',
|
|
171
|
+
filename: 'log%index%.log',
|
|
172
|
+
maxSize: 1024,
|
|
173
|
+
maxCount: 5,
|
|
174
|
+
outToFile: true,
|
|
175
|
+
outToConsole: true,
|
|
176
|
+
level: LogLevels.debug,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// change isPrimary to false, so Logger will send message to Master
|
|
180
|
+
cluster.isPrimary = false;
|
|
181
|
+
logger.debug('isPrimary, %d, %s', 1, '2');
|
|
182
|
+
cluster.isPrimary = true;
|
|
183
|
+
|
|
184
|
+
expect(mockSend).toHaveBeenCalledWith({
|
|
185
|
+
color: 204,
|
|
186
|
+
id: 'LogWriter',
|
|
187
|
+
level: 'DEBUG',
|
|
188
|
+
messageList: ['isPrimary, %d, %s', 1, '2'],
|
|
189
|
+
namespace: 'test2',
|
|
190
|
+
pid: process.pid,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('test no file output', async () => {
|
|
195
|
+
fs.existsSync.mockReturnValue(true);
|
|
196
|
+
fs.openSync.mockReturnValue(1);
|
|
197
|
+
fs.fstatSync.mockReturnValue({ size: 1 });
|
|
198
|
+
|
|
199
|
+
const logger = new Logger('test2');
|
|
200
|
+
logger.init({
|
|
201
|
+
folder: './log',
|
|
202
|
+
filename: 'log%index%.log',
|
|
203
|
+
maxSize: 1024,
|
|
204
|
+
maxCount: 5,
|
|
205
|
+
outToFile: false,
|
|
206
|
+
outToConsole: true,
|
|
207
|
+
level: LogLevels.debug,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
logger.debug('debug');
|
|
211
|
+
|
|
212
|
+
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
});
|