mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
chore(merge): merging from master
This commit is contained in:
commit
f1a7d57931
@ -12,3 +12,5 @@ lib-cov
|
||||
coverage.html
|
||||
Makefile
|
||||
coverage
|
||||
Gemfile
|
||||
Gemfile.lock
|
||||
|
||||
@ -78,8 +78,8 @@ function setLevelForCategory(category, level) {
|
||||
debug(`setLevelForCategory: found ${categoryConfig} for ${category}`);
|
||||
if (!categoryConfig) {
|
||||
const sourceCategoryConfig = configForCategory(category);
|
||||
debug('setLevelForCategory: no config found for category, found ' +
|
||||
`${sourceCategoryConfig} for parents of ${category}`);
|
||||
debug('setLevelForCategory: no config found for category, ' +
|
||||
`found ${sourceCategoryConfig} for parents of ${category}`);
|
||||
categoryConfig = { appenders: sourceCategoryConfig.appenders };
|
||||
}
|
||||
categoryConfig.level = level;
|
||||
@ -204,6 +204,9 @@ function configure(configurationFileOrObject) {
|
||||
LoggingEvent = loggerModule.LoggingEvent;
|
||||
module.exports.connectLogger = connectModule(config.levels).connectLogger;
|
||||
|
||||
// just in case configure is called after shutdown
|
||||
process.removeListener('message', receiver);
|
||||
cluster.removeListener('message', receiver);
|
||||
if (config.disableClustering) {
|
||||
debug('Not listening for cluster messages, because clustering disabled.');
|
||||
} else if (isPM2Master()) {
|
||||
@ -211,11 +214,9 @@ function configure(configurationFileOrObject) {
|
||||
// PM2 runs everything as workers - install pm2-intercom for this to work.
|
||||
// we only want one of the app instances to write logs
|
||||
debug('listening for PM2 broadcast messages');
|
||||
process.removeListener('message', receiver);
|
||||
process.on('message', receiver);
|
||||
} else if (cluster.isMaster) {
|
||||
debug('listening for cluster messages');
|
||||
cluster.removeListener('message', receiver);
|
||||
cluster.on('message', receiver);
|
||||
} else {
|
||||
debug('not listening for messages, because we are not a master process');
|
||||
|
||||
7457
package-lock.json
generated
7457
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -38,10 +38,10 @@
|
||||
"lib": "lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"date-format": "^1.1.0",
|
||||
"debug": "^2.6.8",
|
||||
"date-format": "^1.2.0",
|
||||
"debug": "^3.1.0",
|
||||
"semver": "^5.3.0",
|
||||
"streamroller": "^0.6.0"
|
||||
"streamroller": "^0.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"codecov": "^3.0.0",
|
||||
|
||||
@ -32,7 +32,7 @@ test('../../lib/appenders/dateFile', (batch) => {
|
||||
t.include(contents, 'This should be in the file');
|
||||
t.match(
|
||||
contents,
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
@ -124,7 +124,7 @@ test('../../lib/appenders/dateFile', (batch) => {
|
||||
t.equal(fileContents.split(EOL).length, 4);
|
||||
t.match(
|
||||
fileContents,
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
|
||||
@ -36,7 +36,7 @@ test('log4js fileAppender', (batch) => {
|
||||
t.include(fileContents, `This should be in the file.${EOL}`);
|
||||
t.match(
|
||||
fileContents,
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
@ -63,7 +63,7 @@ test('log4js fileAppender', (batch) => {
|
||||
t.equal(fileContents.split(EOL).length, 4);
|
||||
t.match(
|
||||
fileContents,
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
|
||||
@ -35,7 +35,7 @@ test('log4js fileSyncAppender', (batch) => {
|
||||
t.include(fileContents, `This should be in the file.${EOL}`);
|
||||
t.match(
|
||||
fileContents,
|
||||
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
@ -60,11 +60,7 @@ test('log4js fileSyncAppender', (batch) => {
|
||||
type: 'fileSync', filename: testFile, maxLogSize: 100, backups: 0
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: {
|
||||
appenders: ['sync'], level: 'debug'
|
||||
}
|
||||
}
|
||||
categories: { default: { appenders: ['sync'], level: 'debug' } }
|
||||
});
|
||||
logger.info('This is the first log message.');
|
||||
logger.info('This is an intermediate log message.');
|
||||
|
||||
@ -76,7 +76,9 @@ const setupLogging = function (options, category, compressedLength) {
|
||||
exitHandler = handler;
|
||||
}
|
||||
},
|
||||
env: {}
|
||||
removeListener: () => {},
|
||||
env: {},
|
||||
stderr: process.stderr
|
||||
},
|
||||
console: fakeConsole
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ test('log4js layouts', (batch) => {
|
||||
|
||||
assert.equal(
|
||||
output,
|
||||
'\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense'
|
||||
'\x1B[31m[2010-12-05T14:18:30.045] [ERROR] cheese - \x1B[39mnonsense'
|
||||
);
|
||||
assert.end();
|
||||
});
|
||||
@ -47,7 +47,7 @@ test('log4js layouts', (batch) => {
|
||||
colour: 'red'
|
||||
}
|
||||
});
|
||||
assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
|
||||
assert.equal(output, '\x1B[31m[2010-12-05T14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
@ -151,7 +151,7 @@ test('log4js layouts', (batch) => {
|
||||
}
|
||||
};
|
||||
|
||||
t.equal(layout(event), '[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test');
|
||||
t.equal(layout(event), '[2010-12-05T14:18:30.045] [DEBUG] tests - this is a test');
|
||||
|
||||
t.test('should output a stacktrace, message if the event has an error attached', (assert) => {
|
||||
let i;
|
||||
@ -166,7 +166,7 @@ test('log4js layouts', (batch) => {
|
||||
assert.equal(lines.length, stack.length);
|
||||
assert.equal(
|
||||
lines[0],
|
||||
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error'
|
||||
'[2010-12-05T14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error'
|
||||
);
|
||||
for (i = 1; i < stack.length; i++) {
|
||||
assert.equal(lines[i], stack[i]);
|
||||
@ -175,7 +175,7 @@ test('log4js layouts', (batch) => {
|
||||
assert.equal(lines.length - 1, stack.length);
|
||||
assert.equal(
|
||||
lines[0],
|
||||
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]'
|
||||
'[2010-12-05T14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]'
|
||||
);
|
||||
for (i = 1; i < stack.length; i++) {
|
||||
assert.equal(lines[i + 2], stack[i + 1]);
|
||||
@ -194,7 +194,7 @@ test('log4js layouts', (batch) => {
|
||||
const output = layout(event);
|
||||
assert.equal(
|
||||
output,
|
||||
'[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test ' +
|
||||
'[2010-12-05T14:18:30.045] [DEBUG] tests - this is a test ' +
|
||||
"{ name: 'Cheese', message: 'Gorgonzola smells.' }"
|
||||
);
|
||||
assert.end();
|
||||
@ -287,13 +287,13 @@ test('log4js layouts', (batch) => {
|
||||
});
|
||||
|
||||
t.test('%d should output the date in ISO8601 format', (assert) => {
|
||||
testPattern(assert, layout, event, tokens, '%d', '2010-12-05 14:18:30.045');
|
||||
testPattern(assert, layout, event, tokens, '%d', '2010-12-05T14:18:30.045');
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%d should allow for format specification', (assert) => {
|
||||
testPattern(assert, layout, event, tokens, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30.045-0000');
|
||||
testPattern(assert, layout, event, tokens, '%d{ISO8601}', '2010-12-05 14:18:30.045');
|
||||
testPattern(assert, layout, event, tokens, '%d{ISO8601}', '2010-12-05T14:18:30.045');
|
||||
testPattern(assert, layout, event, tokens, '%d{ABSOLUTE}', '14:18:30.045');
|
||||
testPattern(assert, layout, event, tokens, '%d{DATE}', '05 12 2010 14:18:30.045');
|
||||
testPattern(assert, layout, event, tokens, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
const test = require('tap').test;
|
||||
const cluster = require('cluster');
|
||||
const debug = require('debug')('log4js:pm2-test');
|
||||
|
||||
// PM2 runs everything as workers
|
||||
// - no master in the cluster (PM2 acts as master itself)
|
||||
@ -14,46 +15,54 @@ if (cluster.isMaster) {
|
||||
cluster.fork({ NODE_APP_INSTANCE: i });
|
||||
});
|
||||
|
||||
cluster.on('message', (worker, msg) => {
|
||||
const messageHandler = (worker, msg) => {
|
||||
if (worker.type || worker.topic) {
|
||||
msg = worker;
|
||||
}
|
||||
if (msg.type === 'testing') {
|
||||
debug(`Received testing message from ${msg.instance} with events ${msg.events}`);
|
||||
appEvents[msg.instance] = msg.events;
|
||||
}
|
||||
|
||||
// we have to do the re-broadcasting that the pm2-intercom module would do.
|
||||
if (msg.topic === 'log4js:message') {
|
||||
debug(`Received log message ${msg}`);
|
||||
for (const id in cluster.workers) {
|
||||
cluster.workers[id].send(msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
cluster.on('message', messageHandler);
|
||||
|
||||
let count = 0;
|
||||
cluster.on('exit', () => {
|
||||
count += 1;
|
||||
if (count === 2) {
|
||||
test('PM2 Support', (batch) => {
|
||||
batch.test('should not get any events when turned off', (t) => {
|
||||
t.notOk(appEvents['0'].filter(e => e && e.data[0].indexOf('will not be logged') > -1).length);
|
||||
t.notOk(appEvents['1'].filter(e => e && e.data[0].indexOf('will not be logged') > -1).length);
|
||||
t.end();
|
||||
});
|
||||
// wait for any IPC messages still to come, because it seems they are slooooow.
|
||||
setTimeout(() => {
|
||||
test('PM2 Support', (batch) => {
|
||||
batch.test('should not get any events when turned off', (t) => {
|
||||
t.notOk(appEvents['0'].filter(e => e && e.data[0].indexOf('will not be logged') > -1).length);
|
||||
t.notOk(appEvents['1'].filter(e => e && e.data[0].indexOf('will not be logged') > -1).length);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('should get events on app instance 0', (t) => {
|
||||
t.equal(appEvents['0'].length, 2);
|
||||
t.equal(appEvents['0'][0].data[0], 'this should now get logged');
|
||||
t.equal(appEvents['0'][1].data[0], 'this should now get logged');
|
||||
t.end();
|
||||
});
|
||||
batch.test('should get events on app instance 0', (t) => {
|
||||
t.equal(appEvents['0'].length, 2);
|
||||
t.equal(appEvents['0'][0].data[0], 'this should now get logged');
|
||||
t.equal(appEvents['0'][1].data[0], 'this should now get logged');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('should not get events on app instance 1', (t) => {
|
||||
t.equal(appEvents['1'].length, 0);
|
||||
t.end();
|
||||
batch.test('should not get events on app instance 1', (t) => {
|
||||
t.equal(appEvents['1'].length, 0);
|
||||
t.end();
|
||||
});
|
||||
batch.end();
|
||||
cluster.removeListener('message', messageHandler);
|
||||
});
|
||||
batch.end();
|
||||
});
|
||||
}, 100);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@ -67,21 +76,30 @@ if (cluster.isMaster) {
|
||||
const logger = log4js.getLogger('test');
|
||||
logger.info('this is a test, but without enabling PM2 support it will not be logged');
|
||||
|
||||
// we have to wait a bit, so that the process.send messages get a chance to propagate
|
||||
// IPC messages can take a while to get through to start with.
|
||||
setTimeout(() => {
|
||||
log4js.configure({
|
||||
appenders: { out: { type: 'recording' } },
|
||||
categories: { default: { appenders: ['out'], level: 'info' } },
|
||||
pm2: true
|
||||
});
|
||||
const anotherLogger = log4js.getLogger('test');
|
||||
anotherLogger.info('this should now get logged');
|
||||
}, 500);
|
||||
log4js.shutdown(() => {
|
||||
log4js.configure({
|
||||
appenders: { out: { type: 'recording' } },
|
||||
categories: { default: { appenders: ['out'], level: 'info' } },
|
||||
pm2: true
|
||||
});
|
||||
const anotherLogger = log4js.getLogger('test');
|
||||
setTimeout(() => {
|
||||
anotherLogger.info('this should now get logged');
|
||||
}, 100);
|
||||
|
||||
// we have to wait a bit, so that the process.send messages get a chance to propagate
|
||||
setTimeout(() => {
|
||||
const events = recorder.replay();
|
||||
process.send({ type: 'testing', instance: process.env.NODE_APP_INSTANCE, events: events });
|
||||
cluster.worker.disconnect();
|
||||
}, 2500);
|
||||
// if we're the pm2-master we should wait for the other process to send its log messages
|
||||
setTimeout(() => {
|
||||
log4js.shutdown(() => {
|
||||
const events = recorder.replay();
|
||||
debug(`Sending test events ${events} from ${process.env.NODE_APP_INSTANCE}`);
|
||||
process.send(
|
||||
{ type: 'testing', instance: process.env.NODE_APP_INSTANCE, events: events },
|
||||
() => { setTimeout(() => { cluster.worker.disconnect(); }, 100); }
|
||||
);
|
||||
});
|
||||
}, 300);
|
||||
});
|
||||
}, 200);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user