From fd36eea0ed837bffe63d0d68a83eb716628068d6 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 16 Apr 2016 13:35:04 +0300 Subject: [PATCH 1/2] Rename log to userLog --- src/client.js | 4 ++-- src/{log.js => userLog.js} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{log.js => userLog.js} (100%) diff --git a/src/client.js b/src/client.js index fde1cae0..0948d9b9 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,7 @@ var _ = require("lodash"); var Chan = require("./models/chan"); var crypto = require("crypto"); -var log = require("./log"); +var userLog = require("./userLog"); var Msg = require("./models/msg"); var Network = require("./models/network"); var ircFramework = require("irc-framework"); @@ -90,7 +90,7 @@ Client.prototype.emit = function(event, data) { if (target.chan.type === Chan.Type.LOBBY) { chan = target.network.host; } - log.write( + userLog.write( this.name, target.network.host, chan, diff --git a/src/log.js b/src/userLog.js similarity index 100% rename from src/log.js rename to src/userLog.js From ede3131168fbf26d51e09d9614cb52d2c33b2f02 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 16 Apr 2016 14:32:38 +0300 Subject: [PATCH 2/2] Add global logging helper --- .eslintrc.yml | 1 + package.json | 1 + src/client.js | 2 ++ src/clientManager.js | 13 ++++-------- src/command-line/add.js | 22 +++++++------------- src/command-line/edit.js | 4 +--- src/command-line/index.js | 5 +++-- src/command-line/list.js | 6 +----- src/command-line/remove.js | 14 ++++--------- src/command-line/reset.js | 8 ++----- src/command-line/start.js | 6 ++---- src/identd.js | 5 +++-- src/log.js | 31 ++++++++++++++++++++++++++++ src/plugins/irc-events/connection.js | 2 +- src/plugins/irc-events/error.js | 4 ++-- src/server.js | 6 ++---- src/userLog.js | 6 ++---- 17 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 src/log.js diff --git a/.eslintrc.yml b/.eslintrc.yml index 8ac14f95..7274f1f6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -30,6 +30,7 @@ rules: spaced-comment: [2, always] globals: + log: false $: false Favico: false Handlebars: false diff --git a/package.json b/package.json index 86f0bd94..8d645941 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "dependencies": { "bcrypt-nodejs": "0.0.3", "cheerio": "0.20.0", + "colors": "1.1.2", "commander": "2.9.0", "event-stream": "3.3.2", "express": "4.13.4", diff --git a/src/client.js b/src/client.js index 0948d9b9..561492ce 100644 --- a/src/client.js +++ b/src/client.js @@ -75,6 +75,8 @@ function Client(manager, name, config) { delay += 1000; }); } + + log.info("User '" + name + "' loaded"); } Client.prototype.emit = function(event, data) { diff --git a/src/clientManager.js b/src/clientManager.js index 185ba39f..a982ed0a 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -31,7 +31,7 @@ ClientManager.prototype.loadUser = function(name) { try { var json = this.readUserConfig(name); } catch (e) { - console.log(e); + log.error("Failed to read user config", e); return; } if (!this.findClient(name)) { @@ -40,9 +40,6 @@ ClientManager.prototype.loadUser = function(name) { name, json )); - console.log( - "User '" + name + "' loaded." - ); } }; @@ -58,7 +55,7 @@ ClientManager.prototype.getUsers = function() { } }); } catch (e) { - console.log(e); + log.error("Failed to get users", e); return; } return users; @@ -107,7 +104,7 @@ ClientManager.prototype.updateUser = function(name, opts) { JSON.stringify(user, null, " ") ); } catch (e) { - console.log(e); + log.error("Failed to update user", e); return; } return true; @@ -160,9 +157,7 @@ ClientManager.prototype.autoload = function(/* sockets */) { if (client) { client.quit(); self.clients = _.without(self.clients, client); - console.log( - "User '" + name + "' disconnected." - ); + log.info("User '" + name + "' disconnected"); } }); }, 1000); diff --git a/src/command-line/add.js b/src/command-line/add.js index 3ae52e86..24905c14 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -13,10 +13,8 @@ program try { mkdirp.sync(path); } catch (e) { - console.log(""); - console.log("Could not create " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.error("Could not create", path); + log.info("Try running the command as sudo."); return; } try { @@ -24,18 +22,14 @@ program fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - console.log(""); - console.log("You have no permissions to write to " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.error("You have no permissions to write to", path); + log.info("Try running the command as sudo."); return; } var manager = new ClientManager(); var users = manager.getUsers(); if (users.indexOf(name) !== -1) { - console.log(""); - console.log("User '" + name + "' already exists."); - console.log(""); + log.error("User '" + name + "' already exists."); return; } require("read")({ @@ -47,14 +41,12 @@ program }); function add(manager, name, password) { - console.log(""); var salt = bcrypt.genSaltSync(8); var hash = bcrypt.hashSync(password, salt); manager.addUser( name, hash ); - console.log("User '" + name + "' created:"); - console.log(Helper.HOME + "/users/" + name + ".json"); - console.log(""); + log.info("User '" + name + "' created:"); + log.info(Helper.HOME + "/users/" + name + ".json"); } diff --git a/src/command-line/edit.js b/src/command-line/edit.js index 70f1ebc3..f9671102 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -9,9 +9,7 @@ program .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); return; } child.spawn( diff --git a/src/command-line/index.js b/src/command-line/index.js index 3366dd75..37b221f9 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -1,3 +1,5 @@ +GLOBAL.log = require("../log.js"); + var program = require("commander"); var pkg = require("../../package.json"); var fs = require("fs"); @@ -20,8 +22,7 @@ if (!fs.existsSync(config)) { config, fs.readFileSync(__dirname + "/../../defaults/config.js") ); - console.log("Config created:"); - console.log(config); + log.info("Config created:", config); } require("./start"); diff --git a/src/command-line/list.js b/src/command-line/list.js index f94e20b4..4e8f8bc3 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -7,15 +7,11 @@ program .action(function() { var users = new ClientManager().getUsers(); if (!users.length) { - console.log(""); - console.log("No users found!"); - console.log(""); + log.warn("No users found!"); } else { - console.log(""); console.log("Users:"); for (var i = 0; i < users.length; i++) { console.log(" " + (i + 1) + ". " + users[i]); } - console.log(""); } }); diff --git a/src/command-line/remove.js b/src/command-line/remove.js index bdab70c4..683892f5 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -13,20 +13,14 @@ program fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - console.log(""); - console.log("You have no permissions to delete from " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.warn("You have no permissions to delete from " + path); + log.info("Try running the command as sudo."); return; } var manager = new ClientManager(); if (manager.removeUser(name)) { - console.log(""); - console.log("Removed '" + name + "'."); - console.log(""); + log.info("Removed user '" + name + "'."); } else { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); } }); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 4414de38..d953ce66 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -10,9 +10,7 @@ program .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); return; } var file = Helper.HOME + "/users/" + name + ".json"; @@ -21,7 +19,6 @@ program prompt: "[thelounge] New password: ", silent: true }, function(err, password) { - console.log(""); if (err) { return; } @@ -32,7 +29,6 @@ program file, JSON.stringify(user, null, " ") ); - console.log("Successfully reset password for '" + name + "'."); - console.log(""); + log.info("Successfully reset password for '" + name + "'."); }); }); diff --git a/src/command-line/start.js b/src/command-line/start.js index ac56cee6..58f7a4ba 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -21,10 +21,8 @@ program mode = false; } if (!mode && !users.length) { - console.log(""); - console.log("No users found!"); - console.log("Create a new user with 'lounge add '."); - console.log(""); + log.warn("No users found!"); + log.info("Create a new user with 'lounge add '."); } else { server({ host: program.host || process.env.IP || config.host, diff --git a/src/identd.js b/src/identd.js index 76773568..3d626dad 100644 --- a/src/identd.js +++ b/src/identd.js @@ -4,7 +4,9 @@ var net = require("net"); var users = {}; module.exports.start = function(port) { - net.createServer(init).listen(port || 113); + port = port || 113; + log.info("Starting identd server on port", port); + net.createServer(init).listen(port); }; module.exports.hook = function(stream, user) { @@ -44,4 +46,3 @@ function parse(data) { data = data.split(","); return parseInt(data[0]) + ", " + parseInt(data[1]); } - diff --git a/src/log.js b/src/log.js new file mode 100644 index 00000000..b973e503 --- /dev/null +++ b/src/log.js @@ -0,0 +1,31 @@ +var colors = require("colors/safe"); +var moment = require("moment"); +var Helper = require("./helper"); + +function timestamp(type, messageArgs) { + var config = Helper.getConfig(); + var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; + var tz = (config.logs || {}).timezone || "UTC+00:00"; + + var time = moment().utcOffset(tz).format(format); + + Array.prototype.unshift.call(messageArgs, colors.dim(time), type); + + return messageArgs; +} + +exports.err = function() { + console.error.apply(console, timestamp(colors.red("[ERROR]"), arguments)); +}; + +exports.warn = function() { + console.error.apply(console, timestamp(colors.yellow("[WARN]"), arguments)); +}; + +exports.info = function() { + console.log.apply(console, timestamp(colors.blue("[INFO]"), arguments)); +}; + +exports.debug = function() { + console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); +}; diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index 6d07bb7d..26676297 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -26,7 +26,7 @@ module.exports = function(irc, network) { }); irc.on("socket error", function(err) { - console.log(err); + log.debug("IRC socket error", err); network.channels[0].pushMessage(client, new Msg({ type: Msg.Type.ERROR, text: "Socket error: " + err diff --git a/src/plugins/irc-events/error.js b/src/plugins/irc-events/error.js index 08a5507d..001acd22 100644 --- a/src/plugins/irc-events/error.js +++ b/src/plugins/irc-events/error.js @@ -5,12 +5,12 @@ module.exports = function(irc, network) { // TODO: remove later irc.on("irc_error", function(data) { - console.log("Got an irc_error"); + log.debug("Got an irc_error"); irc.emit("error", data); }); irc.on("error", function(data) { - console.log("error", data); + log.debug("error", data); var text = data.error; if (data.reason) { text = data.reason + " (" + text + ")"; diff --git a/src/server.js b/src/server.js index 5ff0226e..895e7312 100644 --- a/src/server.js +++ b/src/server.js @@ -56,10 +56,8 @@ module.exports = function(options) { manager.sockets = sockets; - console.log(""); - console.log("The Lounge is now running on " + protocol + "://" + config.host + ":" + config.port + "/"); - console.log("Press ctrl-c to stop"); - console.log(""); + log.info("The Lounge is now running on", protocol + "://" + config.host + ":" + config.port + "/"); + log.info("Press ctrl-c to stop"); if (!config.public) { manager.loadUsers(); diff --git a/src/userLog.js b/src/userLog.js index 63d93b06..7cbe5064 100644 --- a/src/userLog.js +++ b/src/userLog.js @@ -8,7 +8,7 @@ module.exports.write = function(user, network, chan, msg) { var path = Helper.HOME + "/logs/" + user + "/" + network; mkdirp.sync(path); } catch (e) { - console.log(e); + log.error("Unabled to create logs directory", e); return; } @@ -39,9 +39,7 @@ module.exports.write = function(user, network, chan, msg) { path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log", line + "\n", function(e) { - if (e) { - console.log("Log#write():\n" + e); - } + log.error("Failed to write user log", e); } ); };