PatternLayout < > escape character in log4j2

Viewed 462

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?

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.

Related