java - Enhanced code coverage with Commons logging and Log4j 2.0 -


i'm migrating log4j 1.2 log4j 2. use apache commons logging 1.1 (jcl), log4j2 implementation.

now when executing unit tests, statements

 if (log.isinfoenabled()) {    log.info("example");  } 

will show uncovered lines in coverage report if log log level high (e.g. warn in examole), if body wont executed.

in log4j 1 of company wrote custom logger return true log.isxxxenabled() methods if detects run maven surefire, this:

import org.apache.commons.logging.log; import org.apache.commons.logging.impl.log4jlogger;  public class log4jenhancedcoverage implements log {    private static final long serialversionuid = -8715529047111858959l;    private final log logger;    private final boolean mavenrun;    public logenhancedcoverage(string name) {     this.logger = new log4jlogger(_name);     this.mavenrun = testsruncontext.ismavensurefirerun();   }    @override   public boolean istraceenabled() {     return (mavenrun) ? true : logger.istraceenabled();   }    @override   public void trace(object message) {    logger.trace(message);   }    // repeat warn, info, etc } 

the effect each isxxxenabled() block executed, log statement redirected log4j via configuration file. log4j see messages of specified log levels shown, while code blocks all levels executed. old: problem can see log4j logger implementation instanciated directly impl package of jcl. (yes, ship own adapter implementation!).

in new setup, i'm using log4j-jcl artifact , not know how create log4j 2 compatible logger in constructor.

update: log4j-jcl ships own log factory loads log4j 2 specific logger implementation. configuration of commons-logging specific logger not honored @ all.

simply logger using commons logging api rather directly instantiating , you'll fine.

import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; //remove this: import org.apache.commons.logging.impl.log4jlogger;  public class log4jenhancedcoverage implements log {      private static final long serialversionuid = -8715529047111858959l;      private final log logger;      private final boolean mavenrun;      public log4jenhancedcoverage(string name) {         //remove this: this.logger = new log4jlogger(name);         this.logger = logfactory.getlog(name);         ... 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -