3AK
3AK

Reputation: 1361

log4j2 prints new line between stdout

Why does log4j prints a new line break in stdout appender?

my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
    <Appenders>
        <File name="FILE" fileName="<<FILEPATH>>\logfile.log"
            append="true">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5p | %l - %m%n" />
        </File>
        <File name="UIFILE" fileName="<<FILEPATH>>\uilogfile.log"
            append="true">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} | %m%n" />
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>[%-5p] %C{2} - %m%n</pattern>
            </PatternLayout>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.log4j.xml" level="INFO"/>
        <Logger name="com.foo" level="DEBUG" />
        <Logger name="com.foo.services.web.controllers.FOOLoggingController"
            level="INFO">
            <AppenderRef ref="UIFILE" />
        </Logger>
        <Root>
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="FILE" />
        </Root>
    </Loggers>
</Configuration>

everything works fine but I get a new line between outputs, don't know why!

enter image description here

I tried few things like removing %n from the pattern layout but when i do this, the log itself stops coming. The file output is good. It doesn't prints new line in between. Has someone faced a similar issue?

Upvotes: 4

Views: 6473

Answers (2)

Nyke
Nyke

Reputation: 11

Replacing with \n is not a good solution. You should make sure if there is another logging framework wrapping your logs to standard output. This happening in case of application servers, where deployed applications use a logging framework to send formatted messages to standard output, and server uses its own active logging framework to wrap the message around with its own format, before sending it to standard output. This causes double formatting & therefore double newlines. Check your runtime, if there is an active logger wrapping your messages.

Upvotes: 1

milijan
milijan

Reputation: 427

I solved this issue by replacing %n with \n inside the Console's pattern string. So the Console appender would look like:

<Console name="STDOUT" target="SYSTEM_OUT">
   <PatternLayout pattern ="[%-5p] %C{2} - %m \n" />
</Console>

Upvotes: 3

Related Questions