10000 Merge pull request #113 from bitcloud/patternLayout_tokens · rboss/log4js-node@22da622 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22da622

Browse files
author
Gareth Jones
committed
Merge pull request log4js-node#113 from bitcloud/patternLayout_tokens
add your own tokens to the patternLayout
2 parents 95568f3 + a3bdac8 commit 22da622

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

examples/patternLayout-tokens.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var log4js = require('./lib/log4js');
2+
3+
var config = {
4+
"appenders": [
5+
{
6+
"type": "console",
7+
"layout": {
8+
"type": "pattern",
9+
"pattern": "%[%r (%x{pid}) %p %c -%] %m%n",
10+
"tokens": {
11+
"pid" : function() { return process.pid; }
12+
}
13+
}
14+
}
15+
]
16+
};
17+
18+
log4js.configure(config, {});
19+
20+
var logger = log4js.getLogger("app");
21+
logger.info("Test log message");

lib/layouts.js

Lines changed: 30 additions & 5 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"
@@ -139,13 +140,26 @@ function messagePassThroughLayout (loggingEvent) {
139140
* - %d date in various formats
140141
* - %% %
141142
* - %n newline
143+
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
142144
* You can use %[ and %] to define a colored block.
143-
* Takes a pattern string and returns a layout function.
145+
*
146+
* Tokens are specified as simple key:value objects.
147+
* The key represents the token name whereas the value can be a string or function
148+
* which is called to extract the value to put in the log message. If token is not
149+
* found, it doesn't replace the field.
150+
*
151+
* A sample token would be: { "pid" : function() { return process.pid; } }
152+
*
153+
* Takes a pattern string, array of tokens and returns a layout function.
154+
* @param {String} Log format pattern String
155+
* @param {object} map object of different tokens
156+
* @return {Function}
144157
* @author Stephan Strittmatter
158+
* @author Jan Schmidle
145159
*/
146-
function patternLayout (pattern) {
160+
function patternLayout (pattern, tokens) {
147161
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
148-
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
162+
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnprx%])(\{([^\}]+)\})?|([^%]+)/;
149163

150164
pattern = pattern || TTCC_CONVERSION_PATTERN;
151165

@@ -221,6 +235,17 @@ function patternLayout (pattern) {
221235
case "%":
222236
replacement = "%";
223237
break;
238+
case "x":
239+
if(typeof(tokens[specifier]) !== 'undefined') {
240+
if(typeof(tokens[specifier]) === 'function') {
241+
replacement = tokens[specifier]();
242+
} else {
243+
replacement = tokens[specifier];
244+
}
245+
} else {
246+
replacement = matchedString;
247+
}
248+
break;
224249
default:
225250
replacement = matchedString;
226251
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