Merge pull request #113 from bitcloud/patternLayout_tokens
add your own tokens to the patternLayout
This commit is contained in:
21
examples/patternLayout-tokens.js
Normal file
21
examples/patternLayout-tokens.js
Normal file
@@ -0,0 +1,21 @@
|
||||
var log4js = require('./lib/log4js');
|
||||
|
||||
var config = {
|
||||
"appenders": [
|
||||
{
|
||||
"type": "console",
|
||||
"layout": {
|
||||
"type": "pattern",
|
||||
"pattern": "%[%r (%x{pid}) %p %c -%] %m%n",
|
||||
"tokens": {
|
||||
"pid" : function() { return process.pid; }
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
log4js.configure(config, {});
|
||||
|
||||
var logger = log4js.getLogger("app");
|
||||
logger.info("Test log message");
|
||||
@@ -10,8 +10,9 @@ var dateFormat = require('./date_format')
|
||||
, "coloured": function() { return colouredLayout; }
|
||||
, "pattern": function (config) {
|
||||
var pattern = config.pattern || undefined;
|
||||
return patternLayout(pattern);
|
||||
}
|
||||
var tokens = config.tokens || undefined;
|
||||
return patternLayout(pattern, tokens);
|
||||
}
|
||||
}
|
||||
, colours = {
|
||||
ALL: "grey"
|
||||
@@ -139,13 +140,26 @@ function messagePassThroughLayout (loggingEvent) {
|
||||
* - %d date in various formats
|
||||
* - %% %
|
||||
* - %n newline
|
||||
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
|
||||
* You can use %[ and %] to define a colored block.
|
||||
* Takes a pattern string and returns a layout function.
|
||||
*
|
||||
* Tokens are specified as simple key:value objects.
|
||||
* The key represents the token name whereas the value can be a string or function
|
||||
* which is called to extract the value to put in the log message. If token is not
|
||||
* found, it doesn't replace the field.
|
||||
*
|
||||
* A sample token would be: { "pid" : function() { return process.pid; } }
|
||||
*
|
||||
* Takes a pattern string, array of tokens and returns a layout function.
|
||||
* @param {String} Log format pattern String
|
||||
* @param {object} map object of different tokens
|
||||
* @return {Function}
|
||||
* @author Stephan Strittmatter
|
||||
* @author Jan Schmidle
|
||||
*/
|
||||
function patternLayout (pattern) {
|
||||
function patternLayout (pattern, tokens) {
|
||||
var TTCC_CONVERSION_PATTERN = "%r %p %c - %m%n";
|
||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
|
||||
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([\[\]cdmnprx%])(\{([^\}]+)\})?|([^%]+)/;
|
||||
|
||||
pattern = pattern || TTCC_CONVERSION_PATTERN;
|
||||
|
||||
@@ -221,6 +235,17 @@ function patternLayout (pattern) {
|
||||
case "%":
|
||||
replacement = "%";
|
||||
break;
|
||||
case "x":
|
||||
if(typeof(tokens[specifier]) !== 'undefined') {
|
||||
if(typeof(tokens[specifier]) === 'function') {
|
||||
replacement = tokens[specifier]();
|
||||
} else {
|
||||
replacement = tokens[specifier];
|
||||
}
|
||||
} else {
|
||||
replacement = matchedString;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
replacement = matchedString;
|
||||
break;
|
||||
|
||||
@@ -4,9 +4,10 @@ assert = require('assert');
|
||||
//used for patternLayout tests.
|
||||
function test(args, pattern, value) {
|
||||
var layout = args[0]
|
||||
, event = args[1];
|
||||
, event = args[1]
|
||||
, tokens = args[2];
|
||||
|
||||
assert.equal(layout(pattern)(event), value);
|
||||
assert.equal(layout(pattern, tokens)(event), value);
|
||||
}
|
||||
|
||||
vows.describe('log4js layouts').addBatch({
|
||||
@@ -175,8 +176,12 @@ vows.describe('log4js layouts').addBatch({
|
||||
level: {
|
||||
toString: function() { return "DEBUG"; }
|
||||
}
|
||||
}, layout = require('../lib/layouts').patternLayout;
|
||||
return [layout, event];
|
||||
}, layout = require('../lib/layouts').patternLayout
|
||||
, tokens = {
|
||||
testString: 'testStringToken',
|
||||
testFunction: function() { return 'testFunctionToken'; }
|
||||
};
|
||||
return [layout, event, tokens];
|
||||
},
|
||||
|
||||
'should default to "time logLevel loggerName - message"': function(args) {
|
||||
@@ -246,6 +251,18 @@ vows.describe('log4js layouts').addBatch({
|
||||
},
|
||||
'%[%r%] should output colored time': function(args) {
|
||||
test(args, '%[%r%]', '\033[36m14:18:30\033[39m');
|
||||
}
|
||||
},
|
||||
'%x{testString} should output the string stored in tokens': function(args) {
|
||||
test(args, '%x{testString}', 'testStringToken');
|
||||
},
|
||||
'%x{testFunction} should output the result of the function stored in tokens': function(args) {
|
||||
test(args, '%x{testFunction}', 'testFunctionToken');
|
||||
},
|
||||
'%x{doesNotExist} should output the string stored in tokens': function(args) {
|
||||
test(args, '%x{doesNotExist}', '%x{doesNotExist}');
|
||||
},
|
||||
'%x should output the string stored in tokens': function(args) {
|
||||
test(args, '%x', '%x');
|
||||
},
|
||||
}
|
||||
}).export(module);
|
||||
|
||||
Reference in New Issue
Block a user