Skip to content
This repository
Browse code

add your own tokens to the patternLayout

  • Loading branch information...
commit e4bf405f201a0a63865171ca0232b3a5bcc3dcf1 1 parent 95568f3
Jan Schmidle bitcloud authored

Showing 2 changed files with 38 additions and 9 deletions. Show diff stats Hide diff stats

  1. +16 4 lib/layouts.js
  2. +22 5 test/layouts-test.js
20 lib/layouts.js
@@ -10,8 +10,9 @@ var dateFormat = require('./date_format')
10 10 , "coloured": function() { return colouredLayout; }
11 11 , "pattern": function (config) {
12 12 var pattern = config.pattern || undefined;
13   - return patternLayout(pattern);
14   - }
  13 + var tokens = config.tokens || undefined;
  14 + return patternLayout(pattern, tokens);
  15 + }
15 16 }
16 17 , colours = {
17 18 ALL: "grey"
@@ -143,9 +144,9 @@ function messagePassThroughLayout (loggingEvent) {
143 144 * Takes a pattern string and returns a layout function.
144 145 * @author Stephan Strittmatter
145 146 */
146   -function patternLayout (pattern) {
  147 +function patternLayout (pattern, tokens) {
147 148 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%])(\{([^\}]+)\})?|([^%]+)/;
149 150
150 151 pattern = pattern || TTCC_CONVERSION_PATTERN;
151 152
@@ -221,6 +222,17 @@ function patternLayout (pattern) {
221 222 case "%":
222 223 replacement = "%";
223 224 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;
224 236 default:
225 237 replacement = matchedString;
226 238 break;
27 test/layouts-test.js
@@ -4,9 +4,10 @@ assert = require('assert');
4 4 //used for patternLayout tests.
5 5 function test(args, pattern, value) {
6 6 var layout = args[0]
7   - , event = args[1];
  7 + , event = args[1]
  8 + , tokens = args[2];
8 9
9   - assert.equal(layout(pattern)(event), value);
  10 + assert.equal(layout(pattern, tokens)(event), value);
10 11 }
11 12
12 13 vows.describe('log4js layouts').addBatch({
@@ -175,8 +176,12 @@ vows.describe('log4js layouts').addBatch({
175 176 level: {
176 177 toString: function() { return "DEBUG"; }
177 178 }
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];
180 185 },
181 186
182 187 'should default to "time logLevel loggerName - message"': function(args) {
@@ -246,6 +251,18 @@ vows.describe('log4js layouts').addBatch({
246 251 },
247 252 '%[%r%] should output colored time': function(args) {
248 253 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 + },
250 267 }
251 268 }).export(module);

0 comments on commit e4bf405

Please sign in to comment.
Something went wrong with that request. Please try again.