Add global logging helper

This commit is contained in:
Pavel Djundik 2016-04-16 14:32:38 +03:00
parent fd36eea0ed
commit ede3131168
17 changed files with 69 additions and 67 deletions

View File

@ -30,6 +30,7 @@ rules:
spaced-comment: [2, always] spaced-comment: [2, always]
globals: globals:
log: false
$: false $: false
Favico: false Favico: false
Handlebars: false Handlebars: false

View File

@ -29,6 +29,7 @@
"dependencies": { "dependencies": {
"bcrypt-nodejs": "0.0.3", "bcrypt-nodejs": "0.0.3",
"cheerio": "0.20.0", "cheerio": "0.20.0",
"colors": "1.1.2",
"commander": "2.9.0", "commander": "2.9.0",
"event-stream": "3.3.2", "event-stream": "3.3.2",
"express": "4.13.4", "express": "4.13.4",

View File

@ -75,6 +75,8 @@ function Client(manager, name, config) {
delay += 1000; delay += 1000;
}); });
} }
log.info("User '" + name + "' loaded");
} }
Client.prototype.emit = function(event, data) { Client.prototype.emit = function(event, data) {

View File

@ -31,7 +31,7 @@ ClientManager.prototype.loadUser = function(name) {
try { try {
var json = this.readUserConfig(name); var json = this.readUserConfig(name);
} catch (e) { } catch (e) {
console.log(e); log.error("Failed to read user config", e);
return; return;
} }
if (!this.findClient(name)) { if (!this.findClient(name)) {
@ -40,9 +40,6 @@ ClientManager.prototype.loadUser = function(name) {
name, name,
json json
)); ));
console.log(
"User '" + name + "' loaded."
);
} }
}; };
@ -58,7 +55,7 @@ ClientManager.prototype.getUsers = function() {
} }
}); });
} catch (e) { } catch (e) {
console.log(e); log.error("Failed to get users", e);
return; return;
} }
return users; return users;
@ -107,7 +104,7 @@ ClientManager.prototype.updateUser = function(name, opts) {
JSON.stringify(user, null, " ") JSON.stringify(user, null, " ")
); );
} catch (e) { } catch (e) {
console.log(e); log.error("Failed to update user", e);
return; return;
} }
return true; return true;
@ -160,9 +157,7 @@ ClientManager.prototype.autoload = function(/* sockets */) {
if (client) { if (client) {
client.quit(); client.quit();
self.clients = _.without(self.clients, client); self.clients = _.without(self.clients, client);
console.log( log.info("User '" + name + "' disconnected");
"User '" + name + "' disconnected."
);
} }
}); });
}, 1000); }, 1000);

View File

@ -13,10 +13,8 @@ program
try { try {
mkdirp.sync(path); mkdirp.sync(path);
} catch (e) { } catch (e) {
console.log(""); log.error("Could not create", path);
console.log("Could not create " + path); log.info("Try running the command as sudo.");
console.log("Try running the command as sudo.");
console.log("");
return; return;
} }
try { try {
@ -24,18 +22,14 @@ program
fs.mkdirSync(test); fs.mkdirSync(test);
fs.rmdirSync(test); fs.rmdirSync(test);
} catch (e) { } catch (e) {
console.log(""); log.error("You have no permissions to write to", path);
console.log("You have no permissions to write to " + path); log.info("Try running the command as sudo.");
console.log("Try running the command as sudo.");
console.log("");
return; return;
} }
var manager = new ClientManager(); var manager = new ClientManager();
var users = manager.getUsers(); var users = manager.getUsers();
if (users.indexOf(name) !== -1) { if (users.indexOf(name) !== -1) {
console.log(""); log.error("User '" + name + "' already exists.");
console.log("User '" + name + "' already exists.");
console.log("");
return; return;
} }
require("read")({ require("read")({
@ -47,14 +41,12 @@ program
}); });
function add(manager, name, password) { function add(manager, name, password) {
console.log("");
var salt = bcrypt.genSaltSync(8); var salt = bcrypt.genSaltSync(8);
var hash = bcrypt.hashSync(password, salt); var hash = bcrypt.hashSync(password, salt);
manager.addUser( manager.addUser(
name, name,
hash hash
); );
console.log("User '" + name + "' created:"); log.info("User '" + name + "' created:");
console.log(Helper.HOME + "/users/" + name + ".json"); log.info(Helper.HOME + "/users/" + name + ".json");
console.log("");
} }

View File

@ -9,9 +9,7 @@ program
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
console.log(""); log.error("User '" + name + "' doesn't exist.");
console.log("User '" + name + "' doesn't exist.");
console.log("");
return; return;
} }
child.spawn( child.spawn(

View File

@ -1,3 +1,5 @@
GLOBAL.log = require("../log.js");
var program = require("commander"); var program = require("commander");
var pkg = require("../../package.json"); var pkg = require("../../package.json");
var fs = require("fs"); var fs = require("fs");
@ -20,8 +22,7 @@ if (!fs.existsSync(config)) {
config, config,
fs.readFileSync(__dirname + "/../../defaults/config.js") fs.readFileSync(__dirname + "/../../defaults/config.js")
); );
console.log("Config created:"); log.info("Config created:", config);
console.log(config);
} }
require("./start"); require("./start");

View File

