2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const log4js = require('log4js');
|
2014-10-20 13:03:43 +02:00
|
|
|
|
2017-08-04 16:03:15 +02:00
|
|
|
function getDefaultLogProvider() {
|
|
|
|
let level;
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2018-07-30 13:48:12 +02:00
|
|
|
level = log4js.levels.ERROR.levelStr;
|
2017-08-04 16:03:15 +02:00
|
|
|
} else if (process.env.NODE_ENV === 'test') {
|
2018-07-30 13:48:12 +02:00
|
|
|
level = log4js.levels.FATAL.levelStr;
|
2017-08-04 16:03:15 +02:00
|
|
|
} else {
|
2018-07-30 13:48:12 +02:00
|
|
|
level = log4js.levels.DEBUG.levelStr;
|
2017-08-04 16:03:15 +02:00
|
|
|
}
|
2015-03-10 16:30:56 +01:00
|
|
|
|
2017-08-04 16:03:15 +02:00
|
|
|
log4js.configure({
|
2017-11-11 11:13:34 +01:00
|
|
|
appenders: {
|
|
|
|
console: { type: 'console' },
|
|
|
|
},
|
|
|
|
categories: {
|
|
|
|
default: { appenders: ['console'], level },
|
2017-08-04 16:03:15 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return log4js.getLogger;
|
|
|
|
}
|
2017-08-05 15:38:55 +02:00
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
let loggerProvider = getDefaultLogProvider();
|
|
|
|
|
2017-08-05 15:38:55 +02:00
|
|
|
function validate(isValid, msg) {
|
|
|
|
if (!isValid) {
|
|
|
|
throw new TypeError(msg);
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
|
|
|
|
module.exports.defaultLogProvider = loggerProvider;
|
|
|
|
|
|
|
|
function validateLogProvider(provider) {
|
|
|
|
validate(typeof provider === 'function', 'Provider needs to be a function');
|
|
|
|
|
|
|
|
const logger = provider('unleash:logger');
|
|
|
|
validate(typeof logger.debug === 'function', 'Logger must implement debug');
|
|
|
|
validate(typeof logger.info === 'function', 'Logger must implement info');
|
|
|
|
validate(typeof logger.warn === 'function', 'Logger must implement warn');
|
|
|
|
validate(typeof logger.error === 'function', 'Logger must implement error');
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.validateLogProvider = validateLogProvider;
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
exports.setLoggerProvider = function setLoggerProvider(provider) {
|
|
|
|
validateLogProvider(provider);
|
|
|
|
|
|
|
|
loggerProvider = provider;
|
|
|
|
const logger = provider('unleash:logger');
|
|
|
|
logger.info(`Your way of configuring a logProvider is depreacted.
|
|
|
|
See https://unleash.github.io/docs/getting_started for details`);
|
|
|
|
};
|