1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Added validation of provided LoggerProvider

This commit is contained in:
ivaosthu 2017-08-05 15:38:55 +02:00 committed by Ivar Conradi Østhus
parent f5e9ca3bbe
commit e97a3820a2
2 changed files with 49 additions and 4 deletions

View File

@ -9,7 +9,17 @@ module.exports = exports = function getLogger(name) {
};
exports.setLoggerProvider = function setLoggerProvider(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');
loggerProvider = provider;
logger.info('Custom Logger Provider initalized.');
};
function getDefaultLogProvider() {
@ -31,3 +41,9 @@ function getDefaultLogProvider() {
return log4js.getLogger;
}
function validate(isValid, msg) {
if (!isValid) {
throw new TypeError(msg);
}
}

View File

@ -3,20 +3,49 @@
const { test } = require('ava');
const createLogger = require('./logger');
const logger = require('../logger');
const sinon = require('sinon');
test('should expose a setLoggerProvider function', t => {
t.true(logger.setLoggerProvider instanceof Function);
});
test('should create logger via custom logger provider', t => {
const provider = sinon.stub();
const loggerName = 'test';
const loggerImpl = {};
provider.withArgs(loggerName).returns(loggerImpl);
const loggerImpl = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
};
const provider = () => loggerImpl;
logger.setLoggerProvider(provider);
const log = createLogger(loggerName);
t.is(log, loggerImpl);
});
test('should require custom logger to implement info', t => {
const loggerImpl = {
debug: () => {},
warn: () => {},
error: () => {},
};
const provider = () => loggerImpl;
const error = t.throws(() => {
logger.setLoggerProvider(provider)();
}, TypeError);
t.is(error.message, 'Logger must implement info');
});
test('should require custom logger to implement error', t => {
const loggerImpl = {
debug: () => {},
warn: () => {},
info: () => {},
};
const provider = () => loggerImpl;
const error = t.throws(() => {
logger.setLoggerProvider(provider)();
}, TypeError);
t.is(error.message, 'Logger must implement error');
});