[6a3a178] | 1 |
|
---|
| 2 |
|
---|
| 3 | const util = require('util');
|
---|
| 4 | const debug = require('debug')('log4js:configuration');
|
---|
| 5 |
|
---|
| 6 | const preProcessingListeners = [];
|
---|
| 7 | const listeners = [];
|
---|
| 8 |
|
---|
| 9 | const not = thing => !thing;
|
---|
| 10 |
|
---|
| 11 | const anObject = thing => thing && typeof thing === 'object' && !Array.isArray(thing);
|
---|
| 12 |
|
---|
| 13 | const validIdentifier = thing => /^[A-Za-z][A-Za-z0-9_]*$/g.test(thing);
|
---|
| 14 |
|
---|
| 15 | const anInteger = thing => thing && typeof thing === 'number' && Number.isInteger(thing);
|
---|
| 16 |
|
---|
| 17 | const addListener = (fn) => {
|
---|
| 18 | listeners.push(fn);
|
---|
| 19 | debug(`Added listener, now ${listeners.length} listeners`);
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | const addPreProcessingListener = (fn) => {
|
---|
| 23 | preProcessingListeners.push(fn);
|
---|
| 24 | debug(`Added pre-processing listener, now ${preProcessingListeners.length} listeners`);
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | const throwExceptionIf = (config, checks, message) => {
|
---|
| 28 | const tests = Array.isArray(checks) ? checks : [checks];
|
---|
| 29 | tests.forEach((test) => {
|
---|
| 30 | if (test) {
|
---|
| 31 | throw new Error(`Problem with log4js configuration: (${util.inspect(config, { depth: 5 })})`
|
---|
| 32 | + ` - ${message}`);
|
---|
| 33 | }
|
---|
| 34 | });
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | const configure = (candidate) => {
|
---|
| 38 | debug('New configuration to be validated: ', candidate);
|
---|
| 39 | throwExceptionIf(candidate, not(anObject(candidate)), 'must be an object.');
|
---|
| 40 |
|
---|
| 41 | debug(`Calling pre-processing listeners (${preProcessingListeners.length})`);
|
---|
| 42 | preProcessingListeners.forEach(listener => listener(candidate));
|
---|
| 43 | debug('Configuration pre-processing finished.');
|
---|
| 44 |
|
---|
| 45 | debug(`Calling configuration listeners (${listeners.length})`);
|
---|
| 46 | listeners.forEach(listener => listener(candidate));
|
---|
| 47 | debug('Configuration finished.');
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | module.exports = {
|
---|
| 51 | configure,
|
---|
| 52 | addListener,
|
---|
| 53 | addPreProcessingListener,
|
---|
| 54 | throwExceptionIf,
|
---|
| 55 | anObject,
|
---|
| 56 | anInteger,
|
---|
| 57 | validIdentifier,
|
---|
| 58 | not
|
---|
| 59 | };
|
---|