index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const path = require('path');
  2. const debug = require('debug')('log4js:appenders');
  3. const configuration = require('../configuration');
  4. const clustering = require('../clustering');
  5. const levels = require('../levels');
  6. const layouts = require('../layouts');
  7. const adapters = require('./adapters');
  8. // pre-load the core appenders so that webpack can find them
  9. const coreAppenders = new Map();
  10. coreAppenders.set('console', require('./console'));
  11. coreAppenders.set('stdout', require('./stdout'));
  12. coreAppenders.set('stderr', require('./stderr'));
  13. coreAppenders.set('logLevelFilter', require('./logLevelFilter'));
  14. coreAppenders.set('categoryFilter', require('./categoryFilter'));
  15. coreAppenders.set('noLogFilter', require('./noLogFilter'));
  16. coreAppenders.set('file', require('./file'));
  17. coreAppenders.set('dateFile', require('./dateFile'));
  18. coreAppenders.set('fileSync', require('./fileSync'));
  19. const appenders = new Map();
  20. const tryLoading = (modulePath, config) => {
  21. debug('Loading module from ', modulePath);
  22. try {
  23. return require(modulePath); //eslint-disable-line
  24. } catch (e) {
  25. // if the module was found, and we still got an error, then raise it
  26. configuration.throwExceptionIf(
  27. config,
  28. e.code !== 'MODULE_NOT_FOUND',
  29. `appender "${modulePath}" could not be loaded (error was: ${e})`
  30. );
  31. return undefined;
  32. }
  33. };
  34. const loadAppenderModule = (type, config) => coreAppenders.get(type)
  35. || tryLoading(`./${type}`, config)
  36. || tryLoading(type, config)
  37. || (require.main && tryLoading(path.join(path.dirname(require.main.filename), type), config))
  38. || tryLoading(path.join(process.cwd(), type), config);
  39. const appendersLoading = new Set();
  40. const getAppender = (name, config) => {
  41. if (appenders.has(name)) return appenders.get(name);
  42. if (!config.appenders[name]) return false;
  43. if (appendersLoading.has(name)) throw new Error(`Dependency loop detected for appender ${name}.`);
  44. appendersLoading.add(name);
  45. debug(`Creating appender ${name}`);
  46. // eslint-disable-next-line no-use-before-define
  47. const appender = createAppender(name, config);
  48. appendersLoading.delete(name);
  49. appenders.set(name, appender);
  50. return appender;
  51. };
  52. const createAppender = (name, config) => {
  53. const appenderConfig = config.appenders[name];
  54. const appenderModule = appenderConfig.type.configure
  55. ? appenderConfig.type : loadAppenderModule(appenderConfig.type, config);
  56. configuration.throwExceptionIf(
  57. config,
  58. configuration.not(appenderModule),
  59. `appender "${name}" is not valid (type "${appenderConfig.type}" could not be found)`
  60. );
  61. if (appenderModule.appender) {
  62. debug(`DEPRECATION: Appender ${appenderConfig.type} exports an appender function.`);
  63. }
  64. if (appenderModule.shutdown) {
  65. debug(`DEPRECATION: Appender ${appenderConfig.type} exports a shutdown function.`);
  66. }
  67. debug(`${name}: clustering.isMaster ? ${clustering.isMaster()}`);
  68. debug(`${name}: appenderModule is ${require('util').inspect(appenderModule)}`); // eslint-disable-line
  69. return clustering.onlyOnMaster(() => {
  70. debug(`calling appenderModule.configure for ${name} / ${appenderConfig.type}`);
  71. return appenderModule.configure(
  72. adapters.modifyConfig(appenderConfig),
  73. layouts,
  74. appender => getAppender(appender, config),
  75. levels
  76. );
  77. }, () => { });
  78. };
  79. const setup = (config) => {
  80. appenders.clear();
  81. appendersLoading.clear();
  82. const usedAppenders = [];
  83. Object.values(config.categories).forEach(category => {
  84. usedAppenders.push(...category.appenders)
  85. });
  86. Object.keys(config.appenders).forEach((name) => {
  87. // dodgy hard-coding of special case for tcp-server which may not have
  88. // any categories associated with it, but needs to be started up anyway
  89. if (usedAppenders.includes(name) || config.appenders[name].type === 'tcp-server') {
  90. getAppender(name, config);
  91. }
  92. });
  93. };
  94. setup({ appenders: { out: { type: 'stdout' } }, categories: { default: { appenders: ['out'], level: 'trace' } } });
  95. configuration.addListener((config) => {
  96. configuration.throwExceptionIf(
  97. config,
  98. configuration.not(configuration.anObject(config.appenders)),
  99. 'must have a property "appenders" of type object.'
  100. );
  101. const appenderNames = Object.keys(config.appenders);
  102. configuration.throwExceptionIf(
  103. config,
  104. configuration.not(appenderNames.length),
  105. 'must define at least one appender.'
  106. );
  107. appenderNames.forEach((name) => {
  108. configuration.throwExceptionIf(
  109. config,
  110. configuration.not(config.appenders[name].type),
  111. `appender "${name}" is not valid (must be an object with property "type")`
  112. );
  113. });
  114. });
  115. configuration.addListener(setup);
  116. module.exports = appenders;