Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Flags for logging levels and logging to console #151

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions bin/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cline
.version(require('../package.json').version)
.description('Command line client for GA4GH data streaming')
.arguments('<url> [output]')
.option('--debug', 'Show debug output')
.option('--loglevel <level>', 'level of logging output', /^(error|warn|info|debug)$/i, 'warn')
.option('--accept-trailers', 'Request trailers from server')
.parse(process.argv);

Expand All @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 4 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 20 additions & 22 deletions lib/logsetup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const LOGGER = require('winston');

const moment = require('moment');

/**
Expand All @@ -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;
9 changes: 5 additions & 4 deletions test/client/highLevelClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ describe('Running with ranger server with a', () => {
let serv = spawn(serverCommand, [
'-s',
'-d',
'--logconsole',
'-n0',
`-p${SERV_PORT}`,
`-m${mongourl}`]);
Expand All @@ -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', [
Expand All @@ -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', [
Expand All @@ -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 = '';
Expand All @@ -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', [
Expand Down
2 changes: 2 additions & 0 deletions test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/server/secure_server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down