@webex/plugin-logger 3.12.0 → 3.13.0-next.1
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/.repo-context.json +389 -0
- package/.sdd/manifest.json +334 -0
- package/AGENTS.md +175 -0
- package/dist/config.js +10 -4
- package/dist/config.js.map +1 -1
- package/dist/logger.js +203 -44
- package/dist/logger.js.map +1 -1
- package/docs/adr/0001-retain-product-readme.md +76 -0
- package/docs/adr/index.md +18 -0
- package/docs/architecture.md +263 -0
- package/docs/getting-started.md +88 -0
- package/docs/index.md +42 -0
- package/docs/specs/README.md +65 -0
- package/index.d.ts +41 -0
- package/package.json +11 -10
- package/schemas/repo-context.schema.json +796 -0
- package/src/config.js +10 -4
- package/src/docs/README.md +331 -0
- package/src/logger.js +203 -49
- package/test/unit/spec/logger.js +1201 -54
package/test/unit/spec/logger.js
CHANGED
|
@@ -10,6 +10,8 @@ import {browserOnly, nodeOnly, inBrowser} from '@webex/test-helper-mocha';
|
|
|
10
10
|
import Logger, {levels} from '@webex/plugin-logger';
|
|
11
11
|
import {WebexHttpError} from '@webex/webex-core';
|
|
12
12
|
|
|
13
|
+
import loggerConfig from '../../../src/config';
|
|
14
|
+
|
|
13
15
|
describe('plugin-logger', () => {
|
|
14
16
|
const logSpies = [];
|
|
15
17
|
|
|
@@ -128,6 +130,69 @@ describe('plugin-logger', () => {
|
|
|
128
130
|
assert.equal(webex.logger.buffer.buffer[1][3], 3);
|
|
129
131
|
});
|
|
130
132
|
|
|
133
|
+
it('adjusts lastSubmitted when buffer overflows in single buffer mode', () => {
|
|
134
|
+
webex.config.logger.historyLength = 3;
|
|
135
|
+
|
|
136
|
+
webex.logger.log('a');
|
|
137
|
+
webex.logger.log('b');
|
|
138
|
+
|
|
139
|
+
// simulate a successful upload
|
|
140
|
+
webex.logger.formatLogs({diff: true});
|
|
141
|
+
webex.logger.updateLastSubmittedIndex();
|
|
142
|
+
assert.equal(webex.logger.buffer.lastSubmitted, 2);
|
|
143
|
+
|
|
144
|
+
// add more logs to trigger overflow
|
|
145
|
+
webex.logger.log('c');
|
|
146
|
+
webex.logger.log('d');
|
|
147
|
+
|
|
148
|
+
// buffer overflowed by 1, so lastSubmitted should be decremented by 1
|
|
149
|
+
assert.equal(webex.logger.buffer.lastSubmitted, 1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('clamps lastSubmitted to 0 when buffer overflow exceeds lastSubmitted in single buffer mode', () => {
|
|
153
|
+
webex.config.logger.historyLength = 2;
|
|
154
|
+
|
|
155
|
+
webex.logger.log('a');
|
|
156
|
+
|
|
157
|
+
// simulate a successful upload after just 1 log
|
|
158
|
+
webex.logger.formatLogs({diff: true});
|
|
159
|
+
webex.logger.updateLastSubmittedIndex();
|
|
160
|
+
assert.equal(webex.logger.buffer.lastSubmitted, 1);
|
|
161
|
+
|
|
162
|
+
// add enough logs to overflow past the lastSubmitted value
|
|
163
|
+
webex.logger.log('b');
|
|
164
|
+
webex.logger.log('c');
|
|
165
|
+
webex.logger.log('d');
|
|
166
|
+
|
|
167
|
+
assert.equal(webex.logger.buffer.lastSubmitted, 0);
|
|
168
|
+
assert.lengthOf(webex.logger.buffer.buffer, 2);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('adjusts lastSubmitted when buffer overflows in separate buffer mode', () => {
|
|
172
|
+
webex.config.logger.separateLogBuffers = true;
|
|
173
|
+
webex.config.logger.clientName = 'someclient';
|
|
174
|
+
webex.config.logger.historyLength = 3;
|
|
175
|
+
|
|
176
|
+
webex.logger.log('sdk1');
|
|
177
|
+
webex.logger.log('sdk2');
|
|
178
|
+
webex.logger.client_log('client1');
|
|
179
|
+
webex.logger.client_log('client2');
|
|
180
|
+
|
|
181
|
+
webex.logger.formatLogs({diff: true});
|
|
182
|
+
webex.logger.updateLastSubmittedIndex();
|
|
183
|
+
assert.equal(webex.logger.sdkBuffer.lastSubmitted, 2);
|
|
184
|
+
assert.equal(webex.logger.clientBuffer.lastSubmitted, 2);
|
|
185
|
+
|
|
186
|
+
// overflow both buffers by 1
|
|
187
|
+
webex.logger.log('sdk3');
|
|
188
|
+
webex.logger.log('sdk4');
|
|
189
|
+
webex.logger.client_log('client3');
|
|
190
|
+
webex.logger.client_log('client4');
|
|
191
|
+
|
|
192
|
+
assert.equal(webex.logger.sdkBuffer.lastSubmitted, 1);
|
|
193
|
+
assert.equal(webex.logger.clientBuffer.lastSubmitted, 1);
|
|
194
|
+
});
|
|
195
|
+
|
|
131
196
|
it('prevents the client and sdk buffer from overflowing', () => {
|
|
132
197
|
webex.config.logger.historyLength = 2;
|
|
133
198
|
webex.config.logger.separateLogBuffers = true;
|
|
@@ -196,8 +261,8 @@ describe('plugin-logger', () => {
|
|
|
196
261
|
test: 'object',
|
|
197
262
|
nested: {
|
|
198
263
|
test2: 'object2',
|
|
199
|
-
}
|
|
200
|
-
}
|
|
264
|
+
},
|
|
265
|
+
};
|
|
201
266
|
|
|
202
267
|
webex.logger.log('foo', 'bar', obj);
|
|
203
268
|
assert.lengthOf(webex.logger.buffer.buffer, 1);
|
|
@@ -205,7 +270,10 @@ describe('plugin-logger', () => {
|
|
|
205
270
|
assert.deepEqual(webex.logger.buffer.buffer[0][2], 'wx-js-sdk');
|
|
206
271
|
assert.deepEqual(webex.logger.buffer.buffer[0][3], 'foo');
|
|
207
272
|
assert.deepEqual(webex.logger.buffer.buffer[0][4], 'bar');
|
|
208
|
-
assert.deepEqual(
|
|
273
|
+
assert.deepEqual(
|
|
274
|
+
webex.logger.buffer.buffer[0][5],
|
|
275
|
+
'{"headers":{"trackingid":"123"},"test":"object","nested":{"test2":"object2"}}'
|
|
276
|
+
);
|
|
209
277
|
});
|
|
210
278
|
|
|
211
279
|
it('formats objects as strings passed to the logger for readability not [Object object] w/ circular reference', async () => {
|
|
@@ -218,8 +286,8 @@ describe('plugin-logger', () => {
|
|
|
218
286
|
test: 'object',
|
|
219
287
|
nested: {
|
|
220
288
|
test2: 'object2',
|
|
221
|
-
}
|
|
222
|
-
}
|
|
289
|
+
},
|
|
290
|
+
};
|
|
223
291
|
|
|
224
292
|
obj.selfReference = obj;
|
|
225
293
|
|
|
@@ -229,19 +297,26 @@ describe('plugin-logger', () => {
|
|
|
229
297
|
assert.deepEqual(webex.logger.buffer.buffer[0][2], 'wx-js-sdk');
|
|
230
298
|
assert.deepEqual(webex.logger.buffer.buffer[0][3], 'foo');
|
|
231
299
|
assert.deepEqual(webex.logger.buffer.buffer[0][4], 'bar');
|
|
232
|
-
assert.deepEqual(
|
|
300
|
+
assert.deepEqual(
|
|
301
|
+
webex.logger.buffer.buffer[0][5],
|
|
302
|
+
'{"headers":{"trackingid":"123"},"test":"object","nested":{"test2":"object2"}}'
|
|
303
|
+
);
|
|
233
304
|
});
|
|
234
305
|
|
|
235
306
|
it('formats Errors correctly', async () => {
|
|
236
307
|
webex.config.logger.level = 'trace';
|
|
237
|
-
const err = new Error('fake error for testing')
|
|
308
|
+
const err = new Error('fake error for testing');
|
|
238
309
|
|
|
239
310
|
webex.logger.log('I got this error:', err);
|
|
240
311
|
assert.lengthOf(webex.logger.buffer.buffer, 1);
|
|
241
312
|
assert.deepEqual(webex.logger.buffer.buffer[0][2], 'wx-js-sdk');
|
|
242
313
|
assert.deepEqual(webex.logger.buffer.buffer[0][3], 'I got this error:');
|
|
243
|
-
|
|
314
|
+
const expected =
|
|
315
|
+
process.env.NODE_ENV === 'test' && inBrowser()
|
|
316
|
+
? `${err}BEGIN STACK${err.stack}END STACK`
|
|
317
|
+
: 'Error: fake error for testing';
|
|
244
318
|
|
|
319
|
+
assert.deepEqual(webex.logger.buffer.buffer[0][4], expected);
|
|
245
320
|
});
|
|
246
321
|
});
|
|
247
322
|
|
|
@@ -649,12 +724,16 @@ describe('plugin-logger', () => {
|
|
|
649
724
|
},
|
|
650
725
|
});
|
|
651
726
|
|
|
652
|
-
if(inBrowser()) {
|
|
653
|
-
assert.calledWith(
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
727
|
+
if (inBrowser()) {
|
|
728
|
+
assert.calledWith(
|
|
729
|
+
console[impl(level)],
|
|
730
|
+
'wx-js-sdk',
|
|
731
|
+
JSON.stringify({
|
|
732
|
+
headers: {
|
|
733
|
+
trackingid: '123',
|
|
734
|
+
},
|
|
735
|
+
})
|
|
736
|
+
);
|
|
658
737
|
} else {
|
|
659
738
|
assert.calledWith(console[impl(level)], 'wx-js-sdk', {
|
|
660
739
|
headers: {
|
|
@@ -676,24 +755,23 @@ describe('plugin-logger', () => {
|
|
|
676
755
|
|
|
677
756
|
// Assert auth was filtered
|
|
678
757
|
|
|
679
|
-
if(inBrowser()) {
|
|
680
|
-
assert.calledWith(console.log,
|
|
758
|
+
if (inBrowser()) {
|
|
759
|
+
assert.calledWith(console.log, 'wx-js-sdk', JSON.stringify({Key: 'myKey'}));
|
|
681
760
|
} else {
|
|
682
|
-
assert.calledWith(console.log,
|
|
761
|
+
assert.calledWith(console.log, 'wx-js-sdk', {Key: 'myKey'});
|
|
683
762
|
}
|
|
684
|
-
|
|
763
|
+
webex.logger.log({
|
|
685
764
|
authorization: 'XXXXXXX',
|
|
686
765
|
Key: 'myKey',
|
|
687
766
|
});
|
|
688
|
-
9
|
|
689
|
-
|
|
690
|
-
if(inBrowser()) {
|
|
691
|
-
assert.calledWith(console.log, "wx-js-sdk", JSON.stringify({Key: 'myKey'}));
|
|
767
|
+
9;
|
|
692
768
|
|
|
769
|
+
if (inBrowser()) {
|
|
770
|
+
assert.calledWith(console.log, 'wx-js-sdk', JSON.stringify({Key: 'myKey'}));
|
|
693
771
|
} else {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
772
|
+
assert.calledWith(console.log, 'wx-js-sdk', {Key: 'myKey'});
|
|
773
|
+
}
|
|
774
|
+
});
|
|
697
775
|
|
|
698
776
|
it('redact emails', () => {
|
|
699
777
|
webex.config.logger.level = 'trace';
|
|
@@ -708,21 +786,42 @@ describe('plugin-logger', () => {
|
|
|
708
786
|
it('redact MTID', () => {
|
|
709
787
|
webex.config.logger.level = 'trace';
|
|
710
788
|
|
|
711
|
-
const destination =
|
|
789
|
+
const destination =
|
|
790
|
+
'https://example.com/example/j.php?MTID=m678957bc1eff989c2176b43ead9d46b5';
|
|
712
791
|
|
|
713
|
-
webex.logger.log(
|
|
714
|
-
|
|
792
|
+
webex.logger.log(`Info Unable to fetch meeting info for ${destination}.`);
|
|
793
|
+
assert.calledWith(
|
|
794
|
+
console.log,
|
|
795
|
+
'wx-js-sdk',
|
|
796
|
+
'Info Unable to fetch meeting info for https://example.com/example/j.php?MTID=[REDACTED]'
|
|
715
797
|
);
|
|
716
|
-
assert.calledWith(console.log, 'wx-js-sdk', 'Info Unable to fetch meeting info for https://example.com/example/j.php?MTID=[REDACTED]');
|
|
717
798
|
|
|
718
|
-
webex.logger.log(
|
|
719
|
-
|
|
799
|
+
webex.logger.log(
|
|
800
|
+
'https://example.com/example/j.php?MTID=m678957bc1eff989c2176b43ead9d46b5&abcdefg'
|
|
801
|
+
);
|
|
802
|
+
assert.calledWith(
|
|
803
|
+
console.log,
|
|
804
|
+
'wx-js-sdk',
|
|
805
|
+
'https://example.com/example/j.php?MTID=[REDACTED]&abcdefg'
|
|
806
|
+
);
|
|
720
807
|
|
|
721
|
-
webex.logger.log(
|
|
722
|
-
|
|
808
|
+
webex.logger.log(
|
|
809
|
+
'https://example.com/example/j.php?MTID=m678957bc1eff989c2176b43ead9d46b5$abcdefg'
|
|
810
|
+
);
|
|
811
|
+
assert.calledWith(
|
|
812
|
+
console.log,
|
|
813
|
+
'wx-js-sdk',
|
|
814
|
+
'https://example.com/example/j.php?MTID=[REDACTED]$abcdefg'
|
|
815
|
+
);
|
|
723
816
|
|
|
724
|
-
webex.logger.log(
|
|
725
|
-
|
|
817
|
+
webex.logger.log(
|
|
818
|
+
'https://example.com/example/j.php?MTID=m678957bc1eff989c2176b43ead9d46b5#abcdefg'
|
|
819
|
+
);
|
|
820
|
+
assert.calledWith(
|
|
821
|
+
console.log,
|
|
822
|
+
'wx-js-sdk',
|
|
823
|
+
'https://example.com/example/j.php?MTID=[REDACTED]#abcdefg'
|
|
824
|
+
);
|
|
726
825
|
});
|
|
727
826
|
|
|
728
827
|
nodeOnly(it)('handle circular references', () => {
|
|
@@ -744,17 +843,15 @@ describe('plugin-logger', () => {
|
|
|
744
843
|
Key: 'myKey',
|
|
745
844
|
};
|
|
746
845
|
|
|
747
|
-
// Has self reference which is bad
|
|
846
|
+
// Has self reference which is bad
|
|
748
847
|
expected.selfReference = expected;
|
|
749
848
|
|
|
750
|
-
if(inBrowser()) {
|
|
751
|
-
assert.calledWith(console.log,
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
}
|
|
757
|
-
});
|
|
849
|
+
if (inBrowser()) {
|
|
850
|
+
assert.calledWith(console.log, 'wx-js-sdk', JSON.stringify(expected));
|
|
851
|
+
} else {
|
|
852
|
+
assert.calledWith(console.log, 'wx-js-sdk', expected);
|
|
853
|
+
}
|
|
854
|
+
});
|
|
758
855
|
|
|
759
856
|
nodeOnly(it)('handle circular references in complex objects', () => {
|
|
760
857
|
webex.config.logger.level = 'trace';
|
|
@@ -802,18 +899,15 @@ describe('plugin-logger', () => {
|
|
|
802
899
|
circularObjectRef: object,
|
|
803
900
|
circularFunctionRef: func,
|
|
804
901
|
},
|
|
805
|
-
}
|
|
806
|
-
|
|
902
|
+
};
|
|
807
903
|
|
|
808
|
-
if(inBrowser()) {
|
|
809
|
-
assert.calledWith(console.log,
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
}
|
|
815
|
-
});
|
|
904
|
+
if (inBrowser()) {
|
|
905
|
+
assert.calledWith(console.log, 'wx-js-sdk', JSON.stringify(res));
|
|
906
|
+
} else {
|
|
907
|
+
assert.calledWith(console.log, 'wx-js-sdk', res);
|
|
908
|
+
}
|
|
816
909
|
});
|
|
910
|
+
});
|
|
817
911
|
|
|
818
912
|
describe('#formatLogs()', () => {
|
|
819
913
|
function sendRandomLog(log) {
|
|
@@ -1265,4 +1359,1057 @@ describe('plugin-logger', () => {
|
|
|
1265
1359
|
assert.lengthOf(webex.logger.buffer.buffer, 5);
|
|
1266
1360
|
});
|
|
1267
1361
|
});
|
|
1362
|
+
|
|
1363
|
+
describe('#updateLastSubmittedIndex()', () => {
|
|
1364
|
+
let clock;
|
|
1365
|
+
|
|
1366
|
+
beforeEach(() => {
|
|
1367
|
+
clock = sinon.useFakeTimers();
|
|
1368
|
+
});
|
|
1369
|
+
|
|
1370
|
+
afterEach(() => {
|
|
1371
|
+
clock.restore();
|
|
1372
|
+
});
|
|
1373
|
+
|
|
1374
|
+
it('updates lastSubmitted to the current nextIndex in single buffer mode', () => {
|
|
1375
|
+
webex.config.logger.separateLogBuffers = false;
|
|
1376
|
+
|
|
1377
|
+
webex.logger.log('a');
|
|
1378
|
+
clock.tick(1000);
|
|
1379
|
+
webex.logger.log('b');
|
|
1380
|
+
clock.tick(1000);
|
|
1381
|
+
|
|
1382
|
+
// advance nextIndex by consuming a diff
|
|
1383
|
+
webex.logger.formatLogs({diff: true});
|
|
1384
|
+
const nextIndexAfterDiff = webex.logger.buffer.nextIndex;
|
|
1385
|
+
|
|
1386
|
+
assert.equal(webex.logger.buffer.lastSubmitted, 0);
|
|
1387
|
+
|
|
1388
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1389
|
+
|
|
1390
|
+
assert.equal(webex.logger.buffer.lastSubmitted, nextIndexAfterDiff);
|
|
1391
|
+
});
|
|
1392
|
+
|
|
1393
|
+
it('updates lastSubmitted to the current nextIndex in separate buffer mode', () => {
|
|
1394
|
+
webex.config.logger.separateLogBuffers = true;
|
|
1395
|
+
webex.config.logger.clientName = 'someclient';
|
|
1396
|
+
|
|
1397
|
+
webex.logger.log('sdk1');
|
|
1398
|
+
clock.tick(1000);
|
|
1399
|
+
webex.logger.client_log('client1');
|
|
1400
|
+
clock.tick(1000);
|
|
1401
|
+
|
|
1402
|
+
webex.logger.formatLogs({diff: true});
|
|
1403
|
+
const sdkNextIndex = webex.logger.sdkBuffer.nextIndex;
|
|
1404
|
+
const clientNextIndex = webex.logger.clientBuffer.nextIndex;
|
|
1405
|
+
|
|
1406
|
+
assert.equal(webex.logger.sdkBuffer.lastSubmitted, 0);
|
|
1407
|
+
assert.equal(webex.logger.clientBuffer.lastSubmitted, 0);
|
|
1408
|
+
|
|
1409
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1410
|
+
|
|
1411
|
+
assert.equal(webex.logger.sdkBuffer.lastSubmitted, sdkNextIndex);
|
|
1412
|
+
assert.equal(webex.logger.clientBuffer.lastSubmitted, clientNextIndex);
|
|
1413
|
+
});
|
|
1414
|
+
|
|
1415
|
+
it('stores lastSubmitted as 0 when no diff has been consumed yet in single buffer mode', () => {
|
|
1416
|
+
webex.config.logger.separateLogBuffers = false;
|
|
1417
|
+
|
|
1418
|
+
webex.logger.log('a');
|
|
1419
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1420
|
+
|
|
1421
|
+
// nextIndex is still 0 because no diff was consumed
|
|
1422
|
+
assert.equal(webex.logger.buffer.lastSubmitted, webex.logger.buffer.nextIndex);
|
|
1423
|
+
});
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
describe('#resetBufferToLastSuccessfulUpload()', () => {
|
|
1427
|
+
let clock;
|
|
1428
|
+
|
|
1429
|
+
beforeEach(() => {
|
|
1430
|
+
clock = sinon.useFakeTimers();
|
|
1431
|
+
});
|
|
1432
|
+
|
|
1433
|
+
afterEach(() => {
|
|
1434
|
+
clock.restore();
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
it('resets nextIndex to lastSubmitted in single buffer mode', () => {
|
|
1438
|
+
webex.config.logger.separateLogBuffers = false;
|
|
1439
|
+
|
|
1440
|
+
webex.logger.log('a');
|
|
1441
|
+
clock.tick(1000);
|
|
1442
|
+
webex.logger.log('b');
|
|
1443
|
+
clock.tick(1000);
|
|
1444
|
+
|
|
1445
|
+
// simulate a successful upload: consume diff then mark as submitted
|
|
1446
|
+
webex.logger.formatLogs({diff: true});
|
|
1447
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1448
|
+
const submittedIndex = webex.logger.buffer.lastSubmitted;
|
|
1449
|
+
|
|
1450
|
+
// log more entries (simulating logs added after the upload)
|
|
1451
|
+
webex.logger.log('c');
|
|
1452
|
+
clock.tick(1000);
|
|
1453
|
+
webex.logger.formatLogs({diff: true});
|
|
1454
|
+
assert.isAbove(webex.logger.buffer.nextIndex, submittedIndex);
|
|
1455
|
+
|
|
1456
|
+
// simulate upload failure — reset so the next diff re-includes logs since last success
|
|
1457
|
+
webex.logger.resetBufferToLastSuccessfulUpload();
|
|
1458
|
+
|
|
1459
|
+
assert.equal(webex.logger.buffer.nextIndex, submittedIndex);
|
|
1460
|
+
});
|
|
1461
|
+
|
|
1462
|
+
it('resets nextIndex to lastSubmitted in separate buffer mode', () => {
|
|
1463
|
+
webex.config.logger.separateLogBuffers = true;
|
|
1464
|
+
webex.config.logger.clientName = 'someclient';
|
|
1465
|
+
|
|
1466
|
+
webex.logger.log('sdk1');
|
|
1467
|
+
clock.tick(1000);
|
|
1468
|
+
webex.logger.client_log('client1');
|
|
1469
|
+
clock.tick(1000);
|
|
1470
|
+
|
|
1471
|
+
webex.logger.formatLogs({diff: true});
|
|
1472
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1473
|
+
const submittedSdk = webex.logger.sdkBuffer.lastSubmitted;
|
|
1474
|
+
const submittedClient = webex.logger.clientBuffer.lastSubmitted;
|
|
1475
|
+
|
|
1476
|
+
webex.logger.log('sdk2');
|
|
1477
|
+
clock.tick(1000);
|
|
1478
|
+
webex.logger.client_log('client2');
|
|
1479
|
+
clock.tick(1000);
|
|
1480
|
+
webex.logger.formatLogs({diff: true});
|
|
1481
|
+
|
|
1482
|
+
assert.isAbove(webex.logger.sdkBuffer.nextIndex, submittedSdk);
|
|
1483
|
+
assert.isAbove(webex.logger.clientBuffer.nextIndex, submittedClient);
|
|
1484
|
+
|
|
1485
|
+
webex.logger.resetBufferToLastSuccessfulUpload();
|
|
1486
|
+
|
|
1487
|
+
assert.equal(webex.logger.sdkBuffer.nextIndex, submittedSdk);
|
|
1488
|
+
assert.equal(webex.logger.clientBuffer.nextIndex, submittedClient);
|
|
1489
|
+
});
|
|
1490
|
+
|
|
1491
|
+
it('causes the next diff to re-include logs since the last successful upload', () => {
|
|
1492
|
+
webex.config.logger.separateLogBuffers = false;
|
|
1493
|
+
|
|
1494
|
+
webex.logger.log('a');
|
|
1495
|
+
clock.tick(1000);
|
|
1496
|
+
webex.logger.log('b');
|
|
1497
|
+
clock.tick(1000);
|
|
1498
|
+
|
|
1499
|
+
// first successful upload
|
|
1500
|
+
const diff1 = webex.logger.formatLogs({diff: true});
|
|
1501
|
+
|
|
1502
|
+
assert.deepEqual(diff1.split('\n'), [
|
|
1503
|
+
',1970-01-01T00:00:00.000Z,wx-js-sdk,a',
|
|
1504
|
+
',1970-01-01T00:00:01.000Z,wx-js-sdk,b',
|
|
1505
|
+
]);
|
|
1506
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1507
|
+
|
|
1508
|
+
// new logs arrive
|
|
1509
|
+
webex.logger.log('c');
|
|
1510
|
+
clock.tick(1000);
|
|
1511
|
+
|
|
1512
|
+
// consume the diff (upload attempt that fails)
|
|
1513
|
+
const diff2 = webex.logger.formatLogs({diff: true});
|
|
1514
|
+
|
|
1515
|
+
assert.deepEqual(diff2.split('\n'), [',1970-01-01T00:00:02.000Z,wx-js-sdk,c']);
|
|
1516
|
+
|
|
1517
|
+
// upload failed — roll back
|
|
1518
|
+
webex.logger.resetBufferToLastSuccessfulUpload();
|
|
1519
|
+
|
|
1520
|
+
// retry: should get 'c' again
|
|
1521
|
+
const diff3 = webex.logger.formatLogs({diff: true});
|
|
1522
|
+
|
|
1523
|
+
assert.deepEqual(diff3.split('\n'), [',1970-01-01T00:00:02.000Z,wx-js-sdk,c']);
|
|
1524
|
+
});
|
|
1525
|
+
|
|
1526
|
+
it('resets nextIndex to 0 when updateLastSubmittedIndex was never called', () => {
|
|
1527
|
+
webex.config.logger.separateLogBuffers = false;
|
|
1528
|
+
|
|
1529
|
+
webex.logger.log('a');
|
|
1530
|
+
webex.logger.formatLogs({diff: true});
|
|
1531
|
+
assert.equal(webex.logger.buffer.nextIndex, 1);
|
|
1532
|
+
|
|
1533
|
+
// lastSubmitted defaults to 0 before any call to updateLastSubmittedIndex
|
|
1534
|
+
webex.logger.resetBufferToLastSuccessfulUpload();
|
|
1535
|
+
|
|
1536
|
+
assert.equal(webex.logger.buffer.nextIndex, 0);
|
|
1537
|
+
});
|
|
1538
|
+
});
|
|
1539
|
+
// Each case reads the logger created by beforeEach. Console calls are the contract under test.
|
|
1540
|
+
/* eslint-disable no-loop-func, no-console */
|
|
1541
|
+
describe('record bridge', () => {
|
|
1542
|
+
const timestamp = 1700000000123;
|
|
1543
|
+
const isoTimestamp = '2023-11-14T22:13:20.123Z';
|
|
1544
|
+
let sandbox;
|
|
1545
|
+
let clock;
|
|
1546
|
+
|
|
1547
|
+
const expectedRecord = (
|
|
1548
|
+
sequence,
|
|
1549
|
+
{type = 'sdk', level = 'info', values = ['message'], metadata} = {}
|
|
1550
|
+
) => ({
|
|
1551
|
+
sequence,
|
|
1552
|
+
timestamp,
|
|
1553
|
+
level,
|
|
1554
|
+
source: metadata?.source || type,
|
|
1555
|
+
body: ['', isoTimestamp, type === 'sdk' ? 'wx-js-sdk' : 'WebClient', ...values].join(','),
|
|
1556
|
+
...(metadata ? {eventName: metadata.eventName, attributes: metadata.attributes} : {}),
|
|
1557
|
+
});
|
|
1558
|
+
|
|
1559
|
+
const expectedEntry = (sequence, options = {}) => {
|
|
1560
|
+
const {type = 'sdk', level = 'info', values = ['message'], metadata} = options;
|
|
1561
|
+
|
|
1562
|
+
return Object.assign(
|
|
1563
|
+
['', isoTimestamp, type === 'sdk' ? 'wx-js-sdk' : 'WebClient', ...values],
|
|
1564
|
+
{
|
|
1565
|
+
sequence,
|
|
1566
|
+
timestamp,
|
|
1567
|
+
level,
|
|
1568
|
+
source: metadata?.source || type,
|
|
1569
|
+
metadata,
|
|
1570
|
+
}
|
|
1571
|
+
);
|
|
1572
|
+
};
|
|
1573
|
+
|
|
1574
|
+
const configureConsoleMethod = (level) => {
|
|
1575
|
+
const method = sandbox.stub(console, 'dir');
|
|
1576
|
+
|
|
1577
|
+
webex.logger.config.consoleMethods = {
|
|
1578
|
+
...webex.logger.config.consoleMethods,
|
|
1579
|
+
[impl(level)]: 'dir',
|
|
1580
|
+
};
|
|
1581
|
+
|
|
1582
|
+
return method;
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
const bufferState = () =>
|
|
1586
|
+
Object.fromEntries(
|
|
1587
|
+
['buffer', 'sdkBuffer', 'clientBuffer'].map((name) => [
|
|
1588
|
+
name,
|
|
1589
|
+
{
|
|
1590
|
+
...webex.logger[name],
|
|
1591
|
+
buffer: webex.logger[name].buffer.map((entry) => Object.assign([...entry], entry)),
|
|
1592
|
+
},
|
|
1593
|
+
])
|
|
1594
|
+
);
|
|
1595
|
+
|
|
1596
|
+
beforeEach(() => {
|
|
1597
|
+
sandbox = sinon.createSandbox();
|
|
1598
|
+
clock = sandbox.useFakeTimers({now: timestamp, toFake: ['Date']});
|
|
1599
|
+
Object.assign(webex.logger.config, {
|
|
1600
|
+
level: 'silent',
|
|
1601
|
+
clientLevel: 'silent',
|
|
1602
|
+
bufferLogLevel: 'trace',
|
|
1603
|
+
metadataField: 'metadata',
|
|
1604
|
+
clientName: 'WebClient',
|
|
1605
|
+
});
|
|
1606
|
+
});
|
|
1607
|
+
|
|
1608
|
+
afterEach(() => sandbox.restore());
|
|
1609
|
+
|
|
1610
|
+
it('reads current level configuration through the existing derived properties', () => {
|
|
1611
|
+
assert.strictEqual(webex.logger.level, 'silent');
|
|
1612
|
+
assert.strictEqual(webex.logger.client_level, 'silent');
|
|
1613
|
+
webex.logger.config.level = 'warn';
|
|
1614
|
+
webex.logger.config.clientLevel = 'debug';
|
|
1615
|
+
assert.strictEqual(webex.logger.level, 'warn');
|
|
1616
|
+
assert.strictEqual(webex.logger.client_level, 'debug');
|
|
1617
|
+
delete webex.logger.config.clientLevel;
|
|
1618
|
+
assert.strictEqual(webex.logger.client_level, 'warn');
|
|
1619
|
+
});
|
|
1620
|
+
|
|
1621
|
+
nodeOnly(it)('keeps test-mode Error arguments intact in Node', () => {
|
|
1622
|
+
process.env.NODE_ENV = 'test';
|
|
1623
|
+
const error = new Error('test error');
|
|
1624
|
+
|
|
1625
|
+
assert.strictEqual(webex.logger.filter(error)[0], error);
|
|
1626
|
+
});
|
|
1627
|
+
|
|
1628
|
+
nodeOnly(it)('uses the existing device prefix only for test console output', () => {
|
|
1629
|
+
process.env.NODE_ENV = 'test';
|
|
1630
|
+
webex.logger.config.level = 'info';
|
|
1631
|
+
webex.internal.device.url = 'https://example.test/device/abc';
|
|
1632
|
+
// Supply the own root binding checked by the legacy test-prefix helper.
|
|
1633
|
+
Object.defineProperty(webex.logger, 'webex', {configurable: true, value: webex});
|
|
1634
|
+
const output = configureConsoleMethod('info');
|
|
1635
|
+
|
|
1636
|
+
webex.logger.info('message');
|
|
1637
|
+
assert.calledOnceWithExactly(output, 'abc', 'wx-js-sdk', 'message');
|
|
1638
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
1639
|
+
});
|
|
1640
|
+
|
|
1641
|
+
nodeOnly(it)('selects the existing console fallback when a level method is missing', () => {
|
|
1642
|
+
const {trace} = console;
|
|
1643
|
+
|
|
1644
|
+
console.trace = undefined;
|
|
1645
|
+
try {
|
|
1646
|
+
let FallbackLogger;
|
|
1647
|
+
|
|
1648
|
+
jest.isolateModules(() => {
|
|
1649
|
+
// Reload this module to select its console method while trace is absent.
|
|
1650
|
+
// eslint-disable-next-line global-require
|
|
1651
|
+
FallbackLogger = require('../../../src/logger').default;
|
|
1652
|
+
});
|
|
1653
|
+
const other = new MockWebex({children: {logger: FallbackLogger}});
|
|
1654
|
+
|
|
1655
|
+
Object.assign(other.logger.config, {level: 'trace', bufferLogLevel: 'silent'});
|
|
1656
|
+
other.logger.trace('fallback');
|
|
1657
|
+
assert.calledOnceWithExactly(console.log, 'wx-js-sdk', 'fallback');
|
|
1658
|
+
assert.deepEqual(other.logger.getRecords(), []);
|
|
1659
|
+
} finally {
|
|
1660
|
+
console.trace = trace;
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
|
|
1664
|
+
nodeOnly(it)('uses the saved method for the resolved console fallback', () => {
|
|
1665
|
+
const output = sandbox.stub(console, 'dir');
|
|
1666
|
+
const {trace} = console;
|
|
1667
|
+
|
|
1668
|
+
console.trace = undefined;
|
|
1669
|
+
try {
|
|
1670
|
+
let FallbackLogger;
|
|
1671
|
+
|
|
1672
|
+
jest.isolateModules(() => {
|
|
1673
|
+
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
1674
|
+
FallbackLogger = require('../../../src/logger').default;
|
|
1675
|
+
});
|
|
1676
|
+
const other = new MockWebex({children: {logger: FallbackLogger}});
|
|
1677
|
+
|
|
1678
|
+
Object.assign(other.logger.config, {
|
|
1679
|
+
level: 'trace',
|
|
1680
|
+
bufferLogLevel: 'trace',
|
|
1681
|
+
consoleMethods: {log: 'dir'},
|
|
1682
|
+
});
|
|
1683
|
+
other.logger.trace('fallback');
|
|
1684
|
+
assert.calledOnceWithExactly(output, 'wx-js-sdk', 'fallback');
|
|
1685
|
+
assert.strictEqual(output.firstCall.thisValue, console);
|
|
1686
|
+
assert.notCalled(console.log);
|
|
1687
|
+
assert.deepEqual(other.logger.getRecords(), [
|
|
1688
|
+
expectedRecord(1, {level: 'trace', values: ['fallback']}),
|
|
1689
|
+
]);
|
|
1690
|
+
} finally {
|
|
1691
|
+
console.trace = trace;
|
|
1692
|
+
}
|
|
1693
|
+
});
|
|
1694
|
+
|
|
1695
|
+
it('keeps the configured history default at 10,000 entries', () => {
|
|
1696
|
+
assert.strictEqual(loggerConfig.logger.historyLength, 10000);
|
|
1697
|
+
});
|
|
1698
|
+
|
|
1699
|
+
for (const metadataField of [undefined, '']) {
|
|
1700
|
+
it(`keeps ordinary object fields when metadata extraction is disabled: ${metadataField}`, () => {
|
|
1701
|
+
webex.logger.config.metadataField = metadataField;
|
|
1702
|
+
const value = {[String(metadataField)]: {eventName: 'ordinary'}, message: 'kept'};
|
|
1703
|
+
|
|
1704
|
+
webex.logger.info(value);
|
|
1705
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1706
|
+
expectedRecord(1, {values: [JSON.stringify(value)]}),
|
|
1707
|
+
]);
|
|
1708
|
+
assert.deepEqual(value, {
|
|
1709
|
+
[String(metadataField)]: {eventName: 'ordinary'},
|
|
1710
|
+
message: 'kept',
|
|
1711
|
+
});
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
it('keeps sequence and retained state separate for each logger instance', () => {
|
|
1716
|
+
const other = new MockWebex({children: {logger: Logger}});
|
|
1717
|
+
|
|
1718
|
+
Object.assign(other.logger.config, {level: 'silent', bufferLogLevel: 'info'});
|
|
1719
|
+
webex.logger.info('message');
|
|
1720
|
+
other.logger.info('independent');
|
|
1721
|
+
webex.logger.info('next');
|
|
1722
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1723
|
+
expectedRecord(1),
|
|
1724
|
+
expectedRecord(2, {values: ['next']}),
|
|
1725
|
+
]);
|
|
1726
|
+
assert.deepEqual(other.logger.getRecords(), [expectedRecord(1, {values: ['independent']})]);
|
|
1727
|
+
assert.notStrictEqual(webex.logger.recordState, other.logger.recordState);
|
|
1728
|
+
assert.notStrictEqual(webex.logger.buffer, other.logger.buffer);
|
|
1729
|
+
});
|
|
1730
|
+
|
|
1731
|
+
for (const source of [undefined, '', 'explicit']) {
|
|
1732
|
+
it(`uses the SDK source when the metadata source is empty: ${source}`, () => {
|
|
1733
|
+
const metadata = {source};
|
|
1734
|
+
|
|
1735
|
+
webex.logger.info('message', {metadata});
|
|
1736
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1, {metadata})]);
|
|
1737
|
+
});
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
const assertLogPolicy = ({type = 'sdk', level = 'info', print, buffer}) => {
|
|
1741
|
+
const metadata = {source: 'producer', eventName: 'event', attributes: {number: 2}};
|
|
1742
|
+
const configured = configureConsoleMethod(level);
|
|
1743
|
+
const normal = console[impl(level)];
|
|
1744
|
+
const listener = sinon.spy();
|
|
1745
|
+
|
|
1746
|
+
webex.logger.config[type === 'sdk' ? 'level' : 'clientLevel'] = print ? 'trace' : 'silent';
|
|
1747
|
+
webex.logger.config.bufferLogLevel = buffer ? 'trace' : 'silent';
|
|
1748
|
+
webex.logger.on('log:record', listener);
|
|
1749
|
+
webex.logger[type === 'sdk' ? level : `client_${level}`]('message', {metadata});
|
|
1750
|
+
const expectedOutput = print ? [[type === 'sdk' ? 'wx-js-sdk' : 'WebClient', 'message']] : [];
|
|
1751
|
+
const output = buffer ? configured : normal;
|
|
1752
|
+
|
|
1753
|
+
assert.deepEqual(output.args, expectedOutput);
|
|
1754
|
+
assert.notCalled(buffer ? normal : configured);
|
|
1755
|
+
if (print) assert.strictEqual(output.firstCall.thisValue, console);
|
|
1756
|
+
assert.deepEqual(
|
|
1757
|
+
webex.logger.getRecords(),
|
|
1758
|
+
buffer ? [expectedRecord(1, {type, level, metadata})] : []
|
|
1759
|
+
);
|
|
1760
|
+
assert.deepEqual(webex.logger.buffer, {
|
|
1761
|
+
buffer: buffer ? [expectedEntry(1, {type, level, metadata})] : [],
|
|
1762
|
+
nextIndex: 0,
|
|
1763
|
+
lastSubmitted: 0,
|
|
1764
|
+
});
|
|
1765
|
+
assert.deepEqual(listener.args, buffer ? [[expectedRecord(1, {type, level, metadata})]] : []);
|
|
1766
|
+
assert.equal(webex.logger.recordState.lastSequence, buffer ? 1 : 0);
|
|
1767
|
+
assert.equal(webex.logger.groupLevel, buffer && level === 'group' ? 1 : 0);
|
|
1768
|
+
};
|
|
1769
|
+
|
|
1770
|
+
for (const {print, buffer} of [
|
|
1771
|
+
{print: false, buffer: false},
|
|
1772
|
+
{print: false, buffer: true},
|
|
1773
|
+
{print: true, buffer: false},
|
|
1774
|
+
{print: true, buffer: true},
|
|
1775
|
+
]) {
|
|
1776
|
+
it(`keeps print=${print} and buffer=${buffer} independent of metadata`, () => {
|
|
1777
|
+
assertLogPolicy({print, buffer});
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
for (const type of ['sdk', 'client']) {
|
|
1782
|
+
for (const level of levels) {
|
|
1783
|
+
it(`keeps ${type}/${level} method mapping and metadata`, () => {
|
|
1784
|
+
assertLogPolicy({type, level, print: true, buffer: true});
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
for (const position of [0, 1, 2]) {
|
|
1790
|
+
it(`extracts metadata at argument position ${position} and redacts the other fields`, () => {
|
|
1791
|
+
const attributes = Object.freeze({detail: 'caller-value'});
|
|
1792
|
+
const metadata = Object.freeze({source: 'producer', eventName: 'event', attributes});
|
|
1793
|
+
const value = {
|
|
1794
|
+
metadata,
|
|
1795
|
+
headers: {authorization: 'Bearer', kept: 'value'},
|
|
1796
|
+
email: 'test@example.com',
|
|
1797
|
+
};
|
|
1798
|
+
const input = ['first', 'second'];
|
|
1799
|
+
|
|
1800
|
+
input.splice(position, 0, value);
|
|
1801
|
+
webex.logger.info(...input);
|
|
1802
|
+
const expected = ['first', 'second'];
|
|
1803
|
+
|
|
1804
|
+
expected.splice(position, 0, '{"headers":{"kept":"value"},"email":"[REDACTED]"}');
|
|
1805
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1806
|
+
expectedRecord(1, {values: expected, metadata}),
|
|
1807
|
+
]);
|
|
1808
|
+
assert.strictEqual(webex.logger.getRecords()[0].attributes, attributes);
|
|
1809
|
+
assert.deepEqual(value, {
|
|
1810
|
+
metadata,
|
|
1811
|
+
headers: {authorization: 'Bearer', kept: 'value'},
|
|
1812
|
+
email: 'test@example.com',
|
|
1813
|
+
});
|
|
1814
|
+
assert.strictEqual(input[position], value);
|
|
1815
|
+
assert.deepEqual(
|
|
1816
|
+
input.filter((item) => item !== value),
|
|
1817
|
+
['first', 'second']
|
|
1818
|
+
);
|
|
1819
|
+
});
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
for (const field of ['metadata', 'anotherField']) {
|
|
1823
|
+
it(`uses the configured own field ${field} and keeps the filter array contract`, () => {
|
|
1824
|
+
webex.logger.config.metadataField = field;
|
|
1825
|
+
const metadata = {eventName: 'event'};
|
|
1826
|
+
const input = {[field]: metadata, message: 'value'};
|
|
1827
|
+
|
|
1828
|
+
assert.deepEqual(webex.logger.filter('first', input, 'last'), [
|
|
1829
|
+
'first',
|
|
1830
|
+
{message: 'value'},
|
|
1831
|
+
'last',
|
|
1832
|
+
]);
|
|
1833
|
+
assert.deepEqual(input, {[field]: metadata, message: 'value'});
|
|
1834
|
+
webex.logger.info(input);
|
|
1835
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1836
|
+
expectedRecord(1, {values: ['{"message":"value"}'], metadata}),
|
|
1837
|
+
]);
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
for (const metadata of [undefined, null, {}, {eventName: 'last'}]) {
|
|
1842
|
+
it(`uses the last supplied metadata value ${JSON.stringify(metadata)}`, () => {
|
|
1843
|
+
webex.logger.info({metadata: {eventName: 'first'}}, 'message', {metadata});
|
|
1844
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1, {metadata})]);
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
it('omits metadata-only arguments from the message', () => {
|
|
1849
|
+
const metadata = {eventName: 'metadata-only', attributes: {flag: false}};
|
|
1850
|
+
|
|
1851
|
+
webex.logger.info({metadata});
|
|
1852
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1, {values: [], metadata})]);
|
|
1853
|
+
assert.deepEqual(webex.logger.buffer.buffer[0], expectedEntry(1, {values: [], metadata}));
|
|
1854
|
+
});
|
|
1855
|
+
|
|
1856
|
+
it('keeps all arguments as message data when metadata config is absent', () => {
|
|
1857
|
+
delete webex.logger.config.metadataField;
|
|
1858
|
+
const value = {metadata: {eventName: 'ordinary-data'}, message: 'value'};
|
|
1859
|
+
|
|
1860
|
+
webex.logger.info(value);
|
|
1861
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1862
|
+
expectedRecord(1, {values: [JSON.stringify(value)]}),
|
|
1863
|
+
]);
|
|
1864
|
+
assert.deepEqual(webex.logger.filter(value), [value]);
|
|
1865
|
+
});
|
|
1866
|
+
|
|
1867
|
+
it('does not read an inherited metadata property', () => {
|
|
1868
|
+
const prototype = {metadata: {eventName: 'inherited'}};
|
|
1869
|
+
const value = Object.assign(Object.create(prototype), {message: 'own'});
|
|
1870
|
+
|
|
1871
|
+
webex.logger.info(value);
|
|
1872
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1873
|
+
expectedRecord(1, {values: ['{"message":"own"}']}),
|
|
1874
|
+
]);
|
|
1875
|
+
assert.strictEqual(Object.getPrototypeOf(value), prototype);
|
|
1876
|
+
assert.deepEqual(value.metadata, prototype.metadata);
|
|
1877
|
+
});
|
|
1878
|
+
|
|
1879
|
+
it('keeps primitives, arrays and Error objects on their normal formatting paths', () => {
|
|
1880
|
+
const array = ['array'];
|
|
1881
|
+
const error = new Error('original error');
|
|
1882
|
+
|
|
1883
|
+
array.metadata = {source: 'not-array-metadata'};
|
|
1884
|
+
error.metadata = {source: 'not-error-metadata'};
|
|
1885
|
+
const errorText =
|
|
1886
|
+
process.env.NODE_ENV === 'test' && inBrowser()
|
|
1887
|
+
? `${error}BEGIN STACK${error.stack}END STACK`
|
|
1888
|
+
: error.toString();
|
|
1889
|
+
|
|
1890
|
+
webex.logger.info(null, undefined, false, 0, 'text', array, error);
|
|
1891
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1892
|
+
expectedRecord(1, {
|
|
1893
|
+
values: ['null', undefined, false, 0, 'text', '["array"]', errorText],
|
|
1894
|
+
}),
|
|
1895
|
+
]);
|
|
1896
|
+
assert.deepEqual(array, Object.assign(['array'], {metadata: {source: 'not-array-metadata'}}));
|
|
1897
|
+
assert.deepEqual(error.metadata, {source: 'not-error-metadata'});
|
|
1898
|
+
const filteredError = webex.logger.filter(error)[0];
|
|
1899
|
+
|
|
1900
|
+
if (process.env.NODE_ENV === 'test' && inBrowser()) {
|
|
1901
|
+
assert.strictEqual(filteredError, errorText);
|
|
1902
|
+
} else {
|
|
1903
|
+
assert.strictEqual(filteredError, error);
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
|
|
1907
|
+
it('keeps trusted metadata out of message redaction', () => {
|
|
1908
|
+
const attributes = {authorization: 'caller-owned', email: 'metadata@example.com'};
|
|
1909
|
+
const metadata = {attributes};
|
|
1910
|
+
|
|
1911
|
+
webex.logger.info({metadata, authorization: 'Bearer', email: 'body@example.com'});
|
|
1912
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1913
|
+
expectedRecord(1, {metadata, values: ['{"email":"[REDACTED]"}']}),
|
|
1914
|
+
]);
|
|
1915
|
+
assert.strictEqual(webex.logger.getRecords()[0].attributes, attributes);
|
|
1916
|
+
assert.deepEqual(attributes, {authorization: 'caller-owned', email: 'metadata@example.com'});
|
|
1917
|
+
});
|
|
1918
|
+
|
|
1919
|
+
it('uses one Date for the support timestamp and the fixed record times', () => {
|
|
1920
|
+
const toISOString = sandbox.spy(Date.prototype, 'toISOString');
|
|
1921
|
+
const getTime = sandbox.spy(Date.prototype, 'getTime');
|
|
1922
|
+
const parse = sandbox.spy(Date, 'parse');
|
|
1923
|
+
|
|
1924
|
+
webex.logger.info('message');
|
|
1925
|
+
clock.tick(5000);
|
|
1926
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
1927
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
1928
|
+
assert.calledOnce(toISOString);
|
|
1929
|
+
assert.calledOnce(getTime);
|
|
1930
|
+
assert.strictEqual(toISOString.firstCall.thisValue, getTime.firstCall.thisValue);
|
|
1931
|
+
assert.notCalled(parse);
|
|
1932
|
+
assert.strictEqual(webex.logger.buffer.buffer[0][1], isoTimestamp);
|
|
1933
|
+
});
|
|
1934
|
+
|
|
1935
|
+
for (const separate of [false, true]) {
|
|
1936
|
+
for (const order of [
|
|
1937
|
+
[],
|
|
1938
|
+
['sdk'],
|
|
1939
|
+
['client'],
|
|
1940
|
+
['sdk', 'client', 'sdk'],
|
|
1941
|
+
['client', 'sdk', 'client'],
|
|
1942
|
+
]) {
|
|
1943
|
+
it(`reads retained ${order.join(
|
|
1944
|
+
'/'
|
|
1945
|
+
)} records without changing state; separate=${separate}`, () => {
|
|
1946
|
+
webex.logger.config.separateLogBuffers = separate;
|
|
1947
|
+
order.forEach((type) => webex.logger[type === 'sdk' ? 'info' : 'client_info']('message'));
|
|
1948
|
+
webex.logger.formatLogs({diff: true});
|
|
1949
|
+
webex.logger.updateLastSubmittedIndex();
|
|
1950
|
+
const before = bufferState();
|
|
1951
|
+
const {groupLevel} = webex.logger;
|
|
1952
|
+
const expected = order.map((type, index) => expectedRecord(index + 1, {type}));
|
|
1953
|
+
|
|
1954
|
+
assert.deepEqual(webex.logger.getRecords(), expected);
|
|
1955
|
+
assert.deepEqual(webex.logger.getRecords(), expected);
|
|
1956
|
+
assert.deepEqual(bufferState(), before);
|
|
1957
|
+
assert.equal(webex.logger.groupLevel, groupLevel);
|
|
1958
|
+
webex.logger.info('next');
|
|
1959
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1960
|
+
...expected,
|
|
1961
|
+
expectedRecord(order.length + 1, {values: ['next']}),
|
|
1962
|
+
]);
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
for (const separate of [false, true]) {
|
|
1968
|
+
it(`keeps acceptance order when the clock moves backward; separate=${separate}`, () => {
|
|
1969
|
+
webex.logger.config.separateLogBuffers = separate;
|
|
1970
|
+
webex.logger.info('first');
|
|
1971
|
+
clock.setSystemTime(timestamp - 1000);
|
|
1972
|
+
webex.logger.client_info('second');
|
|
1973
|
+
clock.setSystemTime(timestamp + 2000);
|
|
1974
|
+
webex.logger.info('third');
|
|
1975
|
+
|
|
1976
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
1977
|
+
expectedRecord(1, {values: ['first']}),
|
|
1978
|
+
{
|
|
1979
|
+
...expectedRecord(2, {type: 'client', values: ['second']}),
|
|
1980
|
+
timestamp: timestamp - 1000,
|
|
1981
|
+
body: ',2023-11-14T22:13:19.123Z,WebClient,second',
|
|
1982
|
+
},
|
|
1983
|
+
{
|
|
1984
|
+
...expectedRecord(3, {values: ['third']}),
|
|
1985
|
+
timestamp: timestamp + 2000,
|
|
1986
|
+
body: ',2023-11-14T22:13:22.123Z,wx-js-sdk,third',
|
|
1987
|
+
},
|
|
1988
|
+
]);
|
|
1989
|
+
});
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
it('creates fresh history wrappers and shares only caller metadata values', () => {
|
|
1993
|
+
const attributes = {nested: {value: 'kept'}};
|
|
1994
|
+
const metadata = {eventName: 'event', attributes};
|
|
1995
|
+
const listener = sinon.spy();
|
|
1996
|
+
|
|
1997
|
+
webex.logger.on('log:record', listener);
|
|
1998
|
+
webex.logger.info('message', {metadata});
|
|
1999
|
+
const first = webex.logger.getRecords()[0];
|
|
2000
|
+
const second = webex.logger.getRecords()[0];
|
|
2001
|
+
|
|
2002
|
+
assert.deepEqual(first, expectedRecord(1, {metadata}));
|
|
2003
|
+
assert.deepEqual(second, first);
|
|
2004
|
+
assert.notStrictEqual(first, second);
|
|
2005
|
+
assert.notStrictEqual(first, listener.firstCall.args[0]);
|
|
2006
|
+
assert.strictEqual(first.attributes, attributes);
|
|
2007
|
+
assert.strictEqual(second.attributes, attributes);
|
|
2008
|
+
first.body = 'changed wrapper';
|
|
2009
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1, {metadata})]);
|
|
2010
|
+
assert.strictEqual(webex.logger.buffer.buffer[0].metadata, metadata);
|
|
2011
|
+
});
|
|
2012
|
+
|
|
2013
|
+
for (const separate of [false, true]) {
|
|
2014
|
+
it(`keeps sequence after trimming and runtime limit changes; separate=${separate}`, () => {
|
|
2015
|
+
webex.logger.config.separateLogBuffers = separate;
|
|
2016
|
+
webex.logger.config.historyLength = 2;
|
|
2017
|
+
['info', 'client_info', 'info', 'client_info', 'info', 'client_info'].forEach((method) =>
|
|
2018
|
+
webex.logger[method]('message')
|
|
2019
|
+
);
|
|
2020
|
+
const expected = separate
|
|
2021
|
+
? [
|
|
2022
|
+
expectedRecord(3),
|
|
2023
|
+
expectedRecord(4, {type: 'client'}),
|
|
2024
|
+
expectedRecord(5),
|
|
2025
|
+
expectedRecord(6, {type: 'client'}),
|
|
2026
|
+
]
|
|
2027
|
+
: [expectedRecord(5), expectedRecord(6, {type: 'client'})];
|
|
2028
|
+
|
|
2029
|
+
assert.deepEqual(webex.logger.getRecords(), expected);
|
|
2030
|
+
webex.logger.config.historyLength = 1;
|
|
2031
|
+
webex.logger.info('message');
|
|
2032
|
+
assert.deepEqual(
|
|
2033
|
+
webex.logger.getRecords(),
|
|
2034
|
+
separate
|
|
2035
|
+
? [
|
|
2036
|
+
expectedRecord(4, {type: 'client'}),
|
|
2037
|
+
expectedRecord(6, {type: 'client'}),
|
|
2038
|
+
expectedRecord(7),
|
|
2039
|
+
]
|
|
2040
|
+
: [expectedRecord(7)]
|
|
2041
|
+
);
|
|
2042
|
+
webex.logger.config.historyLength = 3;
|
|
2043
|
+
webex.logger.client_info('message');
|
|
2044
|
+
assert.deepEqual(
|
|
2045
|
+
webex.logger.getRecords(),
|
|
2046
|
+
separate
|
|
2047
|
+
? [
|
|
2048
|
+
expectedRecord(4, {type: 'client'}),
|
|
2049
|
+
expectedRecord(6, {type: 'client'}),
|
|
2050
|
+
expectedRecord(7),
|
|
2051
|
+
expectedRecord(8, {type: 'client'}),
|
|
2052
|
+
]
|
|
2053
|
+
: [expectedRecord(7), expectedRecord(8, {type: 'client'})]
|
|
2054
|
+
);
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
|
|
2058
|
+
it('uses clientHistoryLength for each separate buffer and keeps SDK/client state isolated', () => {
|
|
2059
|
+
webex.logger.config.separateLogBuffers = true;
|
|
2060
|
+
webex.logger.config.historyLength = 5;
|
|
2061
|
+
webex.logger.config.clientHistoryLength = 1;
|
|
2062
|
+
webex.logger.info('old sdk');
|
|
2063
|
+
webex.logger.client_info('old client');
|
|
2064
|
+
webex.logger.formatLogs({diff: true});
|
|
2065
|
+
webex.logger.updateLastSubmittedIndex();
|
|
2066
|
+
webex.logger.info('new sdk');
|
|
2067
|
+
assert.deepEqual(webex.logger.sdkBuffer, {
|
|
2068
|
+
buffer: [expectedEntry(3, {values: ['new sdk']})],
|
|
2069
|
+
nextIndex: 0,
|
|
2070
|
+
lastSubmitted: 0,
|
|
2071
|
+
});
|
|
2072
|
+
assert.deepEqual(webex.logger.clientBuffer, {
|
|
2073
|
+
buffer: [expectedEntry(2, {type: 'client', values: ['old client']})],
|
|
2074
|
+
nextIndex: 1,
|
|
2075
|
+
lastSubmitted: 1,
|
|
2076
|
+
});
|
|
2077
|
+
webex.logger.client_info('new client');
|
|
2078
|
+
assert.deepEqual(webex.logger.clientBuffer, {
|
|
2079
|
+
buffer: [expectedEntry(4, {type: 'client', values: ['new client']})],
|
|
2080
|
+
nextIndex: 0,
|
|
2081
|
+
lastSubmitted: 0,
|
|
2082
|
+
});
|
|
2083
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2084
|
+
expectedRecord(3, {values: ['new sdk']}),
|
|
2085
|
+
expectedRecord(4, {type: 'client', values: ['new client']}),
|
|
2086
|
+
]);
|
|
2087
|
+
});
|
|
2088
|
+
|
|
2089
|
+
for (const separate of [false, true]) {
|
|
2090
|
+
it(`keeps group indentation and support-upload cursors unchanged by reads; separate=${separate}`, () => {
|
|
2091
|
+
webex.logger.config.separateLogBuffers = separate;
|
|
2092
|
+
webex.logger.group('open');
|
|
2093
|
+
webex.logger.info('inside');
|
|
2094
|
+
const state = bufferState();
|
|
2095
|
+
const records = webex.logger.getRecords();
|
|
2096
|
+
|
|
2097
|
+
assert.equal(webex.logger.groupLevel, 1);
|
|
2098
|
+
assert.deepEqual(bufferState(), state);
|
|
2099
|
+
assert.deepEqual(records, [
|
|
2100
|
+
expectedRecord(1, {level: 'group', values: ['open']}),
|
|
2101
|
+
{
|
|
2102
|
+
...expectedRecord(2, {values: ['inside']}),
|
|
2103
|
+
body: `| ,${isoTimestamp},wx-js-sdk,inside`,
|
|
2104
|
+
},
|
|
2105
|
+
]);
|
|
2106
|
+
assert.strictEqual(
|
|
2107
|
+
webex.logger.formatLogs(),
|
|
2108
|
+
records.map((record) => record.body).join('\n')
|
|
2109
|
+
);
|
|
2110
|
+
const first = webex.logger.formatLogs({diff: true});
|
|
2111
|
+
|
|
2112
|
+
webex.logger.updateLastSubmittedIndex();
|
|
2113
|
+
webex.logger.groupEnd();
|
|
2114
|
+
assert.equal(webex.logger.groupLevel, 0);
|
|
2115
|
+
const next = webex.logger.formatLogs({diff: true});
|
|
2116
|
+
|
|
2117
|
+
webex.logger.resetBufferToLastSuccessfulUpload();
|
|
2118
|
+
assert.strictEqual(webex.logger.formatLogs({diff: true}), next);
|
|
2119
|
+
assert.strictEqual(webex.logger.formatLogs(), `${first}\n${next}`);
|
|
2120
|
+
assert.deepEqual(
|
|
2121
|
+
webex.logger.getRecords().map((record) => record.sequence),
|
|
2122
|
+
[1, 2, 3]
|
|
2123
|
+
);
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
it('does not emit records during subscription or history reads and removes the same callback', () => {
|
|
2128
|
+
const listener = sinon.spy();
|
|
2129
|
+
const changes = sinon.spy();
|
|
2130
|
+
|
|
2131
|
+
webex.logger.on('log:record', listener);
|
|
2132
|
+
webex.logger.on('change:recordState', changes);
|
|
2133
|
+
assert.deepEqual(webex.logger.getRecords(), []);
|
|
2134
|
+
assert.notCalled(listener);
|
|
2135
|
+
webex.logger.info('message');
|
|
2136
|
+
assert.calledOnceWithExactly(listener, expectedRecord(1));
|
|
2137
|
+
webex.logger.getRecords();
|
|
2138
|
+
assert.calledOnce(listener);
|
|
2139
|
+
webex.logger.off('log:record', listener);
|
|
2140
|
+
webex.logger.info('second');
|
|
2141
|
+
assert.calledOnce(listener);
|
|
2142
|
+
assert.notCalled(changes);
|
|
2143
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2144
|
+
expectedRecord(1),
|
|
2145
|
+
expectedRecord(2, {values: ['second']}),
|
|
2146
|
+
]);
|
|
2147
|
+
});
|
|
2148
|
+
|
|
2149
|
+
it('keeps native nested event order while retained history keeps acceptance order', () => {
|
|
2150
|
+
const laterListener = sinon.spy();
|
|
2151
|
+
|
|
2152
|
+
webex.logger.on('log:record', (record) => {
|
|
2153
|
+
if (record.sequence === 1) webex.logger.info('nested');
|
|
2154
|
+
});
|
|
2155
|
+
webex.logger.on('log:record', laterListener);
|
|
2156
|
+
webex.logger.info('message');
|
|
2157
|
+
assert.deepEqual(laterListener.args, [
|
|
2158
|
+
[expectedRecord(2, {values: ['nested']})],
|
|
2159
|
+
[expectedRecord(1)],
|
|
2160
|
+
]);
|
|
2161
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2162
|
+
expectedRecord(1),
|
|
2163
|
+
expectedRecord(2, {values: ['nested']}),
|
|
2164
|
+
]);
|
|
2165
|
+
});
|
|
2166
|
+
|
|
2167
|
+
for (const method of ['info', 'logToBuffer', 'client_logToBuffer']) {
|
|
2168
|
+
it(`contains a listener failure from ${method} and keeps the accepted record`, () => {
|
|
2169
|
+
const failure = new Error('listener failed');
|
|
2170
|
+
const bad = () => {
|
|
2171
|
+
throw failure;
|
|
2172
|
+
};
|
|
2173
|
+
const later = sinon.spy();
|
|
2174
|
+
|
|
2175
|
+
webex.logger.on('log:record', bad);
|
|
2176
|
+
webex.logger.on('log:record', later);
|
|
2177
|
+
assert.doesNotThrow(() => webex.logger[method]('message'));
|
|
2178
|
+
assert.notCalled(later);
|
|
2179
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2180
|
+
expectedRecord(1, {type: method.startsWith('client_') ? 'client' : 'sdk'}),
|
|
2181
|
+
]);
|
|
2182
|
+
webex.logger.off('log:record', bad);
|
|
2183
|
+
webex.logger.info('next');
|
|
2184
|
+
assert.calledOnceWithExactly(later, expectedRecord(2, {values: ['next']}));
|
|
2185
|
+
});
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
for (const method of ['logToBuffer', 'client_logToBuffer']) {
|
|
2189
|
+
it(`buffers ${method} at info even when configured levels are silent`, () => {
|
|
2190
|
+
webex.logger.config.bufferLogLevel = 'silent';
|
|
2191
|
+
const output = configureConsoleMethod('info');
|
|
2192
|
+
const listener = sinon.spy();
|
|
2193
|
+
|
|
2194
|
+
webex.logger.on('log:record', listener);
|
|
2195
|
+
webex.logger[method]('message');
|
|
2196
|
+
const record = expectedRecord(1, {type: method.startsWith('client_') ? 'client' : 'sdk'});
|
|
2197
|
+
|
|
2198
|
+
assert.calledOnceWithExactly(listener, record);
|
|
2199
|
+
assert.deepEqual(webex.logger.getRecords(), [record]);
|
|
2200
|
+
assert.notCalled(output);
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
for (const method of ['logToBuffer', 'client_logToBuffer']) {
|
|
2205
|
+
for (const listenerThrows of [false, true]) {
|
|
2206
|
+
it(`keeps ${method} silent with trace levels; listenerThrows=${listenerThrows}`, () => {
|
|
2207
|
+
Object.assign(webex.logger.config, {
|
|
2208
|
+
level: 'trace',
|
|
2209
|
+
clientLevel: 'trace',
|
|
2210
|
+
bufferLogLevel: 'silent',
|
|
2211
|
+
});
|
|
2212
|
+
const output = configureConsoleMethod('info');
|
|
2213
|
+
const listener = sinon.spy();
|
|
2214
|
+
|
|
2215
|
+
webex.logger.on('log:record', listener);
|
|
2216
|
+
if (listenerThrows) {
|
|
2217
|
+
webex.logger.on('log:record', () => {
|
|
2218
|
+
throw new Error('listener failed');
|
|
2219
|
+
});
|
|
2220
|
+
}
|
|
2221
|
+
assert.doesNotThrow(() => webex.logger[method]('message'));
|
|
2222
|
+
const record = expectedRecord(1, {type: method.startsWith('client_') ? 'client' : 'sdk'});
|
|
2223
|
+
|
|
2224
|
+
assert.calledOnceWithExactly(listener, record);
|
|
2225
|
+
assert.deepEqual(webex.logger.getRecords(), [record]);
|
|
2226
|
+
assert.notCalled(output);
|
|
2227
|
+
Object.values(logSpies).forEach((logSpy) => assert.notCalled(logSpy));
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
it('uses console method names supplied in startup configuration', () => {
|
|
2233
|
+
const output = configureConsoleMethod('info');
|
|
2234
|
+
const configured = new MockWebex({
|
|
2235
|
+
children: {logger: Logger},
|
|
2236
|
+
config: {
|
|
2237
|
+
logger: {
|
|
2238
|
+
level: 'info',
|
|
2239
|
+
bufferLogLevel: 'info',
|
|
2240
|
+
historyLength: 10000,
|
|
2241
|
+
consoleMethods: {info: 'dir'},
|
|
2242
|
+
},
|
|
2243
|
+
},
|
|
2244
|
+
});
|
|
2245
|
+
|
|
2246
|
+
configured.logger.info('message');
|
|
2247
|
+
assert.calledOnceWithExactly(output, 'wx-js-sdk', 'message');
|
|
2248
|
+
assert.strictEqual(output.firstCall.thisValue, console);
|
|
2249
|
+
assert.notCalled(console.info);
|
|
2250
|
+
assert.deepEqual(configured.logger.getRecords(), [expectedRecord(1)]);
|
|
2251
|
+
});
|
|
2252
|
+
|
|
2253
|
+
it('prints buffered calls through the configured method with the console receiver', () => {
|
|
2254
|
+
webex.logger.config.level = 'info';
|
|
2255
|
+
webex.logger.config.bufferLogLevel = 'info';
|
|
2256
|
+
const output = configureConsoleMethod('info');
|
|
2257
|
+
|
|
2258
|
+
webex.logger.info('message');
|
|
2259
|
+
assert.calledOnceWithExactly(output, 'wx-js-sdk', 'message');
|
|
2260
|
+
assert.strictEqual(output.firstCall.thisValue, console);
|
|
2261
|
+
assert.notCalled(console.info);
|
|
2262
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
2263
|
+
});
|
|
2264
|
+
|
|
2265
|
+
it('uses the normal console method when no methods are configured', () => {
|
|
2266
|
+
webex.logger.config.level = 'info';
|
|
2267
|
+
webex.logger.info('message');
|
|
2268
|
+
assert.calledOnceWithExactly(console.info, 'wx-js-sdk', 'message');
|
|
2269
|
+
assert.strictEqual(console.info.firstCall.thisValue, console);
|
|
2270
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
2271
|
+
});
|
|
2272
|
+
|
|
2273
|
+
it('uses the normal console method when the configured map lacks that method', () => {
|
|
2274
|
+
webex.logger.config.level = 'info';
|
|
2275
|
+
const error = configureConsoleMethod('error');
|
|
2276
|
+
|
|
2277
|
+
webex.logger.info('message');
|
|
2278
|
+
assert.calledOnceWithExactly(console.info, 'wx-js-sdk', 'message');
|
|
2279
|
+
assert.notCalled(error);
|
|
2280
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
2281
|
+
});
|
|
2282
|
+
|
|
2283
|
+
for (const savedMethod of [undefined, {}]) {
|
|
2284
|
+
it(`uses the normal console method when the saved value is ${typeof savedMethod}`, () => {
|
|
2285
|
+
webex.logger.config.level = 'info';
|
|
2286
|
+
webex.logger.config.consoleMethods = {info: 'dir'};
|
|
2287
|
+
sandbox.stub(console, 'dir').value(savedMethod);
|
|
2288
|
+
|
|
2289
|
+
webex.logger.info('message');
|
|
2290
|
+
assert.calledOnceWithExactly(console.info, 'wx-js-sdk', 'message');
|
|
2291
|
+
assert.strictEqual(console.info.firstCall.thisValue, console);
|
|
2292
|
+
assert.deepEqual(webex.logger.getRecords(), [expectedRecord(1)]);
|
|
2293
|
+
});
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
it('uses the saved method when it becomes available after configuration', () => {
|
|
2297
|
+
webex.logger.config.level = 'info';
|
|
2298
|
+
webex.logger.config.consoleMethods = {info: 'dir'};
|
|
2299
|
+
sandbox.stub(console, 'dir').value(undefined);
|
|
2300
|
+
|
|
2301
|
+
webex.logger.info('before capture');
|
|
2302
|
+
const output = sinon.spy();
|
|
2303
|
+
|
|
2304
|
+
console.dir = output;
|
|
2305
|
+
webex.logger.info('after capture');
|
|
2306
|
+
assert.calledOnceWithExactly(console.info, 'wx-js-sdk', 'before capture');
|
|
2307
|
+
assert.calledOnceWithExactly(output, 'wx-js-sdk', 'after capture');
|
|
2308
|
+
assert.strictEqual(output.firstCall.thisValue, console);
|
|
2309
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2310
|
+
expectedRecord(1, {values: ['before capture']}),
|
|
2311
|
+
expectedRecord(2, {values: ['after capture']}),
|
|
2312
|
+
]);
|
|
2313
|
+
});
|
|
2314
|
+
|
|
2315
|
+
for (const type of ['sdk', 'client']) {
|
|
2316
|
+
for (const level of ['debug', 'trace', 'info']) {
|
|
2317
|
+
it(`keeps one retained copy with host capture for ${type}/${level}`, () => {
|
|
2318
|
+
webex.logger.config[type === 'sdk' ? 'level' : 'clientLevel'] = 'trace';
|
|
2319
|
+
webex.logger.config.bufferLogLevel = 'info';
|
|
2320
|
+
const method = impl(level);
|
|
2321
|
+
const original = configureConsoleMethod(level);
|
|
2322
|
+
const normal = console[method];
|
|
2323
|
+
const capture = sinon.spy((...args) => {
|
|
2324
|
+
webex.logger.client_logToBuffer(`console ${method}: ${args.toString()}`);
|
|
2325
|
+
original.apply(console, args);
|
|
2326
|
+
});
|
|
2327
|
+
|
|
2328
|
+
console[method] = capture;
|
|
2329
|
+
try {
|
|
2330
|
+
webex.logger[type === 'sdk' ? level : `client_${level}`]('message');
|
|
2331
|
+
assert.calledOnceWithExactly(
|
|
2332
|
+
original,
|
|
2333
|
+
type === 'sdk' ? 'wx-js-sdk' : 'WebClient',
|
|
2334
|
+
'message'
|
|
2335
|
+
);
|
|
2336
|
+
assert.strictEqual(original.firstCall.thisValue, console);
|
|
2337
|
+
const expected =
|
|
2338
|
+
level === 'info'
|
|
2339
|
+
? expectedRecord(1, {type, level})
|
|
2340
|
+
: expectedRecord(1, {
|
|
2341
|
+
type: 'client',
|
|
2342
|
+
values: [
|
|
2343
|
+
`console ${method}: ${type === 'sdk' ? 'wx-js-sdk' : 'WebClient'},message`,
|
|
2344
|
+
],
|
|
2345
|
+
});
|
|
2346
|
+
|
|
2347
|
+
assert.deepEqual(webex.logger.getRecords(), [expected]);
|
|
2348
|
+
assert.strictEqual(webex.logger.formatLogs(), expected.body);
|
|
2349
|
+
assert.equal(capture.callCount, level === 'info' ? 0 : 1);
|
|
2350
|
+
assert.equal(webex.logger.recordState.lastSequence, 1);
|
|
2351
|
+
} finally {
|
|
2352
|
+
console[method] = normal;
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
it('keeps host capture for printed-only logs enabled by the developer feature', () => {
|
|
2359
|
+
delete webex.logger.config.level;
|
|
2360
|
+
webex.logger.config.bufferLogLevel = 'info';
|
|
2361
|
+
webex.internal.device = {features: {developer: {get: () => 'debug'}}};
|
|
2362
|
+
const output = configureConsoleMethod('debug');
|
|
2363
|
+
const normal = console.debug;
|
|
2364
|
+
|
|
2365
|
+
console.debug = (...args) => {
|
|
2366
|
+
webex.logger.client_logToBuffer(`console debug: ${args.toString()}`);
|
|
2367
|
+
output.apply(console, args);
|
|
2368
|
+
};
|
|
2369
|
+
try {
|
|
2370
|
+
webex.logger.debug('message');
|
|
2371
|
+
assert.calledOnceWithExactly(output, 'wx-js-sdk', 'message');
|
|
2372
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2373
|
+
expectedRecord(1, {
|
|
2374
|
+
type: 'client',
|
|
2375
|
+
values: ['console debug: wx-js-sdk,message'],
|
|
2376
|
+
}),
|
|
2377
|
+
]);
|
|
2378
|
+
} finally {
|
|
2379
|
+
console.debug = normal;
|
|
2380
|
+
}
|
|
2381
|
+
});
|
|
2382
|
+
|
|
2383
|
+
it('keeps the existing stringify fallback without exposing metadata in text', () => {
|
|
2384
|
+
const metadata = {eventName: 'event'};
|
|
2385
|
+
const value = {
|
|
2386
|
+
metadata,
|
|
2387
|
+
toJSON() {
|
|
2388
|
+
throw new Error('stringify failed');
|
|
2389
|
+
},
|
|
2390
|
+
};
|
|
2391
|
+
|
|
2392
|
+
webex.logger.info(value);
|
|
2393
|
+
assert.deepEqual(webex.logger.getRecords(), [
|
|
2394
|
+
expectedRecord(1, {metadata, values: ['Failed to stringify: [object Object]']}),
|
|
2395
|
+
]);
|
|
2396
|
+
assert.strictEqual(value.metadata, metadata);
|
|
2397
|
+
});
|
|
2398
|
+
|
|
2399
|
+
it('contains an argument getter failure before accepting a record', () => {
|
|
2400
|
+
const failure = new Error('metadata getter failed');
|
|
2401
|
+
const value = Object.defineProperty({}, 'metadata', {
|
|
2402
|
+
enumerable: true,
|
|
2403
|
+
get() {
|
|
2404
|
+
throw failure;
|
|
2405
|
+
},
|
|
2406
|
+
});
|
|
2407
|
+
|
|
2408
|
+
assert.doesNotThrow(() => webex.logger.info(value));
|
|
2409
|
+
assert.deepEqual(webex.logger.getRecords(), []);
|
|
2410
|
+
assert.equal(webex.logger.recordState.lastSequence, 0);
|
|
2411
|
+
assert.calledWithExactly(console.warn, 'failed to execute Logger#info', failure);
|
|
2412
|
+
});
|
|
2413
|
+
});
|
|
2414
|
+
/* eslint-enable no-loop-func, no-console */
|
|
1268
2415
|
});
|