chore(lint): added linting to test files

This commit is contained in:
Gareth Jones 2017-11-14 08:45:22 +11:00
parent b65871a4ab
commit 906a77403d
16 changed files with 276 additions and 322 deletions

View File

@ -8,7 +8,7 @@ const debug = require('debug')('log4js:configuration');
let cluster;
try {
cluster = require('cluster'); // eslint-disable-line global-require
cluster = require('cluster'); // eslint-disable-line global-require
} catch (e) {
debug('Clustering support disabled because require(cluster) threw an error: ', e);
}
@ -36,14 +36,12 @@ function anInteger(thing) {
}
class Configuration {
throwExceptionIf(checks, message) {
const tests = Array.isArray(checks) ? checks : [checks];
tests.forEach((test) => {
if (test) {
throw new Error(
`Problem with log4js configuration: (${util.inspect(this.candidate, { depth: 5 })}) - ${message}`
);
throw new Error(`Problem with log4js configuration: (${util.inspect(this.candidate, { depth: 5 })})` +
` - ${message}`);
}
});
}

View File

@ -22,7 +22,7 @@ function getUrl(req) {
}
/**
/**
* Adds custom {token, replacement} objects to defaults,
* overwriting the defaults if any tokens clash
*
@ -37,8 +37,8 @@ function assembleTokens(req, res, customTokens) {
const a = array.concat();
for (let i = 0; i < a.length; ++i) {
for (let j = i + 1; j < a.length; ++j) {
// not === because token can be regexp object
/* eslint eqeqeq:0 */
// not === because token can be regexp object
/* eslint eqeqeq:0 */
if (a[i].token == a[j].token) {
a.splice(j--, 1);
}
@ -91,15 +91,15 @@ function assembleTokens(req, res, customTokens) {
token: /:res\[([^\]]+)]/g,
replacement: function (_, field) {
return res._headers ?
(res._headers[field.toLowerCase()] || res.__headers[field])
: (res.__headers && res.__headers[field]);
(res._headers[field.toLowerCase()] || res.__headers[field])
: (res.__headers && res.__headers[field]);
}
});
return arrayUniqueTokens(customTokens.concat(defaultTokens));
}
/**
/**
* Return formatted log line.
*
* @param {String} str
@ -114,7 +114,7 @@ function format(str, tokens) {
return str;
}
/**
/**
* Return RegExp Object about nolog
*
* @param {String|Array} nolog
@ -154,7 +154,7 @@ function createNoLogCondition(nolog) {
}
if (Array.isArray(nolog)) {
// convert to strings
// convert to strings
const regexpsAsStrings = nolog.map(reg => (reg.source ? reg.source : reg));
regexp = new RegExp(regexpsAsStrings.join('|'));
}

View File

@ -112,10 +112,10 @@ function timestampLevelAndCategory(loggingEvent, colour, timezoneOffset) {
*/
function basicLayout(loggingEvent, timezoneOffset) {
return timestampLevelAndCategory(
loggingEvent,
undefined,
timezoneOffset
) + formatLogData(loggingEvent.data);
loggingEvent,
undefined,
timezoneOffset
) + formatLogData(loggingEvent.data);
}
/**
@ -124,10 +124,10 @@ function basicLayout(loggingEvent, timezoneOffset) {
*/
function colouredLayout(loggingEvent, timezoneOffset) {
return timestampLevelAndCategory(
loggingEvent,
loggingEvent.level.colour,
timezoneOffset
) + formatLogData(loggingEvent.data);
loggingEvent,
loggingEvent.level.colour,
timezoneOffset
) + formatLogData(loggingEvent.data);
}
function messagePassThroughLayout(loggingEvent) {

View File

@ -36,7 +36,6 @@ module.exports = function (customLevels) {
}
return this.level === otherLevel.level;
}
}
const defaultLevels = {

View File

@ -31,7 +31,7 @@ const layouts = require('./layouts');
let cluster;
try {
cluster = require('cluster'); // eslint-disable-line global-require
cluster = require('cluster'); // eslint-disable-line global-require
} catch (e) {
debug('Clustering support disabled because require(cluster) threw an error: ', e);
}
@ -78,9 +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;
@ -207,21 +206,19 @@ function configure(configurationFileOrObject) {
if (config.disableClustering) {
debug('Not listening for cluster messages, because clustering disabled.');
} else {
} else if (isPM2Master()) {
// PM2 cluster support
// PM2 runs everything as workers - install pm2-intercom for this to work.
// we only want one of the app instances to write logs
if (isPM2Master()) {
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');
}
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');
}
enabled = true;

View File

@ -6,7 +6,7 @@ const debug = require('debug')('log4js:logger');
let cluster;
try {
cluster = require('cluster'); // eslint-disable-line global-require
cluster = require('cluster'); // eslint-disable-line global-require
} catch (e) {
debug('Clustering support disabled because require(cluster) threw an error: ', e);
}

View File

@ -25,11 +25,10 @@
},
"scripts": {
"clean": "find test -type f ! -name '*.json' ! -name '*.js' ! -name '.eslintrc' -delete && rm *.log",
"lint": "eslint lib/ test/",
"prepush": "npm test",
"commitmsg": "validate-commit-msg",
"posttest": "npm run clean",
"pretest": "eslint lib/**/*",
"pretest": "eslint 'lib/**/*.js' 'test/**/*.js'",
"test": "tap 'test/tap/**/*.js'",
"coverage": "tap 'test/tap/**/*.js' --cov",
"codecov": "tap 'test/tap/**/*.js' --cov --coverage-report=lcov && codecov"

View File

@ -24,9 +24,8 @@ function testAppender(label) {
test('log4js configuration validation', (batch) => {
batch.test('should give error if config is just plain silly', (t) => {
[null, undefined, '', ' ', []].forEach((config) => {
const expectedError = new Error(
`Problem with log4js configuration: (${util.inspect(config)}) - must be an object.`
);
const expectedError =
new Error(`Problem with log4js configuration: (${util.inspect(config)}) - must be an object.`);
t.throws(
() => new Configuration(config),
expectedError
@ -37,34 +36,32 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if config is an empty object', (t) => {
const expectedError = new Error(
'Problem with log4js configuration: ({}) - must have a property "appenders" of type object.'
);
const expectedError =
new Error('Problem with log4js configuration: ({}) - must have a property "appenders" of type object.');
t.throws(() => new Configuration({}), expectedError);
t.end();
});
batch.test('should give error if config has no appenders', (t) => {
const expectedError = new Error(
'Problem with log4js configuration: ({ categories: {} }) - must have a property "appenders" of type object.'
);
const expectedError =
new Error('Problem with log4js configuration: ({ categories: {} }) ' +
'- must have a property "appenders" of type object.');
t.throws(() => new Configuration({ categories: {} }), expectedError);
t.end();
});
batch.test('should give error if config has no categories', (t) => {
const expectedError = new Error(
'Problem with log4js configuration: ({ appenders: {} }) - must have a property "categories" of type object.'
);
const expectedError =
new Error('Problem with log4js configuration: ({ appenders: {} }) ' +
'- must have a property "categories" of type object.');
t.throws(() => new Configuration({ appenders: {} }), expectedError);
t.end();
});
batch.test('should give error if appenders is not an object', (t) => {
const error = new Error(
'Problem with log4js configuration: ({ appenders: [], categories: [] })' +
' - must have a property "appenders" of type object.'
);
const error =
new Error('Problem with log4js configuration: ({ appenders: [], categories: [] })' +
' - must have a property "appenders" of type object.');
t.throws(
() => new Configuration({ appenders: [], categories: [] }),
error
@ -73,10 +70,9 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if appenders are not all valid', (t) => {
const error = new Error(
'Problem with log4js configuration: ({ appenders: { thing: \'cheese\' }, categories: {} })' +
' - appender "thing" is not valid (must be an object with property "type")'
);
const error =
new Error('Problem with log4js configuration: ({ appenders: { thing: \'cheese\' }, categories: {} })' +
' - appender "thing" is not valid (must be an object with property "type")');
t.throws(
() => new Configuration({ appenders: { thing: 'cheese' }, categories: {} }),
error
@ -85,10 +81,8 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should require at least one appender', (t) => {
const error = new Error(
'Problem with log4js configuration: ({ appenders: {}, categories: {} })' +
' - must define at least one appender.'
);
const error = new Error('Problem with log4js configuration: ({ appenders: {}, categories: {} })' +
' - must define at least one appender.');
t.throws(
() => new Configuration({ appenders: {}, categories: {} }),
error
@ -97,11 +91,9 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if categories are not all valid', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n categories: { thing: \'cheese\' } })' +
' - category "thing" is not valid (must be an object with properties "appenders" and "level")'
);
' - category "thing" is not valid (must be an object with properties "appenders" and "level")');
t.throws(
() => new Configuration({ appenders: { stdout: { type: 'stdout' } }, categories: { thing: 'cheese' } }),
error
@ -110,27 +102,24 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if default category not defined', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n' +
' categories: { thing: { appenders: [ \'stdout\' ], level: \'ERROR\' } } })' +
' - must define a "default" category.'
);
' - must define a "default" category.');
t.throws(
() => new Configuration({
appenders: { stdout: { type: 'stdout' } },
categories: { thing: { appenders: ['stdout'], level: 'ERROR' } } }
),
categories: { thing: { appenders: ['stdout'], level: 'ERROR' } }
}),
error
);
t.end();
});
batch.test('should require at least one category', (t) => {
const error = new Error(
'Problem with log4js configuration: ({ appenders: { stdout: { type: \'stdout\' } }, categories: {} })' +
' - must define at least one category.'
);
const error =
new Error('Problem with log4js configuration: ({ appenders: { stdout: { type: \'stdout\' } }, categories: {} })' +
' - must define at least one category.');
t.throws(
() => new Configuration({ appenders: { stdout: { type: 'stdout' } }, categories: {} }),
error
@ -139,12 +128,10 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if category.appenders is not an array', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n' +
' categories: { thing: { appenders: {}, level: \'ERROR\' } } })' +
' - category "thing" is not valid (appenders must be an array of appender names)'
);
' - category "thing" is not valid (appenders must be an array of appender names)');
t.throws(
() => new Configuration({
appenders: { stdout: { type: 'stdout' } },
@ -156,12 +143,10 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if category.appenders is empty', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n' +
' categories: { thing: { appenders: [], level: \'ERROR\' } } })' +
' - category "thing" is not valid (appenders must contain at least one appender name)'
);
' - category "thing" is not valid (appenders must contain at least one appender name)');
t.throws(
() => new Configuration({
appenders: { stdout: { type: 'stdout' } },
@ -173,12 +158,10 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if categories do not refer to valid appenders', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n' +
' categories: { thing: { appenders: [ \'cheese\' ], level: \'ERROR\' } } })' +
' - category "thing" is not valid (appender "cheese" is not defined)'
);
' - category "thing" is not valid (appender "cheese" is not defined)');
t.throws(
() => new Configuration({
appenders: { stdout: { type: 'stdout' } },
@ -190,13 +173,11 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if category level is not valid', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { stdout: { type: \'stdout\' } },\n' +
' categories: { default: { appenders: [ \'stdout\' ], level: \'Biscuits\' } } })' +
' - category "default" is not valid (level "Biscuits" not recognised; ' +
'valid levels are ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, MARK, OFF)'
);
'valid levels are ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, MARK, OFF)');
t.throws(
() => new Configuration({
appenders: { stdout: { type: 'stdout' } },
@ -208,12 +189,10 @@ test('log4js configuration validation', (batch) => {
});
batch.test('should give error if appender type cannot be found', (t) => {
const error = new Error(
'Problem with log4js configuration: ' +
const error = new Error('Problem with log4js configuration: ' +
'({ appenders: { thing: { type: \'cheese\' } },\n' +
' categories: { default: { appenders: [ \'thing\' ], level: \'ERROR\' } } })' +
' - appender "thing" is not valid (type "cheese" could not be found)'
);
' - appender "thing" is not valid (type "cheese" could not be found)');
t.throws(
() => new Configuration({
appenders: { thing: { type: 'cheese' } },
@ -278,9 +257,7 @@ test('log4js configuration validation', (batch) => {
sandboxConfig.requires[
`${path.join(mainPath, '../../node_modules/tap/node_modules/nyc/bin/cheese')}`
] = testAppender('correct');
const SandboxedConfiguration = sandbox.require(
'../../lib/configuration', sandboxConfig
);
const SandboxedConfiguration = sandbox.require('../../lib/configuration', sandboxConfig);
const config = new SandboxedConfiguration({
appenders: { thing: { type: 'cheese' } },

View File

@ -84,9 +84,13 @@ test('log4js fileAppender', (batch) => {
// log file of 100 bytes maximum, no backups
log4js.configure({
appenders: {
file: { type: 'file', filename: testFile, maxLogSize: 100, backups: 0 }
file: {
type: 'file', filename: testFile, maxLogSize: 100, backups: 0
}
},
categories: { default: { appenders: ['file'], level: 'debug' } }
categories: {
default: { appenders: ['file'], level: 'debug' }
}
});
logger.info('This is the first log message.');
@ -98,9 +102,7 @@ test('log4js fileAppender', (batch) => {
t.include(fileContents, 'This is the second log message.');
t.equal(fileContents.indexOf('This is the first log message.'), -1);
fs.readdir(__dirname, (e, files) => {
const logFiles = files.filter(
file => file.includes('fa-maxFileSize-test.log')
);
const logFiles = files.filter(file => file.includes('fa-maxFileSize-test.log'));
t.equal(logFiles.length, 2, 'should be 2 files');
t.end();
});
@ -124,7 +126,9 @@ test('log4js fileAppender', (batch) => {
// log file of 50 bytes maximum, 2 backups
log4js.configure({
appenders: {
file: { type: 'file', filename: testFile, maxLogSize: 50, backups: 2 }
file: {
type: 'file', filename: testFile, maxLogSize: 50, backups: 2
}
},
categories: { default: { appenders: ['file'], level: 'debug' } }
});
@ -136,9 +140,7 @@ test('log4js fileAppender', (batch) => {
// give the system a chance to open the stream
setTimeout(() => {
fs.readdir(__dirname, (err, files) => {
const logFiles = files.sort().filter(
file => file.includes('fa-maxFileSize-with-backups-test.log')
);
const logFiles = files.sort().filter(file => file.includes('fa-maxFileSize-with-backups-test.log'));
t.equal(logFiles.length, 3);
t.same(logFiles, [
'fa-maxFileSize-with-backups-test.log',
@ -184,7 +186,9 @@ test('log4js fileAppender', (batch) => {
// log file of 50 bytes maximum, 2 backups
log4js.configure({
appenders: {
file: { type: 'file', filename: testFile, maxLogSize: 50, backups: 2, compress: true }
file: {
type: 'file', filename: testFile, maxLogSize: 50, backups: 2, compress: true
}
},
categories: { default: { appenders: ['file'], level: 'debug' } }
});
@ -195,9 +199,7 @@ test('log4js fileAppender', (batch) => {
// give the system a chance to open the stream
setTimeout(() => {
fs.readdir(__dirname, (err, files) => {
const logFiles = files.sort().filter(
file => file.includes('fa-maxFileSize-with-backups-compressed-test.log')
);
const logFiles = files.sort().filter(file => file.includes('fa-maxFileSize-with-backups-compressed-test.log'));
t.equal(logFiles.length, 3, 'should be 3 files');
t.same(logFiles, [
'fa-maxFileSize-with-backups-compressed-test.log',

View File

@ -55,8 +55,16 @@ test('log4js fileSyncAppender', (batch) => {
// log file of 100 bytes maximum, no backups
log4js.configure({
appenders: { sync: { type: 'fileSync', filename: testFile, maxLogSize: 100, backups: 0 } },
categories: { default: { appenders: ['sync'], level: 'debug' } }
appenders: {
sync: {
type: 'fileSync', filename: testFile, maxLogSize: 100, backups: 0
}
},
categories: {
default: {
appenders: ['sync'], level: 'debug'
}
}
});
logger.info('This is the first log message.');
logger.info('This is an intermediate log message.');
@ -72,9 +80,7 @@ test('log4js fileSyncAppender', (batch) => {
t.test('there should be two test files', (assert) => {
fs.readdir(__dirname, (err, files) => {
const logFiles = files.filter(
file => file.includes('fa-maxFileSize-sync-test.log')
);
const logFiles = files.filter(file => file.includes('fa-maxFileSize-sync-test.log'));
assert.equal(logFiles.length, 2);
assert.end();
});
@ -97,7 +103,11 @@ test('log4js fileSyncAppender', (batch) => {
// log file of 50 bytes maximum, 2 backups
log4js.configure({
appenders: { sync: { type: 'fileSync', filename: testFile, maxLogSize: 50, backups: 2 } },
appenders: {
sync: {
type: 'fileSync', filename: testFile, maxLogSize: 50, backups: 2
}
},
categories: { default: { appenders: ['sync'], level: 'debug' } }
});
logger.info('This is the first log message.');
@ -108,9 +118,7 @@ test('log4js fileSyncAppender', (batch) => {
t.test('the log files', (assert) => {
assert.plan(5);
fs.readdir(__dirname, (err, files) => {
const logFiles = files.filter(
file => file.includes('fa-maxFileSize-with-backups-sync-test.log')
);
const logFiles = files.filter(file => file.includes('fa-maxFileSize-with-backups-sync-test.log'));
assert.equal(logFiles.length, 3, 'should be 3 files');
assert.same(logFiles, [
'fa-maxFileSize-with-backups-sync-test.log',
@ -136,11 +144,12 @@ test('log4js fileSyncAppender', (batch) => {
// this config defines one file appender (to ./tmp-sync-tests.log)
// and sets the log level for "tests" to WARN
log4js.configure({
appenders: { sync: {
type: 'fileSync',
filename: 'tmp-sync-tests.log',
layout: { type: 'messagePassThrough' }
}
appenders: {
sync: {
type: 'fileSync',
filename: 'tmp-sync-tests.log',
layout: { type: 'messagePassThrough' }
}
},
categories: {
default: { appenders: ['sync'], level: 'debug' },

View File

@ -318,7 +318,8 @@ test('log4js layouts', (batch) => {
});
t.test('should handle complicated patterns', (assert) => {
testPattern(assert, layout, event, tokens,
testPattern(
assert, layout, event, tokens,
'%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
`this is a test${EOL} of.tests at 14:18:30.045 cheese DEBUG${EOL}`
);

View File

@ -49,126 +49,108 @@ test('levels', (batch) => {
t.test('ALL', (assert) => {
const all = levels.ALL;
assertThat(assert, all).isLessThanOrEqualTo(
[
levels.ALL,
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, all).isNotGreaterThanOrEqualTo(
[
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, all).isLessThanOrEqualTo([
levels.ALL,
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, all).isNotGreaterThanOrEqualTo([
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, all).isEqualTo([levels.getLevel('ALL')]);
assertThat(assert, all).isNotEqualTo(
[
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, all).isNotEqualTo([
levels.TRACE,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assert.end();
});
t.test('TRACE', (assert) => {
const trace = levels.TRACE;
assertThat(assert, trace).isLessThanOrEqualTo(
[
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, trace).isLessThanOrEqualTo([
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, trace).isNotLessThanOrEqualTo([levels.ALL]);
assertThat(assert, trace).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]);
assertThat(assert, trace).isNotGreaterThanOrEqualTo(
[
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, trace).isNotGreaterThanOrEqualTo([
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, trace).isEqualTo([levels.getLevel('TRACE')]);
assertThat(assert, trace).isNotEqualTo(
[
levels.ALL,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, trace).isNotEqualTo([
levels.ALL,
levels.DEBUG,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assert.end();
});
t.test('DEBUG', (assert) => {
const debug = levels.DEBUG;
assertThat(assert, debug).isLessThanOrEqualTo(
[
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, debug).isLessThanOrEqualTo([
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, debug).isNotLessThanOrEqualTo([levels.ALL, levels.TRACE]);
assertThat(assert, debug).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]);
assertThat(assert, debug).isNotGreaterThanOrEqualTo(
[
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, debug).isNotGreaterThanOrEqualTo([
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assertThat(assert, debug).isEqualTo([levels.getLevel('DEBUG')]);
assertThat(assert, debug).isNotEqualTo(
[
levels.ALL,
levels.TRACE,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]
);
assertThat(assert, debug).isNotEqualTo([
levels.ALL,
levels.TRACE,
levels.INFO,
levels.WARN,
levels.ERROR,
levels.FATAL,
levels.MARK,
levels.OFF
]);
assert.end();
});

View File

@ -138,13 +138,13 @@ test('logstashUDP appender', (batch) => {
pattern: '%m'
}
});
setup.logger.log('trace', 'Log event #1');
setup.logger.log('trace', 'Log event #1');
const json = JSON.parse(setup.results.buffer.toString());
t.equal(json.fields.field1, 'value1');
t.equal(json.fields.field2, 'evaluated at runtime' );
const json = JSON.parse(setup.results.buffer.toString());
t.equal(json.fields.field1, 'value1');
t.equal(json.fields.field2, 'evaluated at runtime');
t.end();
t.end();
});
batch.test('extra fields should be added to the fields structure', (t) => {

View File

@ -7,7 +7,11 @@ const fs = require('fs');
test('multiFile appender', (batch) => {
batch.test('should write to multiple files based on the loggingEvent property', (t) => {
log4js.configure({
appenders: { multi: { type: 'multiFile', base: 'logs/', property: 'categoryName', extension: '.log' } },
appenders: {
multi: {
type: 'multiFile', base: 'logs/', property: 'categoryName', extension: '.log'
}
},
categories: { default: { appenders: ['multi'], level: 'info' } }
});
const loggerA = log4js.getLogger('A');
@ -23,7 +27,11 @@ test('multiFile appender', (batch) => {
batch.test('should write to multiple files based on loggingEvent.context properties', (t) => {
log4js.configure({
appenders: { multi: { type: 'multiFile', base: 'logs/', property: 'label', extension: '.log' } },
appenders: {
multi: {
type: 'multiFile', base: 'logs/', property: 'label', extension: '.log'
}
},
categories: { default: { appenders: ['multi'], level: 'info' } }
});
const loggerC = log4js.getLogger('cheese');
@ -41,7 +49,11 @@ test('multiFile appender', (batch) => {
batch.test('should fail silently if loggingEvent property has no value', (t) => {
log4js.configure({
appenders: { multi: { type: 'multiFile', base: 'logs/', property: 'label', extension: '.log' } },
appenders: {
multi: {
type: 'multiFile', base: 'logs/', property: 'label', extension: '.log'
}
},
categories: { default: { appenders: ['multi'], level: 'info' } }
});
const loggerE = log4js.getLogger();
@ -62,14 +74,16 @@ test('multiFile appender', (batch) => {
batch.test('should pass options to rolling file stream', (t) => {
log4js.configure({
appenders: { multi: {
type: 'multiFile',
base: 'logs/',
property: 'label',
extension: '.log',
maxLogSize: 61,
backups: 2
} },
appenders: {
multi: {
type: 'multiFile',
base: 'logs/',
property: 'label',
extension: '.log',
maxLogSize: 61,
backups: 2
}
},
categories: { default: { appenders: ['multi'], level: 'info' } }
});
const loggerF = log4js.getLogger();

View File

@ -68,7 +68,11 @@ test('Multiprocess Appender', (batch) => {
}
);
log4js.configure({
appenders: { worker: { type: 'multiprocess', mode: 'worker', loggerPort: 1234, loggerHost: 'pants' } },
appenders: {
worker: {
type: 'multiprocess', mode: 'worker', loggerPort: 1234, loggerHost: 'pants'
}
},
categories: { default: { appenders: ['worker'], level: 'trace' } }
});
@ -233,32 +237,22 @@ test('Multiprocess Appender', (batch) => {
});
t.test('when a client connects', (assert) => {
const logString = `${JSON.stringify(
{
level: { level: 10000, levelStr: 'DEBUG' },
data: ['some debug']
}
)}__LOG4JS__`;
const logString = `${JSON.stringify({
level: { level: 10000, levelStr: 'DEBUG' },
data: ['some debug']
})}__LOG4JS__`;
net.cbs.data(
`${JSON.stringify(
{
level: { level: 40000, levelStr: 'ERROR' },
data: ['an error message']
}
)}__LOG4JS__`
);
net.cbs.data(`${JSON.stringify({
level: { level: 40000, levelStr: 'ERROR' },
data: ['an error message']
})}__LOG4JS__`);
net.cbs.data(logString.substring(0, 10));
net.cbs.data(logString.substring(10));
net.cbs.data(logString + logString + logString);
net.cbs.end(
`${JSON.stringify(
{
level: { level: 50000, levelStr: 'FATAL' },
data: ["that's all folks"]
}
)}__LOG4JS__`
);
net.cbs.end(`${JSON.stringify({
level: { level: 50000, levelStr: 'FATAL' },
data: ["that's all folks"]
})}__LOG4JS__`);
net.cbs.data('bad message__LOG4JS__');
const logEvents = recording.replay();
@ -304,11 +298,12 @@ test('Multiprocess Appender', (batch) => {
}
}
);
t.throws(() =>
log4js.configure({
appenders: { master: { type: 'multiprocess', mode: 'master' } },
categories: { default: { appenders: ['master'], level: 'trace' } }
}),
t.throws(
() =>
log4js.configure({
appenders: { master: { type: 'multiprocess', mode: 'master' } },
categories: { default: { appenders: ['master'], level: 'trace' } }
}),
new Error('multiprocess master must have an "appender" defined')
);
t.end();
@ -325,11 +320,12 @@ test('Multiprocess Appender', (batch) => {
}
}
);
t.throws(() =>
log4js.configure({
appenders: { master: { type: 'multiprocess', mode: 'master', appender: 'cheese' } },
categories: { default: { appenders: ['master'], level: 'trace' } }
}),
t.throws(
() =>
log4js.configure({
appenders: { master: { type: 'multiprocess', mode: 'master', appender: 'cheese' } },
categories: { default: { appenders: ['master'], level: 'trace' } }
}),
new Error('multiprocess master appender "cheese" not defined')
);
t.end();

View File

@ -118,12 +118,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { cheese: { value: 'biscuits' } },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level "cheese".value must have an integer value'
));
'level "cheese".value must have an integer value'));
t.throws(() => {
log4js.configure({
@ -133,12 +131,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { cheese: 'biscuits' },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level "cheese" must be an object'
));
'level "cheese" must be an object'));
t.throws(() => {
log4js.configure({
@ -148,12 +144,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { cheese: { thing: 'biscuits' } },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level "cheese" must have a \'value\' property'
));
'level "cheese" must have a \'value\' property'));
t.throws(() => {
log4js.configure({
@ -163,12 +157,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { cheese: { value: 3 } },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level "cheese" must have a \'colour\' property'
));
'level "cheese" must have a \'colour\' property'));
t.throws(() => {
log4js.configure({
@ -178,12 +170,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { cheese: { value: 3, colour: 'pants' } },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level "cheese".colour must be one of white, grey, black, blue, cyan, green, magenta, red, yellow'
));
'level "cheese".colour must be one of white, grey, black, blue, cyan, green, magenta, red, yellow'));
t.throws(() => {
log4js.configure({
@ -193,12 +183,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { '#pants': 3 },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level name "#pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'
));
'level name "#pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'));
t.throws(() => {
log4js.configure({
@ -208,12 +196,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { 'thing#pants': 3 },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level name "thing#pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'
));
'level name "thing#pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'));
t.throws(() => {
log4js.configure({
@ -223,12 +209,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { '1pants': 3 },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level name "1pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'
));
'level name "1pants" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'));
t.throws(() => {
log4js.configure({
@ -238,12 +222,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { '2': 3 },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level name "2" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'
));
'level name "2" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'));
t.throws(() => {
log4js.configure({
@ -253,12 +235,10 @@ test('../../lib/logger', (batch) => {
appenders: { stdout: { type: 'stdout' } },
categories: { default: { appenders: ['stdout'], level: 'trace' } }
});
}, new Error(
'Problem with log4js configuration: ' +
}, new Error('Problem with log4js configuration: ' +
"({ levels: { 'cheese!': 3 },\n appenders: { stdout: { type: 'stdout' } },\n" +
" categories: { default: { appenders: [ 'stdout' ], level: 'trace' } } }) - " +
'level name "cheese!" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'
));
'level name "cheese!" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)'));
t.end();
});