Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PatternLayout < > escape character in log4j2

Tags:

java

log4j2

I'm trying to output a pattern that contains the characters < > in the form

08:06:41 DEBUG 69 [ch.fhnw.edu.rental.model.Movie] - <Movie 'Cars' created>

Where <Movie 'Cars' created> is the log-message.

My PatternLayout is as follows:

            <PatternLayout pattern="%d{HH:mm:ss} %-5p %L [%c] - <%m%n>"/>

Unfortunately this produces the error

[Fatal Error] log4j2.xml:5:65: The value of attribute "pattern" associated with an element type "PatternLayout" must not contain the '<' character.

How can I escape the < > characters without hardcoding them into the source code?

like image 802
CuriousIndeed Avatar asked Jun 25 '26 08:06

CuriousIndeed


1 Answers

You can use HTML entity codes for this: &lt; for < and &gt; for >.

Be careful where you place them:

<PatternLayout pattern="%d{HH:mm:ss} %-5p %L [%c] - &lt;%m&gt;%n"/>

In the above example, the &gt; needs to be placed before the %n since %n generates a newline.

like image 108
andrewJames Avatar answered Jun 27 '26 20:06

andrewJames



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!