all tests pass, now with proper console.log formatting

This commit is contained in:
Gareth Jones
2011-07-13 18:12:29 +10:00
parent f89d54b66e
commit 5868856a7d
6 changed files with 143 additions and 102 deletions

View File

@@ -1,4 +1,6 @@
var dateFormat = require('./date_format')
, util = require('util')
, replacementRegExp = /%[sdj]/g
, layoutMakers = {
"messagePassThrough": function() { return messagePassThroughLayout; },
"basic": function() { return basicLayout; },
@@ -10,31 +12,34 @@ var dateFormat = require('./date_format')
}
};
/**
* BasicLayout is a simple layout for storing the logs. The logs are stored
* in following format:
* <pre>
* [startTime] [logLevel] categoryName - message\n
* </pre>
*
* @author Stephan Strittmatter
*/
function basicLayout (loggingEvent) {
var timestampLevelAndCategory = '[' + dateFormat.asString(loggingEvent.startTime) + '] ';
timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] ';
timestampLevelAndCategory += loggingEvent.categoryName + ' - ';
function formatLogData(logData) {
var output = ""
, data = Array.isArray(logData) ? logData.slice() : Array.prototype.slice.call(arguments)
, format = data.shift();
var output = timestampLevelAndCategory + loggingEvent.message;
if (loggingEvent.exception) {
output += '\n'
output += timestampLevelAndCategory;
if (loggingEvent.exception.stack) {
output += loggingEvent.exception.stack;
} else {
output += loggingEvent.exception.name + ': '+loggingEvent.exception.message;
}
if (typeof format === "string") {
output = format.replace(replacementRegExp, function(match) {
switch (match) {
case "%s": return new String(data.shift());
case "%d": return new Number(data.shift());
case "%j": return JSON.stringify(data.shift());
default:
return match;
};
});
if (data.length > 0) {
output += '\n';
}
}
data.forEach(function (item) {
if (item.stack) {
output += item.stack;
} else {
output += util.inspect(item);
}
});
return output;
}
@@ -60,8 +65,34 @@ function colorize (str, style) {
'red' : [31, 39],
'yellow' : [33, 39]
};
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
return style ? '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm' : str;
}
function timestampLevelAndCategory(loggingEvent, colour) {
var output = colorize(
formatLogData(
'[%s] [%s] %s - '
, dateFormat.asString(loggingEvent.startTime)
, loggingEvent.level
, loggingEvent.categoryName
)
, colour
);
return output;
}
/**
* BasicLayout is a simple layout for storing the logs. The logs are stored
* in following format:
* <pre>
* [startTime] [logLevel] categoryName - message\n
* </pre>
*
* @author Stephan Strittmatter
*/
function basicLayout (loggingEvent) {
return timestampLevelAndCategory(loggingEvent) + formatLogData(loggingEvent.data);
}
/**
@@ -69,28 +100,11 @@ function colorize (str, style) {
* same as basicLayout, but with colours.
*/
function colouredLayout (loggingEvent) {
var timestampLevelAndCategory = colorize('[' + dateFormat.asString(loggingEvent.startTime) + '] ', 'grey');
timestampLevelAndCategory += colorize(
'[' + loggingEvent.level.toString() + '] ', loggingEvent.level.colour
);
timestampLevelAndCategory += colorize(loggingEvent.categoryName + ' - ', 'grey');
var output = timestampLevelAndCategory + loggingEvent.message;
if (loggingEvent.exception) {
output += '\n'
output += timestampLevelAndCategory;
if (loggingEvent.exception.stack) {
output += loggingEvent.exception.stack;
} else {
output += loggingEvent.exception.name + ': '+loggingEvent.exception.message;
}
}
return output;
return timestampLevelAndCategory(loggingEvent, loggingEvent.level.colour) + formatLogData(loggingEvent.data);
}
function messagePassThroughLayout (loggingEvent) {
return loggingEvent.message;
return formatLogData(loggingEvent.data);
}
/**

View File

@@ -181,21 +181,16 @@ function configureLevels(levels) {
* @constructor
* @param {String} categoryName name of category
* @param {Log4js.Level} level level of message
* @param {String} message message to log
* @param {Array} data objects to log
* @param {Log4js.Logger} logger the associated logger
* @author Seth Chisamore
*/
function LoggingEvent (categoryName, level, message, exception, logger) {
function LoggingEvent (categoryName, level, data, logger) {
this.startTime = new Date();
this.categoryName = categoryName;
this.message = message;
this.data = data;
this.level = level;
this.logger = logger;
if (exception && exception.message && exception.name) {
this.exception = exception;
} else if (exception) {
this.exception = new Error(sys.inspect(exception));
}
}
/**
@@ -222,8 +217,9 @@ Logger.prototype.removeLevel = function() {
delete this.level;
};
Logger.prototype.log = function(logLevel, message, exception) {
var loggingEvent = new LoggingEvent(this.category, logLevel, message, exception, this);
Logger.prototype.log = function(logLevel, args) {
var data = Array.prototype.slice.call(args)
, loggingEvent = new LoggingEvent(this.category, logLevel, data, this);
this.emit("log", loggingEvent);
};
@@ -238,9 +234,9 @@ Logger.prototype.isLevelEnabled = function(otherLevel) {
return this.isLevelEnabled(level);
};
Logger.prototype[levelString.toLowerCase()] = function (message, exception) {
Logger.prototype[levelString.toLowerCase()] = function () {
if (this.isLevelEnabled(level)) {
this.log(level, message, exception);
this.log(level, arguments);
}
};
}