Add global logging helper
This commit is contained in:
parent
fd36eea0ed
commit
ede3131168
@ -30,6 +30,7 @@ rules:
|
||||
spaced-comment: [2, always]
|
||||
|
||||
globals:
|
||||
log: false
|
||||
$: false
|
||||
Favico: false
|
||||
Handlebars: false
|
||||
|
@ -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",
|
||||
|
@ -75,6 +75,8 @@ function Client(manager, name, config) {
|
||||
delay += 1000;
|
||||
});
|
||||
}
|
||||
|
||||
log.info("User '" + name + "' loaded");
|
||||
}
|
||||
|
||||
Client.prototype.emit = function(event, data) {
|
||||
|
@ -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);
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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");
|
||||
|
@ -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("");
|
||||
}
|
||||
});
|
||||
|
@ -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.");
|
||||
}
|
||||
});
|
||||
|
@ -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 + "'.");
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
|
@ -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]);
|
||||
}
|
||||
|
||||
|
31
src/log.js
Normal file
31
src/log.js
Normal 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));
|
||||
};
|
@ -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
|
||||
|
@ -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 + ")";
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user