From 7162562cdceb979ba7cd1c4abc44adb1d6f0f8a9 Mon Sep 17 00:00:00 2001 From: jomo Date: Tue, 3 Feb 2015 22:28:35 +0100 Subject: [PATCH] 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* --- modules/logging.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/logging.js b/modules/logging.js index 7519a23..e7bd854 100644 --- a/modules/logging.js +++ b/modules/logging.js @@ -15,19 +15,24 @@ function split_args(args) { return text; } -function log(level, args) { +function log(level, args, logger) { + logger = logger || console.log; 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() { log(" INFO", arguments); }; exp.warn = function() { - log(" WARN", arguments); + log(" WARN", arguments, console.warn); }; exp.error = function() { - log("ERROR", arguments); + log("ERROR", arguments, console.error); }; if (config.debug_enabled || process.env.DEBUG === "true") { exp.debug = function() {