prepend log prefix to each log line, use console.warn & console.error

this has two effects:
1. errors will go to stderr
2. errors (and other multi-line messages) will have the log prefix on *each line*
This commit is contained in:
jomo 2015-02-03 22:28:35 +01:00
parent 3958ad6a26
commit 7162562cdc

View File

@ -15,19 +15,24 @@ function split_args(args) {
return text; return text;
} }
function log(level, args) { function log(level, args, logger) {
logger = logger || console.log;
var time = new Date().toISOString(); var time = new Date().toISOString();
console.log(time + " " + (cluster.worker && cluster.worker.id || "M") + " " + level + ": " + split_args(args)); var clid = (cluster.worker && cluster.worker.id || "M");
var lines = split_args(args).split("\n");
for (var i = 0, l = lines.length; i < l; i++) {
logger(time + " " + clid + " " + level + ": " + lines[i]);
}
} }
exp.log = function() { exp.log = function() {
log(" INFO", arguments); log(" INFO", arguments);
}; };
exp.warn = function() { exp.warn = function() {
log(" WARN", arguments); log(" WARN", arguments, console.warn);
}; };
exp.error = function() { exp.error = function() {
log("ERROR", arguments); log("ERROR", arguments, console.error);
}; };
if (config.debug_enabled || process.env.DEBUG === "true") { if (config.debug_enabled || process.env.DEBUG === "true") {
exp.debug = function() { exp.debug = function() {