From ad6d86eb81d54ab7d0bdfd02e2ce015509b39fe2 Mon Sep 17 00:00:00 2001 From: Hermann Mayer Date: Fri, 5 Apr 2013 10:04:37 +0200 Subject: [PATCH] Added logging option to en-/disable logging to stdout. --- README.markdown | 4 ++++ lib/connect-mysql-session.js | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.markdown b/README.markdown index 9dddd0b..5ce03c4 100644 --- a/README.markdown +++ b/README.markdown @@ -53,6 +53,10 @@ Default: `1000*60*10` (10 minutes). How frequently the session store checks for Default: `1000*60*60*24` (1 day). How long session data is stored for "user session" cookies -- i.e. sessions that only last as long as the user keeps their browser open, which are created by doing `req.session.maxAge = null`. +### logging ### + +Default: `false`. If set to true, we are logging whats going on to stdout. + Changes ------- diff --git a/lib/connect-mysql-session.js b/lib/connect-mysql-session.js index bf33c96..007add2 100644 --- a/lib/connect-mysql-session.js +++ b/lib/connect-mysql-session.js @@ -4,7 +4,7 @@ module.exports = function (connect) { function MySQLStore(database, user, password, options) { - options = options || {}; + options = options || {logging: false}; connect.session.Store.call(this, options); var self = this, @@ -30,14 +30,14 @@ module.exports = function (connect) sequelize.sync({force: forceSync}) .on('success', function () { - console.log('MySQL session store initialized.'); + options.logging && console.log('MySQL session store initialized.'); initialized = true; callback(); }) .on('failure', function (error) { - console.log('Failed to initialize MySQL session store:'); - console.log(error); + options.logging && console.log('Failed to initialize MySQL session store:'); + options.logging && console.log(error); callback(error); }); } @@ -54,7 +54,7 @@ module.exports = function (connect) { if (sessions.length > 0) { - console.log('Destroying ' + sessions.length + ' expired sessions.'); + options.logging && console.log('Destroying ' + sessions.length + ' expired sessions.'); for (var i in sessions) { sessions[i].destroy(); @@ -63,8 +63,8 @@ module.exports = function (connect) }) .on('failure', function (error) { - console.log('Failed to fetch expired sessions:'); - console.log(error); + options.logging && console.log('Failed to fetch expired sessions:'); + options.logging && console.log(error); }); }); }, checkExpirationInterval); @@ -141,8 +141,8 @@ module.exports = function (connect) }) .on('failure', function (error) { - console.log('Session ' + sid + ' could not be destroyed:'); - console.log(error); + options.logging && console.log('Session ' + sid + ' could not be destroyed:'); + options.logging && console.log(error); fn && fn(error); }); }