|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const util = require('util'); |
| 4 | +const levels = require('./levels'); |
| 5 | +const layouts = require('./layouts'); |
| 6 | + |
| 7 | +function not(thing) { |
| 8 | + return !thing; |
| 9 | +} |
| 10 | + |
| 11 | +function anObject(thing) { |
| 12 | + return thing && typeof thing === 'object' && !Array.isArray(thing); |
| 13 | +} |
| 14 | + |
| 15 | +class Configuration { |
| 16 | + |
| 17 | + throwExceptionIf(checks, message) { |
| 18 | + const tests = Array.isArray(checks) ? checks : [checks]; |
| 19 | + tests.forEach((test) => { |
| 20 | + if (test) { |
| 21 | + throw new Error( |
| 22 | + `Problem with log4js configuration: (${util.inspect(this.candidate, { depth: 5 })}) - ${message}` |
| 23 | + ); |
| 24 | + } |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + tryLoading(path) { |
| 29 | + try { |
| 30 | + return require(path); //eslint-disable-line |
| 31 | + } catch (e) { |
| 32 | + // if the module was found, and we still got an error, then raise it |
| 33 | + this.throwExceptionIf( |
| 34 | + e.code !== 'MODULE_NOT_FOUND', |
| 35 | + `appender "${path}" could not be loaded (error was: ${e})` |
| 36 | + ); |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + loadAppenderModule(type) { |
| 42 | + return this.tryLoading(`./appenders/${type}`) || this.tryLoading(type); |
| 43 | + } |
| 44 | + |
| 45 | + createAppender(name, config) { |
| 46 | + const appenderModule = this.loadAppenderModule(config.type); |
| 47 | + this.throwExceptionIf( |
| 48 | + not(appenderModule), |
| 49 | + `appender "${name}" is not valid (type "${config.type}" could not be found)` |
| 50 | + ); |
| 51 | + return appenderModule.configure(config, layouts, this.configuredAppenders.get.bind(this.configuredAppenders)); |
| 52 | + } |
| 53 | + |
| 54 | + get appenders() { |
| 55 | + return this.configuredAppenders; |
| 56 | + } |
| 57 | + |
| 58 | + set appenders(appenderConfig) { |
| 59 | + const appenderNames = Object.keys(appenderConfig); |
| 60 | + this.throwExceptionIf(not(appenderNames.length), 'must define at least one appender.'); |
| 61 | + |
| 62 | + this.configuredAppenders = new Map(); |
| 63 | + appenderNames.forEach((name) => { |
| 64 | + this.throwExceptionIf( |
| 65 | + not(appenderConfig[name].type), |
| 66 | + `appender "${name}" is not valid (must be an object with property "type")` |
| 67 | + ); |
| 68 | + |
| 69 | + this.configuredAppenders.set(name, this.createAppender(name, appenderConfig[name])); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + get categories() { |
| 74 | + return this.configuredCategories; |
| 75 | + } |
| 76 | + |
| 77 | + set categories(categoryConfig) { |
| 78 | + const categoryNames = Object.keys(categoryConfig); |
| 79 | + this.throwExceptionIf(not(categoryNames.length), 'must define at least one category.'); |
| 80 | + |
| 81 | + this.configuredCategories = new Map(); |
| 82 | + categoryNames.forEach((name) => { |
| 83 | + const category = categoryConfig[name]; |
| 84 | + this.throwExceptionIf( |
| 85 | + [ |
| 86 | + not(category.appenders), |
| 87 | + not(category.level) |
| 88 | + ], |
| 89 | + `category "${name}" is not valid (must be an object with properties "appenders" and "level")` |
| 90 | + ); |
| 91 | + |
| 92 | + this.throwExceptionIf( |
| 93 | + not(Array.isArray(category.appenders)), |
| 94 | + `category "${name}" is not valid (appenders must be an array of appender names)` |
| 95 | + ); |
| 96 | + |
| 97 | + this.throwExceptionIf( |
| 98 | + not(category.appenders.length), |
| 99 | + `category "${name}" is not valid (appenders must contain at least one appender name)` |
| 100 | + ); |
| 101 | + |
| 102 | + const appenders = []; |
| 103 | + category.appenders.forEach((appender) => { |
| 104 | + this.throwExceptionIf( |
| 105 | + not(this.configuredAppenders.get(appender)), |
| 106 | + `category "${name}" is not valid (appender "${appender}" is not defined)` |
| 107 | + ); |
| 108 | + appenders.push(this.appenders.get(appender)); |
| 109 | + }); |
| 110 | + |
| 111 | + this.throwExceptionIf( |
| 112 | + not(levels.toLevel(category.level)), |
| 113 | + `category "${name}" is not valid (level "${category.level}" not recognised;` + |
| 114 | + ` valid levels are ${levels.levels.join(', ')})` |
| 115 | + ); |
| 116 | + |
| 117 | + this.configuredCategories.set(name, { appenders: appenders, level: levels.toLevel(category.level) }); |
| 118 | + }); |
| 119 | + |
| 120 | + this.throwExceptionIf(not(categoryConfig.default), 'must define a "default" category.'); |
| 121 | + } |
| 122 | + |
| 123 | + constructor(candidate) { |
| 124 | + this.candidate = candidate; |
| 125 | + |
| 126 | + this.throwExceptionIf(not(anObject(candidate)), 'must be an object.'); |
| 127 | + this.throwExceptionIf(not(anObject(candidate.appenders)), 'must have a property "appenders" of type object.'); |
| 128 | + this.throwExceptionIf(not(anObject(candidate.categories)), 'must have a property "categories" of type object.'); |
| 129 | + |
| 130 | + this.appenders = candidate.appenders; |
| 131 | + this.categories = candidate.categories; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = Configuration; |
0 commit comments