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

Ceki Gülcü ceki at qos.ch
Mon Apr 12 18:56:47 CEST 2010


Hello Albert,

Have you looked at SiftingAppender [1]? All you need is to insert the request so 
that your custom discriminator makes use of it. You could even use the 
MDCBasedDiscriminator which ships with logback. In a servlet filter just insert 
the request id into the MDC and let SiftingAppender do its magic.

HTH,

[1] http://logback.qos.ch/manual/appenders.html#SiftingAppender

On 12/04/2010 3:47 PM, Albert Bupp wrote:
>
> 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
>


More information about the Logback-user mailing list