Log4J not adding newlines between logfile entries

一笑奈何 提交于 2019-12-04 03:40:01

问题


I am just starting with log4j. I don't have a problem with it reading my properties file and actually logging events, but it seems to be appending everything to the end of the same line. My properties file looks like this:

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A2 is set to be a ConsoleAppender.
log4j.appender.A2=org.apache.log4j.FileAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x -
%m%n

# A2 uses PatternLayout.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-4r [%t] %-5p %c %x -
%m%n%

log4j.appender.A2.file=grocerylister.log

The above was modified from an example in log4j the Complete Manual. I have fruitlessly looked through the book and Google to get a listing of what all the options mean, to no avail.

I'm using log4j version 1.2.15 with Java 6. What can I do to get each log entry on a separate line and where can I find a list of what all the options are and what they do?


回答1:


Replace

log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x -
%m%n

with

log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Same for A2 + there remove the % after the n %m%n% -> m%n

Basically you seem to have a newline after the - character in your ConversionPattern lines. That would explain why the newline isn't output (%n --> outputs platform dependend newline character)

Btw. if you want to know what the options mean

javadoc: PatternLayout




回答2:


Are those %m%n on the same line as the rest? If not, that would explain it.

Oh, and for the 2nd appender, you have a % after %n. That doesn't look right either.



来源:https://stackoverflow.com/questions/1806378/log4j-not-adding-newlines-between-logfile-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!