Skip to content

Commit

Permalink
feat(#130): Add support for MDC in pattern layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemsrs committed Jun 17, 2017
1 parent 8befd8a commit 6e15364
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
16 changes: 14 additions & 2 deletions lib/layouts.js
Expand Up @@ -162,6 +162,7 @@ function dummyLayout(loggingEvent) {
* - %n newline
* - %z pid
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
* - %X{<tokenname>} add dynamic tokens to your log. Tokens are specified in logger context
* You can use %[ and %] to define a colored block.
*
* Tokens are specified as simple key:value objects.
Expand All @@ -181,7 +182,7 @@ function dummyLayout(loggingEvent) {
*/
function patternLayout(pattern, tokens, timezoneOffset) {
const TTCC_CONVERSION_PATTERN = '%r %p %c - %m%n';
const regex = /%(-?[0-9]+)?(\.?[0-9]+)?([[\]cdhmnprzxy%])(\{([^}]+)\})?|([^%]+)/;
const regex = /%(-?[0-9]+)?(\.?[0-9]+)?([[\]cdhmnprzxXy%])(\{([^}]+)\})?|([^%]+)/;

pattern = pattern || TTCC_CONVERSION_PATTERN;

Expand Down Expand Up @@ -273,6 +274,16 @@ function patternLayout(pattern, tokens, timezoneOffset) {
return null;
}

function contextDefined(loggingEvent, specifier) {
const resolver = loggingEvent.context[specifier];

if (typeof resolver !== 'undefined') {
return typeof resolver === 'function' ? resolver(loggingEvent) : resolver;
}

return null;
}

/* eslint quote-props:0 */
const replacers = {
'c': categoryName,
Expand All @@ -287,7 +298,8 @@ function patternLayout(pattern, tokens, timezoneOffset) {
'y': clusterInfo,
'z': pid,
'%': percent,
'x': userDefined
'x': userDefined,
'X': contextDefined
};

function replaceToken(conversionCharacter, loggingEvent, specifier) {
Expand Down
48 changes: 37 additions & 11 deletions test/tap/layouts-test.js
Expand Up @@ -201,6 +201,16 @@ test('log4js layouts', (batch) => {
});

batch.test('patternLayout', (t) => {
const tokens = {
testString: 'testStringToken',
testFunction: function () {
return 'testFunctionToken';
},
fnThatUsesLogEvent: function (logEvent) {
return logEvent.level.toString();
}
};

const event = {
data: ['this is a test'],
startTime: new Date('2010-12-05T14:18:30.045Z'), // new Date(2010, 11, 5, 14, 18, 30, 45),
Expand All @@ -209,21 +219,12 @@ test('log4js layouts', (batch) => {
toString: function () {
return 'DEBUG';
}
}
},
context: tokens
};

const layout = require('../../lib/layouts').patternLayout;

const tokens = {
testString: 'testStringToken',
testFunction: function () {
return 'testFunctionToken';
},
fnThatUsesLogEvent: function (logEvent) {
return logEvent.level.toString();
}
};

// override getTimezoneOffset
event.startTime.getTimezoneOffset = function () {
return 0;
Expand Down Expand Up @@ -369,6 +370,31 @@ test('log4js layouts', (batch) => {
assert.end();
});

t.test('%X{testString} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{testString}', 'testStringToken');
assert.end();
});

t.test('%X{testFunction} should output the result of the function stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{testFunction}', 'testFunctionToken');
assert.end();
});

t.test('%X{doesNotExist} should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X{doesNotExist}', 'null');
assert.end();
});

t.test('%X{fnThatUsesLogEvent} should be able to use the logEvent', (assert) => {
testPattern(assert, layout, event, {}, '%X{fnThatUsesLogEvent}', 'DEBUG');
assert.end();
});

t.test('%X should output the string stored in tokens', (assert) => {
testPattern(assert, layout, event, {}, '%X', 'null');
assert.end();
});

t.end();
});

Expand Down

0 comments on commit 6e15364

Please sign in to comment.