
Hello, I'm trying to change the appender used at runtime during testing. I'm using the excellent Unitils project to do my unit testing and am unit testing some Hibernate and Spring stuff. The amount that gets logged is too much to do to stdout so I want it to go to a file. My problem is that if I just do it to a single file all the logs run together and you have a hard time telling where one test begins and another ends. So I want to change the file that is being logged to for each test. I've got the following bit of code that runs before each test class. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory (); Logger logger = lc.getLogger (LoggerContext.ROOT_NAME); m_originalAppender = (FileAppender) logger.detachAppender ("FILE"); String logFile = System.getProperty ("test.log.dir") + File.separator + testClass.getName () + ".log"; FileAppender classAppender = new FileAppender (); classAppender.setContext (lc); classAppender.setName ("FILE"); classAppender.setFile (logFile); classAppender.setAppend (false); classAppender.setLayout (m_originalAppender.getLayout ()); classAppender.setEncoding (m_originalAppender.getEncoding ()); classAppender.start (); and here is the initial logback.xml configuration. <configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <File>test.log</File> <Append>true</Append> <Encoding>UTF-8</Encoding> <BufferedIO>false</BufferedIO> <ImmediateFlush>true</ImmediateFlush> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %-4relative [%thread] %-5level %logger{36} - %msg%n </pattern> </layout> </appender> <root> <level value="debug" /> <appender-ref ref="FILE" /> </root> </configuration> Logging starts going to the test.log file successfully and then the testClass log file gets created, but nothing is logged to it. I'm sure I'm missing something about correctly attaching it or having it listen for logging events, but I can't figure out where. Any hints? Thanks, Rich