简体   繁体   中英

How to generate 5(more than 2) log files using 5 log4j properties in java

How to generate 5(more than 2) log files using 5 log4j properties in java, I done with 2 files it's working fine but I need to create more than 2 files but it's not creating more than 2 files.

Thanks.

configuration:

    # This is first log file

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C://First_logger.out
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

# This is second log file

log4j.rootLogger=INFO, SecondLog

log4j.appender.SecondLog=org.apache.log4j.RollingFileAppender
log4j.appender.SecondLog.File=C://second-report.out
log4j.appender.SecondLog.layout=org.apache.log4j.PatternLayout
log4j.appender.SecondLog.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

log4j.category.SecondLogger=DEBUG, SecondLog
log4j.additivity.SecondLogger=false

# This is Third log File


log4j.rootLogger=INFO, ThirdLog

log4j.appender.ThirdLog=org.apache.log4j.RollingFileAppender
log4j.appender.ThirdLog.File=C://third-report.out
log4j.appender.ThirdLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ThirdLog.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

log4j.category.ThirdLogger=DEBUG, ThirdLog
log4j.additivity.ThirdLogger=false

I reproduce the problem with this configuration, the First_logger.out is not created but second-report.out and third-report.out are.

The three files are generated if we add the following line for the first block:

log4j.category.fileLogger=DEBUG, file

Besides, the three files are also generated if the block for the first file is put at the end of the configuration file, with the original configuration. Ath the moment, I don't know why the order of blocks matters.

Hey I got the solution for this. I created multiple log4j property file's but, Actually we have to write only one log4j property file for multiple property file it not working, log4j initialize only one property file so we have to write 1 log4j file and in that file we have to configure other multiple loggers. Like below

    # This is only one  log4j property file

    log4j.rootLogger=INFO, file, SecondLog, ThirdLog

    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=C://First_logger.out
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

   log4j.category.fileLogger=DEBUG, file

    log4j.appender.SecondLog=org.apache.log4j.RollingFileAppender
    log4j.appender.SecondLog.File=C://second-report.out
    log4j.appender.SecondLog.layout=org.apache.log4j.PatternLayout
    log4j.appender.SecondLog.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

    log4j.category.SecondLogger=DEBUG, SecondLog
    log4j.additivity.SecondLogger=false



    log4j.appender.ThirdLog=org.apache.log4j.RollingFileAppender
    log4j.appender.ThirdLog.File=C://third-report.out
    log4j.appender.ThirdLog.layout=org.apache.log4j.PatternLayout
    log4j.appender.ThirdLog.layout.ConversionPattern=[ %p ] %d{dd MMM yyyy HH:mm:ss,SSS} %c:%L - %m%n%n

    log4j.category.ThirdLogger=DEBUG, ThirdLog
    log4j.additivity.ThirdLogger=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM