Log4J not adding newlines between logfile entries

2019-02-23 09:00发布

问题:

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.