Log4j : is it required to have multiple logger obj

2019-08-30 00:44发布

i was trying to redirect the log to console and two different log files..

this is my log4j.properties file:

log4j.rootLogger = DEBUG, console, file, csv


log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{HH:mm:ss} %5p [%t] - %c.%M - %m%n


log4j.appender.file = org.apache.log4j.FileAppender
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d %5p [%t] - %L %c.%M - %m%n
log4j.appender.file.File=./log/logfile.log


log4j.appender.csv = org.apache.log4j.FileAppender
log4j.appender.csv.layout = org.apache.log4j.PatternLayout
log4j.appender.csv.layout.ConversionPattern =%m%n
log4j.appender.csv.File = ./log/log.csv

now the problem is that the logging is happening, but both the files have both the logs that were meant to be logged separately in different files.. i really dont know where am i going wrong! can anyone please help me out??

1条回答
Explosion°爆炸
2楼-- · 2019-08-30 01:07

Log4j does not easily allow for splitting logs between multiple files. The basic idea is that all log statements are considered equal and should be treated equally.

If you were using slf4j as the API you could easily switch the backend from log4j to logback which has SiftingAppender for exactly this purpose.

http://logback.qos.ch/manual/loggingSeparation.html

查看更多
登录 后发表回答