Hi I am configuring log4j2 via an xml file. I have set up an appender and its is logging properly. I am having issue configuring two regular expressions to replace text in the %message variable of my log.
I am logging messages in my java code like this:
logger.info("{ 'name':'person', age:'42' }");
I am sending these logs to Kafka and want to replace all { or } with "" and all ' with ".
The current pattern I am using looks like this:
<pattern>{ "logTimestamp":"%date{ISO8601}", %replace{%replace{%message{nolookups}}{\\"|\\'|"}{'}}{{|}}{},"host":"${hostname}" }</pattern>
However this is not working and I am getting the following parsed message as a result of the replacement:
{ "logTimestamp":"2017-03-27T11:11:17,247", %replace}{"}{'},"host":"hostname" }
What is the proper way to match and replace two patterns with log4j2 %replace?
You need to nest one %replace
statement inside another.
Try this:
%replace{ %replace{%msg}{'}{"} }{\{|\}}{""}
The result is:
"" "name":"person", age:"42" ""
I hope it helps.