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
|
'use strict';
const Fs = require('fs');
const Tap = require('tap');
const FileLogger = require('../../lib/loggers/file');
// Prepare the test
const rawFile = './raw.log';
const formatterFile = './formatter.log';
const formatterString = 'example';
const logObjectA = {
_timestamp: Date.now() + .134,
_cologneLog: '1.0.0',
_from: 'Dummy Logger',
_level: 6,
_levelString: 'info',
message: 'MessageOne'
};
const logObjectB = {
_timestamp: Date.now() + .134,
_cologneLog: '1.0.0',
_from: 'Dummy Logger',
_level: 6,
_levelString: 'info',
message: 'MessageTwo'
};
const logs = [logObjectA, logObjectB];
const dummyFormatter = {
values: [],
format: function (logObject) {
this.values.push(logObject);
return formatterString;
}
};
const rawLogger = new FileLogger({
file: rawFile
});
const formatterLogger = new FileLogger({
file: formatterFile,
formatter: dummyFormatter
});
/**
* TEST: #log() - regular
*/
rawLogger.log(...logs);
setTimeout(() => {
Tap.test('raw file', (t) => {
Fs.readFile(rawFile, { encoding: 'utf8' }, (_, contents) => {
const lines = contents.trim().split('\n');
Tap.equal(lines.length, logs.length,
'it should send all params to the file');
Tap.equal(JSON.stringify(logObjectA), lines[0],
'it should log the raw json object');
Fs.unlink(rawFile, () => {
t.end();
});
});
});
}, 10); // allow for flush
/**
* TEST: #log() - formatter
*/
formatterLogger.log(...logs);
setTimeout(() => {
Tap.test('formatted file', (t) => {
Fs.readFile(formatterFile, { encoding: 'utf8' }, (_, contents) => {
const lines = contents.trim().split('\n');
Tap.equal(lines.length, logs.length,
'it should send all params to the file');
Tap.equal(formatterString, lines[0],
'it should log the formatted object');
Fs.unlink(formatterFile, () => {
t.end();
});
});
});
}, 10);
|