Log4j Pattern Layout Token in PY
Match Log4j / Logback PatternLayout conversion specifiers like `%d{yyyy-MM-dd}`, `%-5p`, or `%c{1}`.
Try it in the PY tester →Pattern
regexPY
%(?:-?\d+)?(?:\.\d+)?[a-zA-Z](?:\{[^}]*\})? (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"%(?:-?\d+)?(?:\.\d+)?[a-zA-Z](?:\{[^}]*\})?")
input_text = "%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1} - %m%n"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
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%nMatches
%d{yyyy-MM-dd HH:mm:ss}%-5p%c{1}%m%n
Input
%t %pMatches
%t%p
Input
no log4j tokensNo match
—