简体   繁体   English

Java Logging 框架中的标记是什么,使用它们的原因是什么?

[英]What are markers in Java Logging frameworks and what is a reason to use them?

The first time I heard about markers was while reading:我第一次听说标记是在阅读时:

http://slf4j.org/faq.html http://slf4j.org/faq.html

I checked available methods for the Logger object:我检查了Logger对象的可用方法:

and found interfaces:并找到接口:

The more in-depth info I got from:我得到的更深入的信息:

but I am still confused... Note that I asked why , not how to use them, so this is not a duplicate of:但我仍然很困惑......请注意,我问的是为什么,而不是如何使用它们,所以这不是以下内容的重复:

UPDATE Seems that when you use markers you are also required to write custom Java code instead doing configuration in XML or .property files...更新似乎当您使用标记时,您还需要编写自定义 Java 代码,而不是在XML.property文件中进行配置...

UPDATE 2 From http://logback.qos.ch/manual/appenders.html#OnMarkerEvaluator更新 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"));

This is a rehashed version my answer to the question " Best practices for using Markers in SLF4J/Logback ".这是对问题“在 SLF4J/Logback 中使用标记的最佳实践”的回答的重新整理版本

Markers can be used to color or mark a single log statement.标记可用于为单个日志语句着色或标记。 What you do with these colors, ie markers, is entirely up to you.您如何使用这些颜色(即标记)完全取决于您。 However, two patterns seem to be common for marker usage.但是,对于标记使用而言,两种模式似乎很常见。

  1. Triggering : Some appender could be instructed to take an action in the presence of a certain marker.触发:可以指示某些 appender 在存在某个标记的情况下采取行动。 For example, SMTPAppender can be configured to send an email whenever a logging event is marked with the NOTIFY_ADMIN marker regardless of the log level.例如,可以将SMTPAppender配置为在使用NOTIFY_ADMIN标记标记日志事件时发送电子邮件,而不管日志级别如何。 See marker-based triggering in the logback documentation.请参阅 logback 文档中的 基于标记的触发 You may also combine log levels and markers for triggering.您还可以组合日志级别和标记以进行触发。

  2. Filtering : Markers are very useful for making certain valuable log statements stand out.过滤:标记对于突出某些有价值的日志语句非常有用。 For example, you can color/mark all your persistence related logs (in various and multiple class files) with the color "DB".例如,您可以使用颜色“DB”为所有与持久性相关的日志(在各种和多个类文件中)着色/标记。 You could then filter for "DB": disable logging except for log statements marked with DB.然后,您可以过滤“DB”:除了标有 DB 的日志语句外,禁用日志记录。 See the chapter on filters in the logback documentation for more information (search for MarkerFilter).有关更多信息,请参阅 logback 文档中有关过滤器章节(搜索 MarkerFilter)。 Note that filtering on markers can be performed not just by logback but log analysis tools as well.请注意,标记过滤不仅可以通过 logback 执行,还可以通过日志分析工具执行。

Before the advent of Markers, to achieve similar behavior, you had the option 1) using custom levels 2) use modified logger names.在标记出现之前,要实现类似的行为,您可以选择 1) 使用自定义级别 2) 使用修改后的记录器名称。 SLF4J API currently does not support custom levels. SLF4J API 目前不支持自定义级别。 As for option 2, suffixing (or prefixing) logger names is workable if a one or two loggers need to be modified.至于选项 2,如果需要修改一个或两个记录器,则后缀(或前缀)记录器名称是可行的。 The approach becomes impractical as soon 3 or more loggers need to be "sub-classed" because the associated configuration files become unmanageable.一旦需要对 3 个或更多记录器进行“子类化”,该方法就会变得不切实际,因为相关的配置文件变得无法管理。

Even though a single marker can be already very useful, the next version of SLF4J, ie version 2.0, will allow multiple markers per log statement.尽管单个标记已经非常有用,但 SLF4J 的下一个版本,即 2.0 版,将允许每个日志语句有多个标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM