[logback-user] Heck of a time changing name of log file/pausing appender

gordon at activecert.com gordon at activecert.com
Sat Sep 11 19:05:09 CEST 2010


Hi Everyone,

I'm new to Logback and SLF4J. Things were going smoothly until I had two requirements - 

Change from a static log file name to a dynamic one
Pause the console appender

I googled quite a bit and found code that it appeared should work, but it unfortunately did not. 

I'll try to keep code/xml inclusion short and to the point. If someone can guide me on the correct practice to follow, it would be great. Logback jars are version 0.9.24 and SLF4J is at 1.6.

Relevant portion of logback.xml is:

<configuration debug="false" scan="false" scanPeriod="60 seconds">

      <!-- snip... -->



      <!-- variable used to hold desired log file name 

           I comment out because I don't seem to be able to override

           if present 

      -->

      <!--<property name="system.logfile.name" value="deploy" />-->



      <!-- snip... -->

      <timestamp key="bySecond" datePattern="yyyyMMdd'_'HH.mm.ss"/>

      <!-- snip... -->



      <!-- this is the appender I want to "pause" --> 

      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

          <!-- snip... -->

      </appender>

      

      <!-- this is the appender that should use a dynamic file name -->

      <appender name="ROLLINGFILE_SYSTEM" class="ch.qos.logback.core.rolling.RollingFileAppender">

          <!-- snip... -->

          <File>${system.logfile.name}-${bySecond}.log</File>

          <!-- snip... -->

      </appender>

      <!-- snip... -->

</configuration>



Approaches tested for dynamic filename: 1) use SLF4J Logger and Jason Configurator, 2) use Logback Logger.



1) In the case below, the logback.xml file is processed, and a file is created with the name "system.logfile.name_IS_UNDEFINED..." as expected. I then execute the code below to change the name (I also delete the file originally created). The result is a file with the desired name is created - but no logging occurs, either to the console or the log file! If I *don't* execute the code to change the file name, logging occurs just fine. I've wasted a ton of time trying to work with this. But even if this code worked, I'd be out of luck, because use case #2 requires pasusing an appender, and for that, I need to use the Logback logger directly. So on two #2 - change the log file name and access appenders.



import org.slf4j.LoggerFactory;

import org.slf4j.Logger;



public class Foo {



private static Logger logger = LoggerFactory.getLogger(ExecWrapper.class);  



    public static void main(String[] args) {

    

            // -- attempt to change log file anme

      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

      JoranConfigurator configurator = new JoranConfigurator();

                

      String logFileName = "foo.bar";

      lc.reset();

      lc.putProperty("system.logfile.name", logFileName);

      configurator.setContext(lc);   

      configurator.doConfigure("logback.xml");



            logger.info("test message.");



            ...etc.



2) In this code, I'd expect to be able to access the appenders - and not even have to use Joran. But the appender is null, and even the iterator for appenders has no elements! I'm sorry, but it's so unbelievably frustrating - I think life would be easier with plain old Java logging than with working with this. If anyone has any thoughts... 



import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

 

public class Foo {



private static Logger logger = (Logger) LoggerFactory.getLogger(ExecWrapper.class);  



    public static void main(String[] args) {



      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

      RollingFileAppender<ILoggingEvent > fileAppender =

         (RollingFileAppender<ILoggingEvent >)logger.getAppender("ROLLINGFILE_SYSTEM");

      if(fileAppender != null) {

          String logFileName = "foo.bar";

          fileAppender.stop();

          fileAppender.setFile(logFileName + ".log");

          fileAppender.setContext(lc);

          fileAppender.start();

      }



Regards,



Gordon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/logback-user/attachments/20100911/7102ca7e/attachment-0001.html>


More information about the Logback-user mailing list