diff --git a/CHANGES b/CHANGES index 7b0b9ca0..b764003e 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,8 @@ LIST OF CHANGES for npg_ranger project - upgrade tmp to 0.0.31 from 0.0.30 - do not allow query strings containing multiple attribute-value pairs for the same attribute + - added flags to server to change logging level and enable logging to console + - all logging to console is now sent to stderr - upgrade Freebayes to 1.1.0 from 1.0.2-npg-Aug2016 in TravisCI and docs release 1.0.0 diff --git a/bin/client.js b/bin/client.js index be767afa..21cc06fd 100755 --- a/bin/client.js +++ b/bin/client.js @@ -65,7 +65,7 @@ cline .version(require('../package.json').version) .description('Command line client for GA4GH data streaming') .arguments(' [output]') - .option('--debug', 'Show debug output') + .option('--loglevel ', 'level of logging output', /^(error|warn|info|debug)$/i, 'warn') .option('--accept-trailers', 'Request trailers from server') .parse(process.argv); @@ -91,11 +91,9 @@ cline.on('--help', () => { if ( !cline.args.length || ( cline.args.length != 1 && cline.args.length != 2 ) ) { cline.help(); } -if ( !cline.debug ) { - LOGGER.level = 'warn'; -} else { - LOGGER.level = 'debug'; -} +LOGGER.setup(true); + +LOGGER.level = cline.loglevel; var url = cline.args[0]; var output = cline.args.length == 2 ? cline.args[1] : undefined; diff --git a/bin/server.js b/bin/server.js index d065c5ce..d9bcfae8 100755 --- a/bin/server.js +++ b/bin/server.js @@ -247,18 +247,36 @@ class BrokerFactory { : new FlatBroker(serverFactory); } } +function setLogLevel(options) { + if (options.get('debug')) { + LOGGER.level = 'debug'; + return; + } + let logLevel = options.get('loglevel'); + let knownLevels = ['error', 'warn', 'info', 'debug', 'silly']; + if (knownLevels.indexOf(logLevel) !== -1) { + LOGGER.level = logLevel; + return; + } + // --loglevel was given an invalid parameter - leave at default and log error + LOGGER.error('configuration: loglevel expects one of ' + knownLevels.join(',') + '; but received ' + logLevel); + process.exit(1); +} /** * Application's main method. */ if ( require.main === module ) { const options = config.provide(config.fromCommandLine); + if ( options.get('version') ) { console.log(require('../package.json').version); process.exit(0); - } else if ( options.get('debug') ) { - LOGGER.level = 'debug'; + } else { + LOGGER.setup(options.get('logconsole')); + setLogLevel(options); } + let numWorkers = options.get('numworkers'); let waitingConsec = options.get('clustertimeout'); let maxConsec = options.get('clustermaxdeaths'); diff --git a/lib/config.js b/lib/config.js index e4104d6d..cb689e9d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -56,12 +56,15 @@ const optionsList = [ ['s','skipauth' ,'skip authorisation steps'], ['' ,'anyorigin' ,'accept CORS requests from any origin'], ['e','emaildomain=DOMAIN' ,'email domain to validate the remote user value against'], - ['d','debug' ,'debugging mode for this server'], + ['', 'logconsole' ,'outputs logs to console'], + ['d','debug' ,'sets logger level to debug for this server'], + ['L','loglevel=[error|warn|info|debug]','logging level for this server [default: error]'], ['V','version' ,'print version and exit'], ['h','help' ,'display this help'] ]; const defaultOptions = { + loglevel: 'error', [RO_OPTION_KEY]: false, /* cluster */ numworkers: 1, diff --git a/lib/logsetup.js b/lib/logsetup.js index 65f2291f..90a1c935 100644 --- a/lib/logsetup.js +++ b/lib/logsetup.js @@ -1,6 +1,7 @@ "use strict"; const LOGGER = require('winston'); + const moment = require('moment'); /** @@ -27,27 +28,24 @@ var getTimeStamp = function() { return moment().format('YYYY-MM-DD HH:mm:ss Z'); }; -try { - LOGGER.remove(LOGGER.transports.Console); - LOGGER.add(LOGGER.transports.Console, { - level: 'warn', - colorize: true, - json: false, - timestamp: getTimeStamp, - stderrLevels: [ 'error' ] // Which levels to send to stderr - }); - LOGGER.add(LOGGER.transports.File, { - level: 'warn', - filename: 'npg_ranger.winston.log', - json: false, - timestamp: getTimeStamp, - maxsize: 10 * 1000 * 1000, - maxFiles: 20 - }); -} catch (e) { - console.error('Error when trying to init logging with Winston'); - console.error(e); - process.exit(1); -} +var setup = function(logconsole) { + try { + LOGGER.remove(LOGGER.transports.Console); + if (logconsole) { + LOGGER.add(LOGGER.transports.Console, { + level: 'warn', + colorize: true, + json: false, + timestamp: getTimeStamp, + stderrLevels: [ 'error', 'warn', 'info', 'verbose', 'debug', 'silly' ] // Which levels to send to stderr + }); + } + } catch (e) { + console.error('Error when trying to init logging with Winston'); + console.error(e); + process.exit(1); + } +}; module.exports = LOGGER; +module.exports.setup = setup; diff --git a/test/client/highLevelClient.spec.js b/test/client/highLevelClient.spec.js index 6cfd686d..bf0dcf66 100644 --- a/test/client/highLevelClient.spec.js +++ b/test/client/highLevelClient.spec.js @@ -241,6 +241,7 @@ describe('Running with ranger server with a', () => { let serv = spawn(serverCommand, [ '-s', '-d', + '--logconsole', '-n0', `-p${SERV_PORT}`, `-m${mongourl}`]); @@ -256,7 +257,7 @@ describe('Running with ranger server with a', () => { it('file url', (done) => { let serv = startServer( done, fail ); - serv.stdout.on('data', (data) => { + serv.stderr.on('data', (data) => { if (data.toString().match(/Server listening on /)) { // Server is listening and ready for connection let client = spawn('bin/client.js', [ @@ -278,7 +279,7 @@ describe('Running with ranger server with a', () => { it('sample url', (done) => { let serv = startServer( done, fail ); - serv.stdout.on('data', (data) => { + serv.stderr.on('data', (data) => { if (data.toString().match(/Server listening on /)) { // Server is listening and ready for connection let client = spawn('bin/client.js', [ @@ -304,7 +305,7 @@ describe('Running with ranger server with a', () => { it('sample url returning vcf', (done) => { let serv = startServer( done, fail ); - serv.stdout.on('data', (data) => { + serv.stderr.on('data', (data) => { if (data.toString().match(/Server listening on /)) { // Server is listening and ready for connection let body = ''; @@ -327,7 +328,7 @@ describe('Running with ranger server with a', () => { it('GA4GH url and the redirect is followed', (done) => { let serv = startServer( done, fail ); - serv.stdout.on('data', (data) => { + serv.stderr.on('data', (data) => { if (data.toString().match(/Server listening on /)) { // Server is listening and ready for connection let client = spawn('bin/client.js', [ diff --git a/test/config.spec.js b/test/config.spec.js index 9ff5c1b2..e6cc29b9 100644 --- a/test/config.spec.js +++ b/test/config.spec.js @@ -131,6 +131,8 @@ describe('Listing config options', function() { 'debug=true', 'emaildomain="some.com"', 'hostname="myhost"', + 'logconsole=undefined', + 'loglevel="error"', 'mongourl="mymongourl"', 'multiref=undefined', 'numworkers=3', diff --git a/test/server/secure_server.spec.js b/test/server/secure_server.spec.js index d6b46c64..b9ff33b3 100644 --- a/test/server/secure_server.spec.js +++ b/test/server/secure_server.spec.js @@ -35,6 +35,7 @@ describe('test running https server', () => { let s = spawn(serverCommand, [ '-s', '-d', + '--logconsole', '--startssl', '--secure_key', private_pem, '--secure_cert', cert_pem, @@ -106,7 +107,7 @@ describe('test running https server', () => { it('can reply with a reference from a https requests', ( done ) => { serv = startServer( done ); let acc = 'XYZ120923'; - serv.stdout.on('data', (data) => { + serv.stderr.on('data', (data) => { if (data.toString().match(/Server listening on /)) { // Server is listening and ready for connection let options = {