filter markers in logback

2019-08-19 17:49发布

问题:

I have project on scala. I use this lib for logging https://github.com/typesafehub/scala-logging

i create logger

import com.typesafe.scalalogging.Logger
val log                 = Logger(getClass)

and two markers

    import org.slf4j.{Marker, MarkerFactory}
    private val marker: Marker = MarkerFactory.getMarker("DP")
    private val marker2: Marker = MarkerFactory.getMarker("ST")

I use log in my controller

log.debug(marker, "----"
log.debug(marker2, "++++")

This is my logback

<appender name="STDOUTTime" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%coloredLevel %logger{30} - %marker - %d{yyyy/MM/dd/HH:mm:ss.SSS/Z} - %message%n%xException{3}</pattern>
    </encoder>

    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>DP</Marker>
        <OnMatch>DENY</OnMatch>
        <OnMismatch>DENY</OnMismatch>
    </turboFilter>

    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>ST</Marker>
        <onMatch>DENY</onMatch>
        <onMismatch>DENY</onMismatch>
    </turboFilter>
</appender>

<logger name="ds.forwarding" level="DEBUG">
    <appender-ref ref="STDOUTTime"/>
</logger>

<root level="ERROR">

</root>

now when i run my controller i have output in console:

[debug] d.f.c.a.s.InputStatisticController - DP - 2017/09/25/11:55:58.603/+0300 - ----
[debug] d.f.c.a.s.InputStatisticController - ST - 2017/09/25/11:55:58.603/+0300 - ++++

Now I have a questions:

  • Why marker and marker2 is visible, why DENY does not work?
  • How can I exclude two markers?
  • How can I exclude only one marker?

回答1:

Here is a logback.xml which denies both DP and ST markers.

<configuration>
    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>DP</Marker>
        <OnMatch>DENY</OnMatch>
    </turboFilter>

    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>ST</Marker>
        <onMatch>DENY</onMatch>
    </turboFilter>

    <appender name="STDOUTTime" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%coloredLevel %logger{30} - %marker - %d{yyyy/MM/dd/HH:mm:ss.SSS/Z} - %message%n%xException{3}</pattern>
        </encoder>

    </appender>

    <root level="DEBUG">
        <appender-ref ref="STDOUTTime"/>
    </root>  
</configuration>

The mistakes about your file:

  1. Your file doesn't start with <configuration> and end with </configuration>

  2. Filters are not in the correct namespace. They should be under <configuration> (same level with appenders).

  3. No need to use <onMisMatch> flags in your case. It may cause to mix the things up.

  4. Since your logger is named as ds.forwarding, in the class you have to be sure that you are calling that logger. In your case, you call the logger with getClass method. In my logback.xml file, I added the appender to my root logger. Therefore, it is sufficient to call it via Logger(getClass) method.

  5. Always be careful about levels. I set the level to DEBUG.

Once you set the configuration properly, simply change the <onMatch> property to ALLOW if you want the logger to print it, or DENY if you don't. Simply setting both to ALLOW will result in printing all the markers, on the other hand, if you set both of them to DENY, that markers won't be printed.