[logback-user] Setting log filename during runtime

Ceki Gulcu listid at qos.ch
Fri Nov 14 09:59:58 CET 2008


Hello Jenny,

You can dynamically add properties for variable substitution to a logger 
context. Here is how:

  LoggerContext loggerContext = (LoggerContext)
          LoggerFactory.getILoggerFactory();
  JoranConfigurator jc = new JoranConfigurator();
  jc.setContext(loggerContext);
  loggerContext.reset();  // throw away default configuration

  // the next line is the whole point of the exercise
  loggerContext.putProperty("logFileName", valueObtainedFromLDAP);
  try {
    jc.doConfigure("path/to/a/configfile.xml");
  } catch (JoranException je) {
    je.printStackTrace();
  }
  StatusPrinter.printIfErrorsOccured(loggerContext);


And in configfile.xml mentioned above, you would have

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>%{logFileName}</file>
   ...
</appender>

HTH,

Jenny wrote:
> Hi Ceki,
> 
> Variable substition wouldn't accomplish what I want to achieve... or would it? :)
> 
> My goal is to set the log filename in a dynamic way - retrieve it from an LDAP configuration. I don't want it hardcoded in the logback.xml.
> 
> My understanding is that variable substition only works if the variable comes from an external properties file or set in system property.
> 
> thanks,
> Jenny
> 
> 
> --- On Thu, 11/13/08, Ceki Gulcu <listid at qos.ch> wrote:
> 
>> From: Ceki Gulcu <listid at qos.ch>
>> Subject: Re: [logback-user] Setting log filename during runtime
>> To: "logback users list" <logback-user at qos.ch>
>> Date: Thursday, November 13, 2008, 4:11 PM
>> Hello Jenny,
>>
>> Why are you setting the passing value of
>> "logFileName" MDC? Just set it as a 
>> variable, see http://logback.qos.ch/manual/joran.html and
>> search for "Variable 
>> substitution".
>>
>> HTH,
>>
>> Jenny wrote:
>>> Hi, I'm not sure if this is the preferred way or
>> best practice... but what I did was extend the
>> RollingFileAppender and override the getFile() method.
>>>  
>>> and this getFile() method retrieves / generates the
>> log file name.
>>>  
>>> please let me know if you have better solutions.
>>>  
>>> thanks,
>>> Jenny
>>>  
>>> =============================================
>>> From: Jenny jenny_uy81 at yahoo.com
>>> Subject: Re: [logback-user] Setting log filename
>> during runtime
>>> To: "logback users list" logback-user at qos.ch
>>>  
>>> Hello,
>>>
>>> How can i set the log filename set during the init()
>> of my StartupServlet.
>>> I tried using MDC but it's not working for me.
>>>
>>> What happens is the file is not created. Instead a
>> file with name "%exp{logFileName}" \
>>> is created on my web server's bin directory.
>>>
>>> In my StartupServlet, I have this init method that
>> calls MDC.put().
>>> public void init() throws ServletException {
>>> MDC.put("logFileName",
>> "c:\\testlogs\\test.log");
>>> Logger log =
>> LoggerFactory.getLogger(StartupServlet.class); 
>>> log.info("This should be logged");
>>> }
>>>
>>> In my logback.xml, I have a MDC.get().
>>>
>>> <appender name="FILE"
>> class="ch.qos.logback.core.rolling.RollingFileAppender">
>>> <append>true</append>
>>> <expression
>> name="logFileName">MDC.get("logFileName")</expression>
>>> <file>%exp{logFileName}</file>
>>> ...

-- 
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