@ -7,15 +7,11 @@ program
.action(function() { .action(function() {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (!users.length) { if (!users.length) {
console.log(""); log.warn("No users found!");
console.log("No users found!");
console.log("");
} else { } else {
console.log("");
console.log("Users:"); console.log("Users:");
for (var i = 0; i < users.length; i++) { for (var i = 0; i < users.length; i++) {
console.log(" " + (i + 1) + ". " + users[i]); console.log(" " + (i + 1) + ". " + users[i]);
} }
console.log("");
} }
}); });

View File

@ -13,20 +13,14 @@ program
fs.mkdirSync(test); fs.mkdirSync(test);
fs.rmdirSync(test); fs.rmdirSync(test);
} catch (e) { } catch (e) {
console.log(""); log.warn("You have no permissions to delete from " + path);
console.log("You have no permissions to delete from " + path); log.info("Try running the command as sudo.");
console.log("Try running the command as sudo.");
console.log("");
return; return;
} }
var manager = new ClientManager(); var manager = new ClientManager();
if (manager.removeUser(name)) { if (manager.removeUser(name)) {
console.log(""); log.info("Removed user '" + name + "'.");
console.log("Removed '" + name + "'.");
console.log("");
} else { } else {
console.log(""); log.error("User '" + name + "' doesn't exist.");
console.log("User '" + name + "' doesn't exist.");
console.log("");
} }
}); });

View File

@ -10,9 +10,7 @@ program
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
console.log(""); log.error("User '" + name + "' doesn't exist.");
console.log("User '" + name + "' doesn't exist.");
console.log("");
return; return;
} }
var file = Helper.HOME + "/users/" + name + ".json"; var file = Helper.HOME + "/users/" + name + ".json";
@ -21,7 +19,6 @@ program
prompt: "[thelounge] New password: ", prompt: "[thelounge] New password: ",
silent: true silent: true
}, function(err, password) { }, function(err, password) {
console.log("");
if (err) { if (err) {
return; return;
} }
@ -32,7 +29,6 @@ program
file, file,
JSON.stringify(user, null, " ") JSON.stringify(user, null, " ")
); );
console.log("Successfully reset password for '" + name + "'."); log.info("Successfully reset password for '" + name + "'.");
console.log("");
}); });
}); });

View File

@ -21,10 +21,8 @@ program
mode = false; mode = false;
} }
if (!mode && !users.length) { if (!mode && !users.length) {
console.log(""); log.warn("No users found!");
console.log("No users found!"); log.info("Create a new user with 'lounge add <name>'.");
console.log("Create a new user with 'lounge add <name>'.");
console.log("");
} else { } else {
server({ server({
host: program.host || process.env.IP || config.host, host: program.host || process.env.IP || config.host,

View File

@ -4,7 +4,9 @@ var net = require("net");
var users = {}; var users = {};
module.exports.start = function(port) { 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) { module.exports.hook = function(stream, user) {
@ -44,4 +46,3 @@ function parse(data) {
data = data.split(","); data = data.split(",");
return parseInt(data[0]) + ", " + parseInt(data[1]); return parseInt(data[0]) + ", " + parseInt(data[1]);
} }

31
src/log.js Normal file
View File

@ -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));
};

View File

@ -26,7 +26,7 @@ module.exports = function(irc, network) {
}); });
irc.on("socket error", function(err) { irc.on("socket error", function(err) {
console.log(err); log.debug("IRC socket error", err);
network.channels[0].pushMessage(client, new Msg({ network.channels[0].pushMessage(client, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "Socket error: " + err text: "Socket error: " + err

View File

@ -5,12 +5,12 @@ module.exports = function(irc, network) {
// TODO: remove later // TODO: remove later
irc.on("irc_error", function(data) { irc.on("irc_error", function(data) {
console.log("Got an irc_error"); log.debug("Got an irc_error");
irc.emit("error", data); irc.emit("error", data);
}); });
irc.on("error", function(data) { irc.on("error", function(data) {
console.log("error", data); log.debug("error", data);
var text = data.error; var text = data.error;
if (data.reason) { if (data.reason) {
text = data.reason + " (" + text + ")"; text = data.reason + " (" + text + ")";

View File

@ -56,10 +56,8 @@ module.exports = function(options) {
manager.sockets = sockets; manager.sockets = sockets;
console.log(""); log.info("The Lounge is now running on", protocol + "://" + config.host + ":" + config.port + "/");
console.log("The Lounge is now running on " + protocol + "://" + config.host + ":" + config.port + "/"); log.info("Press ctrl-c to stop");
console.log("Press ctrl-c to stop");
console.log("");
if (!config.public) { if (!config.public) {
manager.loadUsers(); manager.loadUsers();

View File

@ -8,7 +8,7 @@ module.exports.write = function(user, network, chan, msg) {
var path = Helper.HOME + "/logs/" + user + "/" + network; var path = Helper.HOME + "/logs/" + user + "/" + network;
mkdirp.sync(path); mkdirp.sync(path);
} catch (e) { } catch (e) {
console.log(e); log.error("Unabled to create logs directory", e);
return; return;
} }
@ -39,9 +39,7 @@ module.exports.write = function(user, network, chan, msg) {
path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log", path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log",
line + "\n", line + "\n",
function(e) { function(e) {
if (e) { log.error("Failed to write user log", e);
console.log("Log#write():\n" + e);
}
} }
); );
}; };