diff --git a/public/templates/admin/logger.tpl b/public/templates/admin/logger.tpl
index 969647f820..83f569010b 100644
--- a/public/templates/admin/logger.tpl
+++ b/public/templates/admin/logger.tpl
@@ -5,9 +5,13 @@
- By enabling the check boxes, you will receive logs to standard output. If you specify a path, logs will then be saved to a file instead. HTTP logging is useful for collecting statistics about who and when people access your forum. Socket.io logging, in combination with redis-cli monitor, can be very helpful for learning NodeBB's internals.
+ By enabling the check boxes, you will receive logs to your terminal. If you specify a path, logs will then be saved to a file instead. HTTP logging is useful for collecting statistics about who, when, and what people access on your forum. In addition to logging HTTP requests, we can also log socket.io events. Socket.io logging, in combination with redis-cli monitor, can be very helpful for learning NodeBB's internals.
+
+ Simply check/uncheck the logging settings to enable or disable logging on the fly. No restart needed.
+
+
diff --git a/src/logger.js b/src/logger.js
new file mode 100644
index 0000000000..37f5aab35b
--- /dev/null
+++ b/src/logger.js
@@ -0,0 +1,198 @@
+/*
+ * Logger module: ability to dynamically turn on/off logging for http requests & socket.io events
+ */
+
+var fs = require('fs'),
+ express = require('express'),
+ winston = require('winston'),
+ util = require('util'),
+ socketio = require('socket.io'),
+ meta = require('./meta.js');
+
+var opts = {
+ /*
+ * state used by Logger
+ */
+ express : {
+ app : {},
+ set : 0,
+ ofn : null,
+ },
+ streams : {
+ log : { f : process.stdout },
+ }
+};
+
+(function(Logger) {
+
+
+ Logger.init = function(app) {
+ opts.express.app = app;
+ /* Open log file stream & initialize express logging if meta.config.logger* variables are set */
+ Logger.setup();
+ }
+
+ Logger.setup = function() {
+ Logger.setup_one('loggerPath', meta.config.loggerPath);
+ }
+
+ Logger.setup_one = function(key,value) {
+ /*
+ * 1. Open the logger stream: stdout or file
+ * 2. Re-initialize the express logger hijack
+ */
+ switch(key) {
+ case 'loggerPath': {
+ Logger.setup_one_log(value);
+ Logger.express_open();
+ }
+ default: return;
+ }
+ }
+
+ Logger.setup_one_log = function(value) {
+
+ if(meta.config.loggerStatus > 0 || meta.config.loggerIOStatus) {
+ var stream = Logger.open(value);
+ if(stream) opts.streams.log.f = stream;
+ else opts.streams.log.f = process.stdout;
+ }
+ else {
+ Logger.close(opts.streams.log);
+ }
+
+ }
+
+ Logger.open = function(value) {
+
+ /* Open the streams to log to: either a path or stdout */
+ var stream;
+ if(value)
+ stream = fs.createWriteStream(value, {flags: 'a'});
+ else
+ stream = process.stdout;
+
+ return stream;
+ }
+
+ Logger.close = function(stream) {
+ if(stream.f != process.stdout && stream.f != null) stream.end();
+ stream.f = null;
+ }
+
+ Logger.monitorConfig = function(socket, data) {
+ /*
+ * This monitor's when a user clicks "save" in the Logger section of the admin panel
+ */
+ Logger.setup_one(data.key,data.value);
+ Logger.io_close(socket);
+ Logger.io(socket);
+ }
+
+ Logger.express_open = function() {
+ if(opts.express.set != 1) {
+ opts.express.set = 1;
+ opts.express.app.use(Logger.expressLogger);
+ }
+ /*
+ * Always initialize "ofn" (original function) with the original logger function
+ */
+ opts.express.ofn = express.logger({stream : opts.streams.log.f});
+ }
+
+ Logger.expressLogger = function(req,res,next) {
+ /*
+ * The new express.logger
+ *
+ * This hijack allows us to turn logger on/off dynamically within express
+ */
+ if(meta.config.loggerStatus > 0) {
+ return opts.express.ofn(req,res,next);
+ }
+ else {
+ return next()
+ }
+ }
+
+ Logger.prepare_io_string = function(_type, _uid, _args) {
+ /*
+ * This prepares the output string for intercepted socket.io events
+ *
+ * The format is: io: