diff --git a/lib/date_format.js b/lib/date_format.js index bf36318..d75ce20 100644 --- a/lib/date_format.js +++ b/lib/date_format.js @@ -45,7 +45,7 @@ exports.asString = function(/*format,*/ date) { var vDay = addZero(date.getDate()); var vMonth = addZero(date.getMonth()+1); 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 vHour = addZero(date.getHours()); var vMinute = addZero(date.getMinutes()); diff --git a/lib/layouts.js b/lib/layouts.js index b171cee..9cfd035 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -120,6 +120,7 @@ function messagePassThroughLayout (loggingEvent) { * - %r time in toLocaleTimeString format * - %p log level * - %c log category + * - %h hostname * - %m log data * - %d date in various formats * - %% % @@ -143,7 +144,7 @@ function messagePassThroughLayout (loggingEvent) { */ function patternLayout (pattern, tokens) { 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; @@ -166,6 +167,8 @@ function patternLayout (pattern, tokens) { // Pick up special cases if (format == "ISO8601") { format = dateFormat.ISO8601_FORMAT; + } else if (format == "ISO8601_WITH_TZ_OFFSET") { + format = dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT; } else if (format == "ABSOLUTE") { format = dateFormat.ABSOLUTETIME_FORMAT; } else if (format == "DATE") { @@ -175,6 +178,10 @@ function patternLayout (pattern, tokens) { // Format the date return dateFormat.asString(format, loggingEvent.startTime); } + + function hostname() { + return os.hostname().toString(); + } function formatMessage(loggingEvent) { return formatLogData(loggingEvent.data); @@ -218,6 +225,7 @@ function patternLayout (pattern, tokens) { var replacers = { 'c': categoryName, 'd': formatAsDate, + 'h': hostname, 'm': formatMessage, 'n': endOfLine, 'p': logLevel, diff --git a/test/date_format-test.js b/test/date_format-test.js index d4a5cb5..6085843 100644 --- a/test/date_format-test.js +++ b/test/date_format-test.js @@ -39,6 +39,13 @@ vows.describe('date_format').addBatch({ dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date), '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); diff --git a/test/fileAppender-test.js b/test/fileAppender-test.js index cb4692a..9476ad6 100644 --- a/test/fileAppender-test.js +++ b/test/fileAppender-test.js @@ -222,7 +222,7 @@ vows.describe('log4js fileAppender').addBatch({ , logger; //this config file defines one file appender (to ./tmp-tests.log) //and sets the log level for "tests" to WARN - log4js.configure('test/log4js.json'); + log4js.configure('./test/log4js.json'); logger = log4js.getLogger('tests'); logger.info('this should not be written to the file'); logger.warn('this should be written to the file'); diff --git a/test/layouts-test.js b/test/layouts-test.js index 9af1242..c355bdd 100644 --- a/test/layouts-test.js +++ b/test/layouts-test.js @@ -209,6 +209,9 @@ vows.describe('log4js layouts').addBatch({ '%n should output a new line': function(args) { 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) { test(args, '%c{1}', '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'); }, '%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{ABSOLUTE}', '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 hh mm ss SSS}', '2010 12 05 14 18 30 045'); },