Jack Gore
Jack Gore

Reputation: 4252

Multiple regular expression replacements log4j2 xml pattern tag

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?

Upvotes: 2

Views: 4793

Answers (1)

wangyuntao
wangyuntao

Reputation: 857

You need to nest one %replace statement inside another.

Try this:

%replace{ %replace{%msg}{&apos;}{&quot;} }{\{|\}}{&quot;&quot;}

The result is:

"" "name":"person", age:"42" ""

I hope it helps.

Upvotes: 3

Related Questions