-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.js
145 lines (129 loc) · 3.32 KB
/
unit.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
var assert = require("chai").assert,
path = require("path"),
fs = require("fs"),
Tail = require("tail").Tail,
randomstring = require("randomstring"),
TIMEOUT = 100,
time;
// Configure Logger
var logPath = path.join(__dirname, "logs/test.log");
config = {
name: "Common log test",
streams: [{
path: logPath,
level: "trace"
}]
};
require("../index.js").init(config);
var commonlog = require("../index.js"),
log = commonlog.logger,
logTime = commonlog.logTime;
// Helper method
var emptyLog = function (callback) {
fs.writeFile(logPath, "", function (error) {
if (error) throw error;
callback();
});
}
describe("Writing to log path", function () {
var tail, testMessage;
beforeEach(function (done) {
testMessage = randomstring.generate();
emptyLog(done);
});
afterEach(function (done) {
tail.unwatch();
done();
});
after(function (done) {
emptyLog(done);
});
it ("should write trace to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 10);
assert.equal(logLine.msg, testMessage);
done();
});
// Let Tail catch its breath
setTimeout(function () {
log.trace(testMessage);
}, TIMEOUT);
});
it ("should write trace to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 20);
assert.equal(logLine.msg, testMessage);
done();
});
setTimeout(function () {
log.debug(testMessage);
}, TIMEOUT);
});
it ("should write info to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 30);
assert.equal(logLine.msg, testMessage);
done();
});
setTimeout(function () {
log.info(testMessage);
}, TIMEOUT);
});
it ("should write warnings to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 40);
assert.equal(logLine.msg, testMessage);
done();
});
setTimeout(function () {
log.warn(testMessage);
}, TIMEOUT);
});
it ("should write errors to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 50);
assert.equal(logLine.msg, testMessage);
done();
});
setTimeout(function () {
log.error(testMessage);
}, TIMEOUT);
});
it ("should write fatals to log path", function (done) {
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 60);
assert.equal(logLine.msg, testMessage);
done();
});
setTimeout(function () {
log.fatal(testMessage);
}, TIMEOUT);
});
it ("should log time", function (done) {
time = process.hrtime();
tail = new Tail(logPath);
tail.on("line", function (data) {
var logLine = JSON.parse(data);
assert.equal(logLine.level, 20);
assert.equal(logLine.message, testMessage);
assert.equal(logLine.secondMessage, testMessage);
assert.isNumber(logLine.time);
done();
});
setTimeout(function () {
logTime(time, {message: testMessage, secondMessage: testMessage});
}, TIMEOUT);
});
});