-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestLogger.js
158 lines (145 loc) · 5.44 KB
/
testLogger.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* eslint-env mocha */
/* eslint padded-blocks: 0 max-len: 0 */
const { assert } = require('chai');
const expect = require('chai').expect;
const StdoutCapture = require('./helpers/stdout');
const runServer = require('./helpers/testHelper').runServer;
const createLogger = require('../logger');
const stdout = new StdoutCapture();
describe('logger', () => {
it('should export function', () => {
assert.isFunction(createLogger);
});
it('should return instance of logger', () => {
const logger = createLogger({ name: 'Test' });
assert.isObject(logger);
assert.isFunction(logger.info);
});
it('should return instance of logger for td-agent forward', () => {
runServer({}, (server, finish) => {
const logger = createLogger({ name: 'Test', output: 'td-agent-forward', port: server.port });
assert.isObject(logger);
assert.isFunction(logger.info);
logger.info({ metadata: { type: 'test' } }, 'Test message');
setTimeout(() => {
finish(() => {
});
}, 1000);
});
});
it('should log to stdout', () => {
const logger = createLogger({ name: 'Test-stdout' });
stdout.capture();
logger.info({ metadata: { type: 'test' } }, 'Test message');
const output = stdout.stop();
assert.lengthOf(output, 1, 'it should output a single line');
});
it('should log to td-agent forward', (done) => {
runServer({}, (server, finish) => {
const logger = createLogger({ name: 'Test-forward', output: 'td-agent-forward', port: server.port });
logger.info({ metadata: { type: 'test' } }, 'Test message');
setTimeout(() => {
finish((data) => {
expect(data[0].tag).to.be.equal('application.logs');
done();
});
}, 1000);
});
});
it('should log in a json encoded output', () => {
const name = 'Test-json';
const logger = createLogger({ name });
stdout.capture();
logger.info({ metadata: { type: 'test' } }, 'Test message');
const output = stdout.stop();
try {
JSON.parse(output[0]);
} catch (err) {
assert.fail(null, null, 'Could not parse output as valid JSON');
}
});
it('should log in a json encoded outout to td-agent forward', (done) => {
runServer({}, (server, finish) => {
const logger = createLogger({ name: 'Test-forward', output: 'td-agent-forward', port: server.port });
logger.info({ metadata: { type: 'test' } }, 'Test message');
setTimeout(() => {
finish((data) => {
try {
JSON.parse(data[0].data.message);
} catch (err) {
assert.fail(null, null, 'Could not parse output as valid JSON');
}
done();
});
}, 1000);
});
});
it('should log correctly structured json', () => {
const name = 'Test-json';
const type = 'test';
const msg = 'Test message';
const logger = createLogger({ name });
stdout.capture();
logger.info({ metadata: { type } }, msg);
const output = stdout.stop();
const outputData = JSON.parse(output[0]);
assert.equal(outputData.name, name);
assert.equal(outputData.metadata.type, type);
assert.equal(outputData.msg, msg);
});
it('should log correctly structured json to td-agent forward', (done) => {
const name = 'Test-json';
const type = 'test';
const msg = 'Test message';
runServer({}, (server, finish) => {
const logger = createLogger({ name, output: 'td-agent-forward', port: server.port });
logger.info({ metadata: { type } }, msg);
setTimeout(() => {
finish((data) => {
const outputData = JSON.parse(data[0].data.message);
assert.equal(outputData.name, name);
assert.equal(outputData.metadata.type, type);
assert.equal(outputData.msg, msg);
done();
});
}, 1000);
});
});
it('should log a timestamp field', () => {
const name = 'Test-timestamp';
const logger = createLogger({ name });
stdout.capture();
const before = new Date();
logger.info({ metadata: { type: 'test' } }, 'Test message');
const after = new Date();
const output = stdout.stop();
const outputData = JSON.parse(output[0]);
assert.property(outputData, 'timestamp');
assert.isString(outputData.timestamp, 'timestamp should be json string');
const timestamp = new Date(outputData.timestamp);
assert.instanceOf(timestamp, Date, 'timestamp can be parsed');
assert.isAtLeast(timestamp, before, 'should be in range');
assert.isAtMost(timestamp, after, 'should be in range');
});
it('should log a timestamp field to td-agent forward', (done) => {
const name = 'Test-timestamp';
runServer({}, (server, finish) => {
const logger = createLogger({ name, output: 'td-agent-forward', port: server.port });
const before = new Date();
logger.info({ metadata: { type: 'test' } }, 'Test message');
const after = new Date();
setTimeout(() => {
finish((data) => {
const outputData = JSON.parse(data[0].data.message);
assert.property(outputData, 'timestamp');
assert.isString(outputData.timestamp, 'timestamp should be json string');
const timestamp = new Date(outputData.timestamp);
assert.instanceOf(timestamp, Date, 'timestamp can be parsed');
assert.isAtLeast(timestamp, before, 'should be in range');
assert.isAtMost(timestamp, after, 'should be in range');
done();
});
}, 1000);
});
});
});