[logback-user] Spawning threads w/ their own appender

Al Bupp albupp at vermontel.net
Sat Mar 6 21:08:29 CET 2010


Greetings Fellow Logback Users,

I hoping that someone on this list can help me to understand why I'm seeing
odd logging behavior in a servlet-based app I've written.  No doubt I've
mis-implemented some aspect of logback, but I don't know what it might be.  

Here's what I've got in braod strokes.  A servlet app which accepts HTTP
requests.  The servlet contains some global configuration and app data,
including backend DB credentials.  The servlet has a logback appender
defined in a xml file:

<configuration>
  <appender name="CostingServlet"
class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also
http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <File>\\pusehk08\pvcost_devl$\costingservlet.log</File>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%-17d{yyyy-MMM-dd HH:mm} %-6r [%t] %-6p %c{3} -
%m%n</Pattern>
    </layout>
    <rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <maxIndex>10</maxIndex>
     
<FileNamePattern>\\pusehk08\pvcost_devl$\costingservlet.log.%i</FileNamePattern>
    </rollingPolicy>
    <triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>2MB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <root level="debug">
    <appender-ref ref="CostingServlet"/>
  </root>
</configuration>

I have to deploy this servlet to a series of dev, test and prod servers all
using the same identical WAR package, so there are actually 3 different xml
files, a particular one of which gets loaded depending on the state of a
runtime server variable.

I load the specific config file when the servlet inializes using this java
code:

<code>
   protected void configLogger()
   {
      // set logging configuration
      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
      try {
         JoranConfigurator configurator = new JoranConfigurator();
         configurator.setContext(lc);
         lc.reset();
         configurator.doConfigure(logConfigFullPath.toString());
      }
      catch (JoranException je) {
         StringBuffer errMsg = new StringBuffer(outAppName.toString());
         errMsg.append("Error loading logging configuration file: ");
         errMsg.append(je.toString());
         System.out.println(errMsg.toString());
         je.printStackTrace();
      }
   }
</code>

As a result of incoming HTTP requests the servlet starts a thread to handle
the request.  This request handling class needs to have logging to a
separate log file for each request.  Since the logs for the requests should
be named uniquely, I use the following xml configuration:

<configuration>
   <timestamp key="byMilliSecond" datePattern="yyyyMMMdd'T'HHmmssSSSS" />
   <appender name="FILE" class="ch.qos.logback.core.FileAppender">
      <File>\\pusehk08\pvcost_devl$\reqlog-${byMilliSecond}.log</File>
      <layout>
         <Pattern>%-17d{yyyy-MMM-dd HH:mm} %-6r [%t] %-6p %c{3} -
%m%n</Pattern>
      </layout>
   </appender>
   <root level="debug">
      <appender-ref ref="FILE" />
   </root>
</configuration>

Again, there have to be 3 separate request  logging config files, one for
each runtime environment.  In the request handling class I also invoke the
manual configuration:

<code>
      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
      try {
         JoranConfigurator configurator = new JoranConfigurator();
         configurator.setContext(lc);
         lc.reset();
         configurator.doConfigure(reqLogConfigPath);
      }
</code>

The problem is that this approach doesn't work too well because after
configuring the request logger, the servlet log message end up going into
the request log until recall its configLogger() method.

Seems like I'm missing something here, probably something hiding in plain
sight in the manual.

Any insight into this will be greatly appreciated.

Cheers, Al
-- 
View this message in context: http://old.nabble.com/Spawning-threads-w--their-own-appender-tp27772903p27772903.html
Sent from the Logback User mailing list archive at Nabble.com.



More information about the Logback-user mailing list