-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogging.test.default.ts
More file actions
109 lines (94 loc) · 3.06 KB
/
logging.test.default.ts
File metadata and controls
109 lines (94 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//===----------------------------------------------------------------------===//
//
// This file is Modular Inc proprietary.
//
//===----------------------------------------------------------------------===//
import * as assert from 'assert';
import { LogChannel, LogLevel } from './logging';
function createLogSpy(): [string[], (level: string, message: string) => void] {
const lines: string[] = [];
return [
lines,
(_level: string, message: string) => {
lines.push(message);
},
];
}
suite('Logging', () => {
test('logs should respect output levels', () => {
const channel = new LogChannel('Test Channel');
const [lines, callback] = createLogSpy();
channel.logCallback = callback;
channel.setOutputLevel(LogLevel.None);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, []);
lines.length = 0;
channel.setOutputLevel(LogLevel.Error);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, ['error']);
lines.length = 0;
channel.setOutputLevel(LogLevel.Warn);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, ['error', 'warn']);
lines.length = 0;
channel.setOutputLevel(LogLevel.Info);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, ['error', 'warn', 'info']);
lines.length = 0;
channel.setOutputLevel(LogLevel.Debug);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, ['error', 'warn', 'info', 'debug']);
lines.length = 0;
channel.setOutputLevel(LogLevel.Trace);
channel.error('error');
channel.warn('warn');
channel.info('info');
channel.debug('debug');
channel.trace('trace');
assert.deepStrictEqual(lines, ['error', 'warn', 'info', 'debug', 'trace']);
lines.length = 0;
});
test('data should be logged as JSON', () => {
const channel = new LogChannel('Test Channel');
const [lines, callback] = createLogSpy();
channel.logCallback = callback;
channel.setOutputLevel(LogLevel.Info);
channel.info('message', { foo: 123, bar: true, baz: [1, 2, 3] });
assert.equal(lines.length, 2);
assert.equal(lines[0], 'message');
const json = JSON.parse(lines[1]);
assert.deepStrictEqual(json, {
foo: 123,
bar: true,
baz: [1, 2, 3],
});
});
test('data should respect log level', () => {
const channel = new LogChannel('Test Channel');
const [lines, callback] = createLogSpy();
channel.logCallback = callback;
channel.setOutputLevel(LogLevel.Info);
channel.debug('message', { foo: 123 });
assert.deepStrictEqual(lines, []);
});
});