Merge pull request #257 from thelounge/xpaw/console-log

Add global logging helper
This commit is contained in:
Jérémie Astori 2016-04-27 02:43:54 -04:00
commit 21fb0e0709
17 changed files with 103 additions and 101 deletions

View File

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

View File

@ -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",

View File

@ -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");
@ -75,6 +75,8 @@ function Client(manager, name, config) {
delay += 1000;
});
}
log.info("User '" + name + "' loaded");
}
Client.prototype.emit = function(event, data) {
@ -90,7 +92,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,

View File

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

View File

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

View File

@ -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(

View File

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

View File

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

View File

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

View File

@ -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 + "'.");
});
});

View File

@ -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 <name>'.");
console.log("");
log.warn("No users found!");
log.info("Create a new user with 'lounge add <name>'.");
} else {
server({
host: program.host || process.env.IP || config.host,

View File

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

View File

@ -1,47 +1,31 @@
var fs = require("fs");
var mkdirp = require("mkdirp");
var colors = require("colors/safe");
var moment = require("moment");
var Helper = require("./helper");
module.exports.write = function(user, network, chan, msg) {
try {
var path = Helper.HOME + "/logs/" + user + "/" + network;
mkdirp.sync(path);
} catch (e) {
console.log(e);
return;
}
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);
var line = "[" + time + "] ";
var type = msg.type.trim();
if (type === "message" || type === "highlight") {
// Format:
// [2014-01-01 00:00:00] <Arnold> Put that cookie down.. Now!!
line += "<" + msg.from + "> " + msg.text;
} else {
// Format:
// [2014-01-01 00:00:00] * Arnold quit
line += "* " + msg.from + " " + msg.type;
if (msg.text) {
line += " " + msg.text;
}
Array.prototype.unshift.call(messageArgs, colors.dim(time), type);
return messageArgs;
}
fs.appendFile(
// Quick fix to escape pre-escape channel names that contain % using %%,
// and / using %. **This does not escape all reserved words**
path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log",
line + "\n",
function(e) {
if (e) {
console.log("Log#write():\n" + e);
}
}
);
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) {
console.log(err);
log.debug("IRC socket error", err);
network.channels[0].pushMessage(client, new Msg({
type: Msg.Type.ERROR,
text: "Socket error: " + err

View File

@ -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 + ")";

View File

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

45
src/userLog.js Normal file
View File

@ -0,0 +1,45 @@
var fs = require("fs");
var mkdirp = require("mkdirp");
var moment = require("moment");
var Helper = require("./helper");
module.exports.write = function(user, network, chan, msg) {
try {
var path = Helper.HOME + "/logs/" + user + "/" + network;
mkdirp.sync(path);
} catch (e) {
log.error("Unabled to create logs directory", e);
return;
}
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);
var line = "[" + time + "] ";
var type = msg.type.trim();
if (type === "message" || type === "highlight") {
// Format:
// [2014-01-01 00:00:00] <Arnold> Put that cookie down.. Now!!
line += "<" + msg.from + "> " + msg.text;
} else {
// Format:
// [2014-01-01 00:00:00] * Arnold quit
line += "* " + msg.from + " " + msg.type;
if (msg.text) {
line += " " + msg.text;
}
}
fs.appendFile(
// Quick fix to escape pre-escape channel names that contain % using %%,
// and / using %. **This does not escape all reserved words**
path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log",
line + "\n",
function(e) {
log.error("Failed to write user log", e);
}
);
};