8000 add your own tokens to the patternLayout · rboss/log4js-node@e4bf405 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4bf405

Browse files
committed
add your own tokens to the patternLayout
1 parent 95568f3 commit e4bf405

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

lib/layouts.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ var dateFormat = require('./date_format')
1010
, "coloured": function() { return colouredLayout; }
1111
, "pattern": function (config) {
1212
var pattern = config.pattern || undefined;
13-
return patternLayout(pattern);
14-
}
13+
var tokens = config.tokens || undefined;
14+
return patternLayout(pattern, tokens);
15+
}
1516
}
1617
, colours = {
1718
ALL: "grey"
@@ -143,9 +144,9 @@ function messagePassThroughLayout (loggingEvent) {
143144
* Takes a pattern string and returns a layout function.
144145
* @author Stephan Strittmatter
145146
*/
146-
function patternLayout (pattern) {
147+
function patternLayout (pattern, tokens) {
147148
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
148-
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
149+
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnprx%])(\{([^\}]+)\})?|([^%]+)/;
149150

150151
pattern = pattern || TTCC_CONVERSION_PATTERN;
151152

@@ -221,6 +222,17 @@ function patternLayout (pattern) {
221222
case "%":
222223
replacement = "%";
223224
break;
225+
case "x":
226+
if(tokens[specifier]) {
227+
if(typeof(tokens[specifier]) === 'function') {
228+
replacement = tokens[specifier]();
229+
} else {
230+
replacement = tokens[specifier];
231+
}
232+
} else {
233+
replacement = matchedString;
234+
}
235+
break;
224236
default:
225237
replacement = matchedString;
226238
break;

test/layouts-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ assert = require('assert');
44
//used for patternLayout tests.
55
function test(args, pattern, value) {
66
var layout = args[0]
7-
, event = args[1];
7+
, event = args[1]
8+
, tokens = args[2];
89

9-
assert.equal(layout(pattern)(event), value);
10+
assert.equal(layout(pattern, tokens)(event), value);
1011
}
1112

1213
vows.describe('log4js layouts').addBatch({
@@ -175,8 +176,12 @@ vows.describe('log4js layouts').addBatch({
175176
level: {
176177
toString: function() { return "DEBUG"; }
177178
}
178-
}, layout = require('../lib/layouts').patternLayout;
179-
return [layout, event];
179+
}, layout = require('../lib/layouts').patternLayout
180+
, tokens = {
181+
testString: 'testStringToken',
182+
testFunction: function() { return 'testFunctionToken'; }
183+
};
184+
return [layout, event, tokens];
180185
},
181186

182187
'should default to "time logLevel loggerName - message"': function(args) {
@@ -246,6 +251,18 @@ vows.describe('log4js layouts').addBatch({
246251
},
247252
'%[%r%] should output colored time': function(args) {
248253
test(args, '%[%r%]', '\033[36m14:18:30\033[39m');
249-
}
254+
},
255+
'%x{testString} should output the string stored in tokens': function(args) {
256+
test(args, '%x{testString}', 'testStringToken');
257+
},
258+
'%x{testFunction} should output the result of the function stored in tokens': function(args) {
259+
test(args, '%x{testFunction}', 'testFunctionToken');
260+
},
261+
'%x{doesNotExist} should output the string stored in tokens': function(args) {
262+
test(args, '%x{doesNotExist}', '%x{doesNotExist}');
263+
},
264+
'%x should output the string stored in tokens': function(args) {
265+
test(args, '%x', '%x');
266+
},
250267
}
251268
}).export(module);

0 commit comments

Comments
 (0)
0