Log4j Pattern Layout Token in GO
Match Log4j / Logback PatternLayout conversion specifiers like `%d{yyyy-MM-dd}`, `%-5p`, or `%c{1}`.
Try it in the GO tester →Pattern
regexGO
%(?:-?\d+)?(?:\.\d+)?[a-zA-Z](?:\{[^}]*\})? (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`%(?:-?\d+)?(?:\.\d+)?[a-zA-Z](?:\{[^}]*\})?`)
input := `%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1} - %m%n`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
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
—