configuration.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const util = require('util');
  3. const debug = require('debug')('log4js:configuration');
  4. const listeners = [];
  5. const not = thing => !thing;
  6. const anObject = thing => thing && typeof thing === 'object' && !Array.isArray(thing);
  7. const validIdentifier = thing => /^[A-Za-z][A-Za-z0-9_]*$/g.test(thing);
  8. const anInteger = thing => thing && typeof thing === 'number' && Number.isInteger(thing);
  9. const addListener = (fn) => {
  10. listeners.push(fn);
  11. debug(`Added listener, listeners now ${listeners.length}`);
  12. };
  13. const throwExceptionIf = (config, checks, message) => {
  14. const tests = Array.isArray(checks) ? checks : [checks];
  15. tests.forEach((test) => {
  16. if (test) {
  17. throw new Error(`Problem with log4js configuration: (${util.inspect(config, { depth: 5 })})` +
  18. ` - ${message}`);
  19. }
  20. });
  21. };
  22. const configure = (candidate) => {
  23. debug('New configuration to be validated: ', candidate);
  24. throwExceptionIf(candidate, not(anObject(candidate)), 'must be an object.');
  25. debug(`Calling configuration listeners (${listeners.length})`);
  26. listeners.forEach(listener => listener(candidate));
  27. debug('Configuration finished.');
  28. };
  29. module.exports = {
  30. configure,
  31. addListener,
  32. throwExceptionIf,
  33. anObject,
  34. anInteger,
  35. validIdentifier,
  36. not
  37. };