Merge pull request #6321 from appium/isaac-logging

Add tests for logging levels
This commit is contained in:
Isaac A. Murchie
2016-03-22 10:22:21 -07:00
2 changed files with 76 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import { patchLogger } from 'appium-logger';
import winston from 'winston';
import { fs } from 'appium-support';
import 'date-utils';
import _ from 'lodash';
// set up distributed logging before everything else
@@ -204,6 +205,15 @@ async function init (args) {
}
}
function clear () {
if (logger) {
for (let transport of _.keys(logger.transports)) {
logger.remove(transport);
}
}
npmlog.removeAllListeners('log');
}
export { init };
export { init, clear };
export default init;

View File

@@ -1,14 +1,72 @@
// transpile:mocha
import { init as logsinkInit } from '../lib/logsink';
import logger from '../lib/logger';
import { init as logsinkInit, clear as logsinkClear } from '../lib/logsink';
import sinon from 'sinon';
import { getLogger } from 'appium-logger';
// temporarily turn on logging to stdio, so we can catch and query
let forceLogs = process.env._FORCE_LOGS;
process.env._FORCE_LOGS = 1;
let logger = getLogger('Appium');
describe('Logger', () => {
before(() => {
logsinkInit({});
let stderrSpy;
let stdoutSpy;
beforeEach(() => {
stderrSpy = sinon.spy(process.stderr, 'write');
stdoutSpy = sinon.spy(process.stdout, 'write');
logsinkClear();
});
it('should work', () => {
logger.warn('something');
logger.debug('something');
afterEach(() => {
stderrSpy.restore();
stdoutSpy.restore();
});
after(() => {
process.env._FORCE_LOGS = forceLogs;
});
const errorMsg = 'some error';
const warnMsg = 'some warning';
const debugMsg = 'some debug';
function doLogging () {
logger.error(errorMsg);
logger.warn(warnMsg);
logger.debug(debugMsg);
}
it('should send error, info and debug when loglevel is debug', async () => {
await logsinkInit({loglevel: 'debug'});
doLogging();
stderrSpy.callCount.should.equal(1);
stderrSpy.args[0][0].should.include(errorMsg);
stdoutSpy.callCount.should.equal(2);
stdoutSpy.args[0][0].should.include(warnMsg);
stdoutSpy.args[1][0].should.include(debugMsg);
});
it('should send error and info when loglevel is info', async () => {
await logsinkInit({loglevel: 'info'});
doLogging();
stderrSpy.callCount.should.equal(1);
stderrSpy.args[0][0].should.include(errorMsg);
stdoutSpy.callCount.should.equal(1);
stdoutSpy.args[0][0].should.include(warnMsg);
});
it('should send error when loglevel is error', async () => {
await logsinkInit({loglevel: 'error'});
doLogging();
stderrSpy.callCount.should.equal(1);
stderrSpy.args[0][0].should.include(errorMsg);
stdoutSpy.callCount.should.equal(0);
});
});