-
Notifications
You must be signed in to change notification settings - Fork 45
/
loggers.test.js
235 lines (195 loc) · 7.16 KB
/
loggers.test.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
const should = require('should');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const sleep = require('ko-sleep');
const coffee = require('coffee');
const Loggers = require('../../../index').EggLoggers;
describe('test/egg/loggers.test.js', () => {
const tmp = path.join(__dirname, '../../fixtures/tmp');
const aLog = path.join(tmp, 'a.log');
const bLog = path.join(tmp, 'b.log');
const cLog = path.join(tmp, 'c.log');
const dLog = path.join(tmp, 'd.log');
const eLog = path.join(tmp, 'e.log');
const fLog = 'f.log';
describe('application', () => {
let loggers;
before(() => {
loggers = new Loggers({
logger: {
type: 'application',
dir: tmp,
appLogName: 'app-web.log',
coreLogName: 'egg-web.log',
agentLogName: 'egg-agent.log',
errorLogName: 'common-error.log',
buffer: false,
coreLogger: {
level: 'WARN',
consoleLevel: 'WARN',
},
},
customLogger: {
aLogger: {
file: aLog,
},
bLogger: {
file: bLog,
},
cLogger: {
file: cLog,
eol: '',
},
dLogger: {
file: dLog,
concentrateError: 'redirect',
},
eLogger: {
file: eLog,
concentrateError: 'ignore',
},
fLogger: {
file: fLog,
},
},
});
});
after(() => {
rimraf.sync(tmp);
});
it('loggers can create multi logger instance', () => {
should.exists(loggers.logger);
should.exists(loggers.coreLogger);
should.exists(loggers.errorLogger);
should.exists(loggers.aLogger);
should.exists(loggers.bLogger);
});
it('all logger.error will duplicate to errorLogger', function* () {
loggers.logger.error('this is logger error foo1');
loggers.coreLogger.error('this is coreLogger error foo1');
loggers.aLogger.error('this is aLogger error foo1');
loggers.bLogger.error('this is bLogger error foo1');
loggers.dLogger.error('this is dLogger error foo1');
loggers.eLogger.error('this is eLogger error foo1');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'common-error.log'), 'utf8');
assert(content.includes('this is logger error foo1'));
assert(content.includes('this is coreLogger error foo1'));
assert(content.includes('this is aLogger error foo1'));
assert(content.includes('this is bLogger error foo1'));
// should duplicate
assert(fs.readFileSync(path.join(tmp, 'app-web.log'), 'utf-8').includes('this is logger error foo1'));
assert(fs.readFileSync(path.join(tmp, 'egg-web.log'), 'utf-8').includes('this is coreLogger error foo1'));
assert(fs.readFileSync(aLog, 'utf-8').includes('this is aLogger error foo1'));
assert(fs.readFileSync(bLog, 'utf-8').includes('this is bLogger error foo1'));
// should redirect
assert(content.includes('this is dLogger error foo1'));
assert(fs.readFileSync(dLog, 'utf-8') === '');
// should only write to self
assert(!content.includes('this is eLogger error foo1'));
assert(fs.readFileSync(eLog, 'utf-8').includes('this is eLogger error foo1'));
assert(loggers.cLogger.options.concentrateError === 'duplicate');
assert(loggers.dLogger.options.concentrateError === 'redirect');
assert(loggers.eLogger.options.concentrateError === 'ignore');
});
it('should app.logger log to appLogName', function*() {
loggers.logger.info('logger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'app-web.log'), 'utf8');
content.should.containEql('logger info foo');
});
it('should app.coreLogger warn log to coreLogName', function*() {
loggers.coreLogger.warn('coreLogger warn foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'egg-web.log'), 'utf8');
content.should.containEql('coreLogger warn foo');
});
it('should support coreLogger level=WARN', function*() {
loggers.coreLogger.info('coreLogger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'egg-web.log'), 'utf8');
content.should.not.containEql('coreLogger info foo');
});
it('should aLogger log to a.log', function*() {
loggers.aLogger.info('aLogger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'a.log'), 'utf8');
content.should.containEql('aLogger info foo');
});
it('should bLogger log to b.log', function*() {
loggers.bLogger.info('bLogger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'b.log'), 'utf8');
content.should.containEql('bLogger info foo');
});
it('cLogger dont contains eol', function*() {
loggers.cLogger.info('cLogger info foo');
loggers.cLogger.info('cLogger info bar');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'c.log'), 'utf8');
content.should.not.containEql('\n');
});
it('should fLogger log to f.log with relative config', function*() {
loggers.fLogger.info('fLogger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'f.log'), 'utf8');
content.should.containEql('fLogger info foo');
});
it('reload all logger', done => {
loggers.reload();
setTimeout(done, 500);
});
});
describe('agent', () => {
let loggers;
before(() => {
loggers = new Loggers({
logger: {
type: 'agent',
dir: tmp,
appLogName: 'app-web.log',
coreLogName: 'egg-web.log',
agentLogName: 'egg-agent.log',
errorLogName: 'common-error.log',
buffer: false,
eol: '\r',
coreLogger: {
level: 'WARN',
consoleLevel: 'WARN',
},
},
});
});
after(() => {
rimraf.sync(tmp);
});
it('loggers.logger alias to loggers.coreLogger', () => {
loggers.logger.options.file.should.equal(loggers.coreLogger.options.file);
});
it('should agent.coreLogger log to agentLogName', function*() {
loggers.logger.info('logger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'egg-agent.log'), 'utf8');
content.should.containEql('logger info foo');
content.should.match(/foo\r$/);
});
it('should support coreLogger level=WARN', function*() {
loggers.coreLogger.info('coreLogger info foo');
yield sleep(10);
const content = fs.readFileSync(path.join(tmp, 'egg-agent.log'), 'utf8');
content.should.not.containEql('coreLogger info foo');
});
});
describe('console', () => {
it('should disable console log', done => {
const loggerFile = path.join(__dirname, '../../fixtures/egg_loggers.js');
coffee.fork(loggerFile)
.expect('stdout', /info foo/)
.notExpect('stdout', /info foo after disable/)
.end(done);
});
});
});