[logback-user] logback-access in an embedded jetty server

Ceki Gülcü ceki at qos.ch
Wed Jan 25 09:09:36 CET 2017


LoggerContext is the context for logback-classic whereas RequestLogImpl
*is* the context for logback-access+Jetty.  You need to set requestLog 
(of type RequestLogImpl) as the context for components such as 
RollingFileAppender and TimeBasedRollingPolicy. By the way, all logback 
components must be started. Otherwise, they will not work.

As such, here a revised version which should work better. Beware, I have 
not tested this code and it may still contain some wrinkles. Without 
further ado here is the code:

   RequestLogImpl requestLog = new RequestLogImpl(); // implements Context

         ch.qos.logback.access.PatternLayoutEncoder encoder = new 
ch.qos.logback.access.PatternLayoutEncoder();
         encoder.setContext(requestLog);
         encoder.setPattern("combined");
         encoder.start(); // // components must be started

         RollingFileAppender<IAccessEvent> appender = new 
RollingFileAppender<IAccessEvent>();
         appender.setContext(requestLog); // requestLog is the context
         appender.setName("REQUESTLOG");
         appender.setFile("/bps/log/request/myrequest.log");
         appender.setEncoder(encoder);


         TimeBasedRollingPolicy<IAccessEvent> timePolicy = new 
TimeBasedRollingPolicy<IAccessEvent>();

 
timePolicy.setFileNamePattern("/bps/log/archive/request/my_request_%d{yyyy-MM-dd}_%d{HH_mm_ss,aux}.log.zip");
         timePolicy.setContext(requestLog); // requestLog is the context
         timePolicy.setMaxHistory(30);
         timePolicy.setTimeBasedFileNamingAndTriggeringPolicy(new 
StartupTimeBasedTriggeringPolicy<IAccessEvent>());
         timePolicy.setParent(appender);
         timePolicy.start(); // components must be started

         appender.setRollingPolicy(timePolicy);
         appender.setTriggeringPolicy(timePolicy);

         appender.start(); // // components must be started
         requestLog.addAppender(appender);

Let us know how it goes.

--
Ceki

On 1/25/2017 8:20, Jan-Olav Eide wrote:

[snip]


> This does not work (it archives etc, but never any contents in the file) :
>
>         LoggerContext loggerContext =
> LoggerContext.class.cast(LoggerFactory.getILoggerFactory());


>         RollingFileAppender<IAccessEvent> appender = new
> RollingFileAppender<>();
>         appender.setName("REQUESTLOG");
>         appender.setFile("/bps/log/request/myrequest.log");
>         TimeBasedRollingPolicy<IAccessEvent> timePolicy = new
> TimeBasedRollingPolicy<>();
>
> timePolicy.setFileNamePattern("/bps/log/archive/request/my_request_%d{yyyy-MM-dd}_%d{HH_mm_ss,aux}.log.zip");
>         timePolicy.setContext(loggerContext);
>         timePolicy.setMaxHistory(30);
>         timePolicy.setTimeBasedFileNamingAndTriggeringPolicy(new
> StartupTimeBasedTriggeringPolicy<IAccessEvent>());
>         timePolicy.setParent(appender);
>         appender.setRollingPolicy(timePolicy);
>         appender.setTriggeringPolicy(timePolicy);
>         PatternLayoutEncoder encoder = new PatternLayoutEncoder();
>         encoder.setPattern("combined");
>         appender.setEncoder(encoder);
>         RequestLogImpl requestLog = new RequestLogImpl();
>         requestLog.addAppender(appender);
>
> On 24 January 2017 at 22:04, Ceki Gülcü <ceki at qos.ch
> <mailto:ceki at qos.ch>> wrote:
>
>
>     Hi Jan-Olav,
>
>     It's not possible to tell without seeing the code in question. Would
>     you want to post it here?
>
>     --
>     Ceki
>
>     On 1/24/2017 19:43, Jan-Olav Eide wrote:
>
>     > Well, things worked much better with a logback-access.xml file rather
>     > than a programmatic construction of the same configuration. Strange,
>     > since the latter code works fine elsewhere.
>
>


More information about the logback-user mailing list