diff --git a/browserid/app.js b/browserid/app.js index 1302ea64a7a6b97b1453e84058d6c1afe13ed1b7..a1389e66b44fdad9b73668720653534f2d0dc4fa 100644 --- a/browserid/app.js +++ b/browserid/app.js @@ -42,7 +42,7 @@ function router(app) { // this should probably be an internal redirect // as soon as relative paths are figured out. app.get('/sign_in', function(req, res, next ) { - logging.userEntry(req); + logging.userEntry('browserid', req); res.render('dialog.ejs', { title: 'A Better Way to Sign In', layout: false, diff --git a/libs/configuration.js b/libs/configuration.js index 200705a11e08052dae95df92921a051a497a6c86..2690dc1f2fa1eb02b72e072f1f0cca4e436cd439 100644 --- a/libs/configuration.js +++ b/libs/configuration.js @@ -25,26 +25,29 @@ const g_configs = { hostname: 'browserid.org', port: '443', scheme: 'https', - use_minified_resources: true + use_minified_resources: true, + log_path: '/home/browserid/var/' }, development: { hostname: 'dev.diresworb.org', port: '443', scheme: 'https', - use_minified_resources: true + use_minified_resources: true, + log_path: '/home/browserid/var/' }, beta: { hostname: 'diresworb.org', port: '443', scheme: 'https', - use_minified_resources: true + use_minified_resources: true, + log_path: '/home/browserid/var/' }, local: { hostname: '127.0.0.1', port: '10002', scheme: 'http', use_minified_resources: false, - log_path: 'log.txt' + log_path: './' } }; diff --git a/libs/logging.js b/libs/logging.js index 37684d29f89a2ef64b261538c1683dd413013148..c34aaa0c15ad10cd60c1f1c93b3f7968fb02dc0e 100644 --- a/libs/logging.js +++ b/libs/logging.js @@ -6,37 +6,47 @@ const configuration = require("./configuration"); // FIXME: separate logs depending on purpose? var log_path = configuration.get('log_path'); -var LOGGER = null; -if (log_path) { - LOGGER= new (winston.Logger)({ - transports: [new (winston.transports.File)({filename: log_path})] +var LOGGERS = []; + +function setupLogger(category) { + if (!log_path) + return console.log("no log path! Not logging!"); + + // don't create the logger if it already exists + if (LOGGERS[category]) + return; + + // FIXME: check if log_path is properly terminated + var filename = log_path + category + "-log.txt"; + + LOGGERS[category] = new (winston.Logger)({ + transports: [new (winston.transports.File)({filename: filename})] }); } // entry is an object that will get JSON'ified -exports.log = function(entry) { +exports.log = function(category, entry) { // entry must have at least a type if (!entry.type) throw new Error("every log entry needs a type"); + // setup the logger if need be + setupLogger(category); + // timestamp entry.at = new Date().toUTCString(); // if no logger, go to console (FIXME: do we really want to log to console?) - if (LOGGER) - LOGGER.info(JSON.stringify(entry)); - else - winston.info(JSON.stringify(entry)); + LOGGERS[category].info(JSON.stringify(entry)); }; // utility function to log a bunch of stuff at user entry point -exports.userEntry = function(req) { - exports.log({ +exports.userEntry = function(category, req) { + exports.log(category, { type: 'signin', browser: req.headers['user-agent'], rp: req.headers['referer'], // IP address (this probably needs to be replaced with the X-forwarded-for value ip: req.connection.remoteAddress }); - console.log(JSON.stringify(req.headers)); }; \ No newline at end of file diff --git a/verifier/app.js b/verifier/app.js index afb710dd4b75329a1d12cacabfeaee13174eb963..740311628d5b61c10b057ce1f4843313cd60e87d 100644 --- a/verifier/app.js +++ b/verifier/app.js @@ -37,7 +37,7 @@ function doVerify(req, resp, next) { audience, function(payload) { // log it! - logging.log({ + logging.log('verifier', { type: 'verify', result: 'success', rp: payload.audience @@ -53,7 +53,7 @@ function doVerify(req, resp, next) { resp.json(result); }, function(errorObj) { - logging.log({ + logging.log('verifier', { type: 'verify', result: 'failure', rp: audience @@ -63,7 +63,7 @@ function doVerify(req, resp, next) { ); } catch (e) { console.log(e.stack); - logging.log({ + logging.log('verifier', { type: 'verify', result: 'failure', rp: audience