Skip to content

Commit

Permalink
Merge pull request #1163 from log4js-node/added-pattern-alias
Browse files Browse the repository at this point in the history
refactor date pattern names for clarity (when %date actually means %datetime)
  • Loading branch information
lamweili committed Jan 23, 2022
2 parents 58b7353 + 26ed377 commit 4deffd1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/layouts.md
Expand Up @@ -116,7 +116,7 @@ Fields can be any of:
* `%c` log category
* `%h` hostname
* `%m` log data
* `%d` date, formatted - default is `ISO8601`, format options are: `ISO8601`, `ISO8601_WITH_TZ_OFFSET`, `ABSOLUTE`, `DATE`, or any string compatible with the [date-format](https://www.npmjs.com/package/date-format) library. e.g. `%d{DATE}`, `%d{yyyy/MM/dd-hh.mm.ss}`
* `%d` date, formatted - default is `ISO8601`, format options are: `ISO8601`, `ISO8601_WITH_TZ_OFFSET`, `ABSOLUTETIME`, `DATETIME`, or any string compatible with the [date-format](https://www.npmjs.com/package/date-format) library. e.g. `%d{DATETIME}`, `%d{yyyy/MM/dd-hh.mm.ss}`
* `%%` % - for when you want a literal `%` in your output
* `%n` newline
* `%z` process id (from `process.pid`)
Expand Down
33 changes: 25 additions & 8 deletions lib/layouts.js
Expand Up @@ -2,6 +2,7 @@ const dateFormat = require('date-format');
const os = require('os');
const util = require('util');
const path = require('path');
const debug = require('debug')('log4js:layouts');

const styles = {
// styles
Expand Down Expand Up @@ -144,14 +145,30 @@ function patternLayout(pattern, tokens) {
if (specifier) {
format = specifier;
// 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') {
format = dateFormat.DATETIME_FORMAT;
switch (format) {
case 'ISO8601':
case 'ISO8601_FORMAT':
format = dateFormat.ISO8601_FORMAT;
break;
case 'ISO8601_WITH_TZ_OFFSET':
case 'ISO8601_WITH_TZ_OFFSET_FORMAT':
format = dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT;
break;
case 'ABSOLUTE':
debug("DEPRECATION: Pattern %d{ABSOLUTE} is deprecated and replaced by %d{ABSOLUTETIME}.");
// falls through
case 'ABSOLUTETIME':
case 'ABSOLUTETIME_FORMAT':
format = dateFormat.ABSOLUTETIME_FORMAT;
break;
case 'DATE':
debug("DEPRECATION: Pattern %d{DATE} is deprecated and replaced by %d{DATETIME}.");
// falls through
case 'DATETIME':
case 'DATETIME_FORMAT':
format = dateFormat.DATETIME_FORMAT;
break;
// no default
}
}
// Format the date
Expand Down
50 changes: 37 additions & 13 deletions test/tap/layouts-test.js
Expand Up @@ -412,33 +412,49 @@ test("log4js layouts", batch => {
"2010-12-05T14:18:30.045"
);

// Commenting this test out, because it does not work in travis
// for reasons I do not understand.
// testPattern(
// assert,
// layout,
// event,
// tokens,
// "%d{ISO8601_WITH_TZ_OFFSET}",
// "2010-12-05T03:18:30.045+1000"
// );
testPattern(
assert,
layout,
event,
tokens,
"%d{ISO8601_WITH_TZ_OFFSET}",
"2010-12-05T14:18:30.045+10:00"
);

testPattern(
assert,
layout,
event,
tokens,
"%d{ABSOLUTE}",
"%d{ABSOLUTE}", // deprecated
"14:18:30.045"
);
testPattern(
assert,
layout,
event,
tokens,
"%d{DATE}",
"%d{ABSOLUTETIME}",
"14:18:30.045"
);

testPattern(
assert,
layout,
event,
tokens,
"%d{DATE}", // deprecated
"05 12 2010 14:18:30.045"
);
testPattern(
assert,
layout,
event,
tokens,
"%d{DATETIME}",
"05 12 2010 14:18:30.045"
);

testPattern(
assert,
layout,
Expand Down Expand Up @@ -617,7 +633,15 @@ test("log4js layouts", batch => {
layout,
event,
tokens,
"%m%n %c{2} at %d{ABSOLUTE} cheese %p%n",
"%m%n %c{2} at %d{ABSOLUTE} cheese %p%n", // deprecated
`this is a test${EOL} of.tests at 14:18:30.045 cheese DEBUG${EOL}`
);
testPattern(
assert,
layout,
event,
tokens,
"%m%n %c{2} at %d{ABSOLUTETIME} cheese %p%n",
`this is a test${EOL} of.tests at 14:18:30.045 cheese DEBUG${EOL}`
);
assert.end();
Expand Down

0 comments on commit 4deffd1

Please sign in to comment.