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!
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?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With