[logback-user] Creating a New File Appender for Each HTTP Request

Albert Bupp albupp at vermontel.net
Mon Apr 12 15:47:01 CEST 2010


Greetings,

I have a servlet app which responds to HTTP requests.  Each request 
initiates (sometimes lengthy) processing that I would like to log to 
its own uniquely named log file.  This request log file should be 
separate from the servlet's log file so that request-based log 
statements aren't interleaved w/ each other making them hard to 
follow.  What approach should I use for this?

I am relatively new to Logback.  I have used programmatically 
instantiated appenders in log4j to do this in the past.  For Logback, 
I have created separate configuration files for the servlet and 
request log, and tried invoking a reset of the context and 
doConfigure() on the Joran Configurator once when the servlet 
initiates, and then again processing each request (based on a 
to-the-millisecond date pattern).  Doing this causes the servlet's 
log messages to go into the request log.

Clearly I'm doing something wrong.   ere are the configuration files 
that I'm using, the first for the servlet, the second for the request 
logging.  Perhaps these need to be combined into one file?  If so, 
how can I get a separate log file for each request as its processed?

<!-- servlet logging config file -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
   <appender name="CostingServlet" 
class="ch.qos.logback.core.rolling.RollingFileAppender">
     <!--See also 
http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
     <File>\\host\app\logs\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>\\host\app\logs\costingservlet.log.%i</FileNamePattern>
     </rollingPolicy>
     <triggeringPolicy 
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
       <MaxFileSize>2MB</MaxFileSize>
     </triggeringPolicy>
   </appender>
   <logger name="com.bearlogic.servlets" level="DEBUG"/>
   <root level="debug">
     <appender-ref ref="CostingServlet"/>
   </root>
</configuration>

<!-- request logging config file -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <timestamp key="byMilliSecond" datePattern="yyyyMMMdd'T'HHmmssSSSS" />
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
       <File>\\host\app\logs\reqlog-${byMilliSecond}.log</File>
       <layout>
          <Pattern>%-17d{yyyy-MMM-dd HH:mm} %-6r [%t] %-6p %c{3} - 
%m%n</Pattern>
       </layout>
    </appender>
    <logger name="com.bearlogic.rc" level="DEBUG" />
    <root level="debug">
       <appender-ref ref="FILE" />
    </root>
</configuration>

I've read the manual, browsed through FAQ and searched the user 
archives for an answer to this w/o understanding what I should be 
doing.  I can't be the first to want to do something like 
this.  Perhaps I need to use a filter of some sort?  Any hints or 
pointers on this would be greatly appreciated.

Thanks in advance,

-Albert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/logback-user/attachments/20100412/4594a47d/attachment-0001.html>


More information about the Logback-user mailing list