antihero989
antihero989

Reputation: 494

"\n" line break ignored in log.txt file using log4j2

I'm currently having a problem with log4j2 ignoring "\n" when writing to my log file. For example, using log4j2 to log in console with the following code

log.info("Hello \nWorld");

will output to console:

Hello
World

Inside my log file log.txt, the same code produces:

Hello World

ultimately ignoring the "\n".

My log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - 
%msg%n" />
    </Console>
    <File name="MonthlyCBK" fileName="${sys:MonthlyCBK}" append="true">
        <PatternLayout pattern="%d{MM-dd-yy HH:mm:ss} %-5level %logger{36} - 
%msg%n"/>
    </File>
</Appenders>
<Loggers>
    <Root level="debug">
        <AppenderRef ref="Console" level="info" />
        <AppenderRef ref="MonthlyCBK" level="debug"/>
    </Root>
</Loggers>
</Configuration>

Am I missing a possible option inside my log4j2 that allows line breaks in my file? Or does log4j2 not allow new lines when writing to log files?

Upvotes: 1

Views: 2254

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

You can use the printf method:

logger.printf(Level.INFO, "Hello %n world");

Upvotes: 2

Related Questions