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

Jan-Olav Eide janolaveide at gmail.com
Wed Jan 25 09:50:37 CET 2017


That worked out of the box, thanks a lot four your help. I had missed the
fact that the RequestLogImpl was in fact the context.

On 25 January 2017 at 09:09, Ceki Gülcü <ceki at qos.ch> wrote:

>
> 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_r
> equest_%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_r
>> equest_%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.
>>
>>
>> _______________________________________________
> logback-user mailing list
> logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20170125/f8c59109/attachment.html>


More information about the logback-user mailing list