在logback中过滤标记 [英] filter markers in 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
,它同时拒绝DP
和ST
标记.
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>
关于文件的错误:
-
您的文件不是以
<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屋!