From 0a9ef4f8d50946fd0951d504d0af72dc22d66f73 Mon Sep 17 00:00:00 2001 From: Marc Trudel Date: Mon, 21 Jan 2013 18:10:46 +0900 Subject: [PATCH] * Allow for configurable logging option (basic callback function) * TODO: create level logging --- api.js | 13 ++++++++----- plugins/irc/irc_plugin.js | 15 +++++++++------ server.js | 20 +++++++++++--------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/api.js b/api.js index b8353b9..8430c30 100644 --- a/api.js +++ b/api.js @@ -1,11 +1,12 @@ var util = require('util'), fs = require('fs'), - logger = require('util'), _ = require('underscore')._, humanized_time_span = require(__dirname + '/lib/humanized_time_span.js'), EventEmitter = require('events').EventEmitter, controller = new EventEmitter(); +var log; + /** Set this module as an express.js controller */ @@ -21,6 +22,8 @@ module.exports.setup = function (settings) { module.exports.configure(settings); + log = settings.logger ? settings.logger : require('util').log; + /** Load available plugins */ @@ -30,11 +33,11 @@ module.exports.setup = function (settings) { if (fs.statSync(__dirname + '/plugins/' + directory).isDirectory() && fs.statSync(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').isFile()) { return require(__dirname + '/plugins/' + directory + '/' + directory + '_plugin.js').create(controller, settings); } else { - logger.log("Excluding plugin: " + directory); + log("Excluding plugin: " + directory); } }); } else { - logger.log("Error when creating plugin: " + err); + log("Error when creating plugin: " + err); } }); @@ -58,7 +61,7 @@ module.exports.configure = function (s) { }; module.exports.addService = function (serviceCfg) { - logger.log('Adding new service: ' + serviceCfg.name); + log('Adding new service: ' + serviceCfg.name); module.exports.removeService(serviceCfg.name); settings.services.push(serviceCfg); }; @@ -85,7 +88,7 @@ status.services = []; module.exports.checkAllServices = function() { status.lastupdate = new Date().toUTCString(); - logger.log('Refreshing status board...'); + log('Refreshing status board...'); /** Update the service status object, check the service diff --git a/plugins/irc/irc_plugin.js b/plugins/irc/irc_plugin.js index b30d3e0..74dcaaa 100644 --- a/plugins/irc/irc_plugin.js +++ b/plugins/irc/irc_plugin.js @@ -1,9 +1,12 @@ var _ = require('underscore')._, - logger = require('util'); + log; exports.create = function(api, settings) { + + log = settings.logger ? settings.logger : require('util').log; + if (settings.plugins && settings.plugins.irc && settings.plugins.irc.enable) { - logger.log('Creating the plugin: ' + __filename); + log('Creating the plugin: ' + __filename); var irc = require('irc'); var channels = settings.plugins.irc.options.channels; @@ -61,11 +64,11 @@ exports.create = function(api, settings) { }); bot.on('join', function(channel, who) { - logger.log(who + ' has joined ' + channel); + log(who + ' has joined ' + channel); if (who == settings.plugins.irc.nick) { // TODO : Let's do it for each channel of the list... - logger.log('You are now connected to channel ' + channel + ' and ready to send messages'); + log('You are now connected to channel ' + channel + ' and ready to send messages'); connected = true; _.each(cache, function(message){ bot.say(channels, message); @@ -76,7 +79,7 @@ exports.create = function(api, settings) { // Add a private message listener to interact with the IRC bot bot.addListener('pm', function(nick, message) { - logger.log('Got private message from ' + nick + ': ' + message); + log('Got private message from ' + nick + ': ' + message); var callme = commands[trim(message)]; if (typeof callme !== 'undefined') { bot.say(nick, callme.call(null, api)); @@ -88,7 +91,7 @@ exports.create = function(api, settings) { // Send the message or cache it var pushMessage = function(message) { - logger.log('Pushing message ' + message); + log('Pushing message ' + message); if (connected) { bot.say(channels, message); } else { diff --git a/server.js b/server.js index 7a37dd6..8ee5f50 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,16 @@ var path = require('path'), - util = require('util'), url = require('url'), _ = require('underscore')._; +var log; + exports.dashboard = function(settings) { var rootPath = path.dirname(module.filename), api = require('./api'), express = require('express'), app = express.createServer(); + log = settings.logger ? settings.logger : require('util').log; /** Express app definition @@ -18,7 +20,7 @@ exports.dashboard = function(settings) { app.configure(function () { var staticPath = path.join(rootPath, 'public'); - util.log("Express server static directory: " + staticPath); + log("Express server static directory: " + staticPath); app.use(express['static'].call(null, staticPath)); app.use(express.favicon(path.join(staticPath, 'favicon.ico'))); @@ -29,7 +31,7 @@ exports.dashboard = function(settings) { app.listen(settings.port, settings.hostname); - util.log('Server running at http://' + settings.hostname + ':' + settings.port); + log('Server running at http://' + settings.hostname + ':' + settings.port); /** Routes are connected to the api, and an extra route is added for self-health checking @@ -66,7 +68,7 @@ exports.dashboard = function(settings) { io.sockets.on('connection', function(socket) { count++; - util.log('New client connected! (' + count + ')'); + log('New client connected! (' + count + ')'); socket.emit('title', settings.title); socket.emit('status', api.getStatus()); @@ -75,7 +77,7 @@ exports.dashboard = function(settings) { socket.on('disconnect', function() { count--; - util.log('Client disconnect! (' + count + ')'); + log('Client disconnect! (' + count + ')'); socket.broadcast.emit('count', count); }); }); @@ -94,18 +96,18 @@ exports.dashboard = function(settings) { api.on("routeContribution", function(route) { app.get(route.path, route.binding); - util.log("Add GET route contribution: " + route.path); + log("Add GET route contribution: " + route.path); }); api.on("postRouteContribution", function(route) { app.post(route.path, route.binding); - util.log("Add POST route contribution: " + route.path); + log("Add POST route contribution: " + route.path); }); api.on("staticContribution", function(plugin) { var docRoot = __dirname + '/plugins/' + plugin + '/public'; app.use("/api/" + plugin, express['static'].call(null, docRoot)); - util.log("Add static contribution: " + plugin + ", " + docRoot); + log("Add static contribution: " + plugin + ", " + docRoot); }); api.on("refresh", function(status) { @@ -116,5 +118,5 @@ exports.dashboard = function(settings) { // We make the API available for external control this.api = api; - util.log('Server started.'); + log('Server started.'); };