在logback中过滤标记 [英] filter markers in logback

查看:1317
本文介绍了在logback中过滤标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在scala上有项目. 我使用这个库进行日志记录 https://github.com/typesafehub/scala-logging

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

我创建记录器

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

和两个标记

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

我在控制器中使用登录

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

这是我的登录信息

<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 - ++++

现在我有一个问题:

  • 为什么marker和marker2是可见的,为什么DENY不起作用?
  • 如何排除两个标记?
  • 如何仅排除一个标记?

推荐答案

这是一个logback.xml,它同时拒绝DPST标记.

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>

关于文件的错误:

  1. 您的文件不是以<configuration>开头并以</configuration>

过滤器不在正确的名称空间中.它们应位于<configuration>(与附加程序处于同一级别)下.

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

在您的情况下无需使用<onMisMatch>标志.这可能会导致混淆.

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

由于记录器的名称为ds.forwarding,因此在该类中,必须确保正在调用该记录器.在您的情况下,您可以使用getClass方法调用记录器.在我的logback.xml文件中,将appender添加到了root记录器中.因此,通过Logger(getClass)方法调用它就足够了.

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.

始终注意级别.我将级别设置为DEBUG.

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

正确设置配置后,如果希望记录仪打印该属性,只需将<onMatch>属性更改为ALLOW,否则就可以将其更改为DENY.简单地将它们都设置为ALLOW会导致打印所有标记,另一方面,如果将它们都设置为DENY,则不会打印标记.

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.

这篇关于在logback中过滤标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