简体   繁体   中英

how to enable apache POI logging with log4j

I need to turn apache POI logging on for debugging purposes as there is a problem with creating xlsx files. I read the documentation and it seems to be possible, I also looked at here and tried the example but I still don't see any logs from apache POI . Here is my attempt:

log4j.properties

log4j.logger.com.my.package.service.MyClass=DEBUG, dailyReportAppender
log4j.additivity.com.my.package.service.MyClass=false

log4j.appender.dailyReportAppender=org.apache.log4j.RollingFileAppender
log4j.appender.dailyReportAppender.File=C:\\Testlogs/ReportTask.log
log4j.appender.dailyReportAppender.DatePattern=${roll.pattern.daily}'.log'
log4j.appender.dailyReportAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyReportAppender.layout.ConversionPattern=%d{${datestamp}} %-5p %C:%L %m%n

MyClass.java

public class MyClass{
    protected static final Logger logger = Logger.getLogger(MyClass.class.getName());
    {
        System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.CommonsLogger" );
    }

    //things to do in this class
}

I also added the following dependency to my pom.xml file:

<dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
</dependency>

what else do I need to do to see logs from apache POI in my log file?

SOLVED:

I created a new logger and appender for org.apache.poi in my log4j.properties

#apache logger for debug
log4j.logger.org.apache.poi=DEBUG, apacheLogger //or any other log level such as ALL
#log4j.additivity.org.apache.poi=false
log4j.appender.apacheLogger=com.my.package.util.MyLog4jFileAppender //this is a custom appender I created but any other appender such as RollingFileAppender will also do
log4j.appender.apacheLogger.File=C:\\Testlogs\\apacheLogs/apache.log
log4j.appender.apacheLogger.DatePattern=${roll.pattern.daily}'.log'
log4j.appender.apacheLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.apacheLogger.layout.ConversionPattern=%d{${datestamp}} %-5p %C:%L %m%n
log4j.appender.apacheLogger.MaxBackupIndex=10

all other configurations are like before as how they are in my question, and now I am able to see the logs from apache POI

Example:

2018-10-09/14:12:00.777/CEST DEBUG org.apache.poi.util.CommonsLogger:85 Save core properties part
2018-10-09/14:12:00.777/CEST DEBUG org.apache.poi.util.CommonsLogger:85 Save package relationships
//and many more..

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