开发者

How to suppress logging of some requests in logback-access?

开发者 https://www.devze.com 2023-04-05 02:02 出处:网络
I\'m using logback-a开发者_如何学运维ccess with Jetty. It\'s working fine, except that http requests for images (*.jpg, *.gif) are getting logged. Most app servers suppress these log records because t

I'm using logback-a开发者_如何学运维ccess with Jetty. It's working fine, except that http requests for images (*.jpg, *.gif) are getting logged. Most app servers suppress these log records because they bloat the logs.

When I was using the old Jetty NCSARequestLog object, there was a setIgnorePaths() method that worked, but the logback RequestLogImpl doesn't have a similar method.

I'm pretty sure there's a way to do it in logback-access.xml, perhaps using some kind of filter. Does anyone have the appropriate syntax?


logback-access has ch.qos.logback.access.net.URLEvaluator that can be used instead of inline scripts with Janino :

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator class="ch.qos.logback.access.net.URLEvaluator">
            <URL>.jpg</URL>
            <URL>.gif</URL>
            <URL>https://example.com/static/</URL>
            <URL>/ops/</URL>
            <URL>/management/</URL>
            <URL>/monitoring</URL>
        </evaluator>
        <OnMismatch>NEUTRAL</OnMismatch>
        <OnMatch>DENY</OnMatch>
    </filter>
   ...

Inside the URLEvaluator makes contains() so the matching can happens on any part of url.

IMHO it's better to avoid to use the Janino library


Here is a simple EvaluatorFilter configuration, copy inside your appender:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator>
            <expression>
            return formattedMessage.contains(".jpg") ||
                formattedMessage.contains(".gif"); 
            </expression>
        </evaluator>
        <onMatch>DENY</onMatch>
    </filter>
    ...

Maybe you should customize the expression, since .gif and .jpg could occur in other log messages too.

EvaluatorFilter needs the following dependency. Put it to your pom.xml or add to your classpath if you don't use Maven:

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>2.5.16</version>
</dependency>

Further EvaluatorFilter documentation

0

精彩评论

暂无评论...
验证码 换一张
取 消