[logback-user] Changing appenders at runtime
Ceki Gulcu
ceki at qos.ch
Thu May 3 18:57:08 CEST 2007
Hi Richard,
It looks OK to me except that you seem to be omitting the call to attaching the
new appender to the root logger. I suggest you add the following line after
starting classAppender.
logger.addAppender(clsssAppender);
That should do it. I hope this helps,
Richard Wallace wrote:
> 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
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
>
--
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
More information about the Logback-user
mailing list