Merge pull request #150 from wood1986/master

layouts supports hostname and ISO8601_WITH_TZ_OFFSET_FORMAT
This commit is contained in:
Gareth Jones 2013-08-20 03:50:36 -07:00
commit 48dc22eb63
5 changed files with 23 additions and 3 deletions

View File

@ -45,7 +45,7 @@ exports.asString = function(/*format,*/ date) {
var vDay = addZero(date.getDate()); var vDay = addZero(date.getDate());
var vMonth = addZero(date.getMonth()+1); var vMonth = addZero(date.getMonth()+1);
var vYearLong = addZero(date.getFullYear()); var vYearLong = addZero(date.getFullYear());
var vYearShort = addZero(date.getFullYear().toString().substring(3,4)); var vYearShort = addZero(date.getFullYear().toString().substring(2,4));
var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort); var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
var vHour = addZero(date.getHours()); var vHour = addZero(date.getHours());
var vMinute = addZero(date.getMinutes()); var vMinute = addZero(date.getMinutes());

View File

@ -120,6 +120,7 @@ function messagePassThroughLayout (loggingEvent) {
* - %r time in toLocaleTimeString format * - %r time in toLocaleTimeString format
* - %p log level * - %p log level
* - %c log category * - %c log category
* - %h hostname
* - %m log data * - %m log data
* - %d date in various formats * - %d date in various formats
* - %% % * - %% %
@ -143,7 +144,7 @@ function messagePassThroughLayout (loggingEvent) {
*/ */
function patternLayout (pattern, tokens) { function patternLayout (pattern, tokens) {
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n"; var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnprx%])(\{([^\}]+)\})?|([^%]+)/; var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdhmnprx%])(\{([^\}]+)\})?|([^%]+)/;
pattern = pattern || TTCC_CONVERSION_PATTERN; pattern = pattern || TTCC_CONVERSION_PATTERN;
@ -166,6 +167,8 @@ function patternLayout (pattern, tokens) {
// Pick up special cases // Pick up special cases
if (format == "ISO8601") { if (format == "ISO8601") {
format = dateFormat.ISO8601_FORMAT; format = dateFormat.ISO8601_FORMAT;
} else if (format == "ISO8601_WITH_TZ_OFFSET") {
format = dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT;
} else if (format == "ABSOLUTE") { } else if (format == "ABSOLUTE") {
format = dateFormat.ABSOLUTETIME_FORMAT; format = dateFormat.ABSOLUTETIME_FORMAT;
} else if (format == "DATE") { } else if (format == "DATE") {
@ -175,6 +178,10 @@ function patternLayout (pattern, tokens) {
// Format the date // Format the date
return dateFormat.asString(format, loggingEvent.startTime); return dateFormat.asString(format, loggingEvent.startTime);
} }
function hostname() {
return os.hostname().toString();
}
function formatMessage(loggingEvent) { function formatMessage(loggingEvent) {
return formatLogData(loggingEvent.data); return formatLogData(loggingEvent.data);
@ -218,6 +225,7 @@ function patternLayout (pattern, tokens) {
var replacers = { var replacers = {
'c': categoryName, 'c': categoryName,
'd': formatAsDate, 'd': formatAsDate,
'h': hostname,
'm': formatMessage, 'm': formatMessage,
'n': endOfLine, 'n': endOfLine,
'p': logLevel, 'p': logLevel,

View File

@ -39,6 +39,13 @@ vows.describe('date_format').addBatch({
dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date), dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date),
'14:31:30.005' '14:31:30.005'
); );
},
'should provide a custom format': function(date) {
date.getTimezoneOffset = function() { return 120; };
assert.equal(
dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date),
'-0200.005.30.31.14.11.01.10'
);
} }
} }
}).export(module); }).export(module);

View File

@ -222,7 +222,7 @@ vows.describe('log4js fileAppender').addBatch({
, logger; , logger;
//this config file defines one file appender (to ./tmp-tests.log) //this config file defines one file appender (to ./tmp-tests.log)
//and sets the log level for "tests" to WARN //and sets the log level for "tests" to WARN
log4js.configure('test/log4js.json'); log4js.configure('./test/log4js.json');
logger = log4js.getLogger('tests'); logger = log4js.getLogger('tests');
logger.info('this should not be written to the file'); logger.info('this should not be written to the file');
logger.warn('this should be written to the file'); logger.warn('this should be written to the file');

View File

@ -209,6 +209,9 @@ vows.describe('log4js layouts').addBatch({
'%n should output a new line': function(args) { '%n should output a new line': function(args) {
test(args, '%n', '\n'); test(args, '%n', '\n');
}, },
'%h should output hostname' : function(args) {
test(args, '%h', require('os').hostname().toString());
},
'%c should handle category names like java-style package names': function(args) { '%c should handle category names like java-style package names': function(args) {
test(args, '%c{1}', 'tests'); test(args, '%c{1}', 'tests');
test(args, '%c{2}', 'of.tests'); test(args, '%c{2}', 'of.tests');
@ -221,9 +224,11 @@ vows.describe('log4js layouts').addBatch({
test(args, '%d', '2010-12-05 14:18:30.045'); test(args, '%d', '2010-12-05 14:18:30.045');
}, },
'%d should allow for format specification': function(args) { '%d should allow for format specification': function(args) {
test(args, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30-0000');
test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045'); test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045');
test(args, '%d{ABSOLUTE}', '14:18:30.045'); test(args, '%d{ABSOLUTE}', '14:18:30.045');
test(args, '%d{DATE}', '05 12 2010 14:18:30.045'); test(args, '%d{DATE}', '05 12 2010 14:18:30.045');
test(args, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
test(args, '%d{yyyy MM dd}', '2010 12 05'); test(args, '%d{yyyy MM dd}', '2010 12 05');
test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045'); test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
}, },