133

我第一次听说标记是在阅读时:

http://slf4j.org/faq.html

我检查了Logger对象的可用方法:

并找到接口:

我从中获得的更深入的信息:

但我仍然很困惑......请注意,我问的是为什么,而不是如何使用它们,所以这不是重复的:

更新似乎当您使用标记时,您还需要编写自定义 Java 代码,而不是在XML.property文件中进行配置......

更新 2来自http://logback.qos.ch/manual/appenders.html#OnMarkerEvaluator

Marker notifyAdmin = MarkerFactory.getMarker("NOTIFY_ADMIN");
logger.error(notifyAdmin,
  "This is a serious an error requiring the admin's attention",
   new Exception("Just testing"));
4

1 回答 1

135

这是我对“在 SLF4J/Logback 中使用标记的最佳实践”问题的回答的重新散列版本。

标记可用于为单个日志语句着色或标记。您如何处理这些颜色(即标记)完全取决于您。然而,两种模式似乎对于标记的使用很常见。

  1. 触发:可以指示某些附加程序在存在某个标记的情况下执行操作。例如,SMTPAppender可以配置为NOTIFY_ADMIN无论日志级别如何,只要使用标记标记日志记录事件就发送电子邮件。请参阅logback 文档中的基于标记的触发。您还可以组合日志级别和标记以进行触发。

  2. 过滤:标记对于使某些有价值的日志语句脱颖而出非常有用。例如,您可以使用颜色“DB”对所有与持久性相关的日志(在各种和多个类文件中)进行着色/标记。然后,您可以过滤“DB”:禁用日志记录,但标有 DB 的日志语句除外。有关更多信息,请参阅logback 文档中有关过滤器的章节(搜索 MarkerFilter)。请注意,标记过滤不仅可以通过 logback 执行,还可以通过日志分析工具执行。

Before the advent of Markers, to achieve similar behavior, you had the option 1) using custom levels 2) use modified logger names. SLF4J API currently does not support custom levels. As for option 2, suffixing (or prefixing) logger names is workable if a one or two loggers need to be modified. The approach becomes impractical as soon 3 or more loggers need to be "sub-classed" because the associated configuration files become unmanageable.

Even though a single marker can be already very useful, the next version of SLF4J, i.e. version 2.0, will allow multiple markers per log statement.

于 2013-05-29T18:08:26.593 回答