From 4e06613a2a1402d93b2017b29bd44ba9880c1055 Mon Sep 17 00:00:00 2001 From: Ben Adida <ben@adida.net> Date: Wed, 3 Aug 2011 13:32:00 -0700 Subject: [PATCH] logging now supports multiple log files --- browserid/app.js | 2 +- libs/configuration.js | 11 +++++++---- libs/logging.js | 34 ++++++++++++++++++++++------------ verifier/app.js | 6 +++--- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/browserid/app.js b/browserid/app.js index 1302ea64a..a1389e66b 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 200705a11..2690dc1f2 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 37684d29f..c34aaa0c1 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 afb710dd4..740311628 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 -- GitLab