JavaScript / ECMAScript

Log4j Pattern Layout Token in JS

Match Log4j / Logback PatternLayout conversion specifiers like `%d{yyyy-MM-dd}`, `%-5p`, or `%c{1}`.

Try it in the JS tester →

Pattern

regexJS
%(?:-?\d+)?(?:\.\d+)?[a-zA-Z](?:\{[^}]*\})?   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("%(?:-?\\d+)?(?:\\.\\d+)?[a-zA-Z](?:\\{[^}]*\\})?", "g");
const input = "%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1} - %m%n";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

% starts a conversion specifier. (?:-?\d+)? optionally matches a width modifier (negative for left-justify). (?:\.\d+)? optionally matches a max-width. [a-zA-Z] captures the conversion letter (d=date, p=priority, c=category, m=message, n=newline, etc.). (?:\{[^}]*\})? optionally matches a parameter in braces.

Examples

Input

%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1} - %m%n

Matches

  • %d{yyyy-MM-dd HH:mm:ss}
  • %-5p
  • %c{1}
  • %m
  • %n

Input

%t %p

Matches

  • %t
  • %p

Input

no log4j tokens

No match

Same pattern, other engines

← Back to Log4j Pattern Layout Token overview (all engines)