 <p>You can easily invoke log4j's API programmatically, e.g.</p> <pre><code>FileAppender appender = new FileAppender(); // configure the appender here, with file location, etc appender.activateOptions(); Logger logger = getRootLogger(); logger.addAppender(appender); </code></pre> <p>The <code>logger</code> can be the root logger as in this example, or any logger down the tree. Your unit test can add its custom appender during steup, and remove the appender (using <code>removeAppender()</code>) during teardown.</p>