Java Logging框架中的标记是什么以及使用它们的原因是什么? [英] What are markers in Java Logging frameworks and what is a reason to use them?

查看:214
本文介绍了Java Logging框架中的标记是什么以及使用它们的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次听到有关标记的信息:

First time I hear about markers when read:

http://slf4j.org/faq.html

我查看 Logger 对象的可用方法:

I check available methods for Logger object:

  • http://www.slf4j.org/api/org/slf4j/Logger.html
  • http://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Logger.html

并找到接口:

  • http://www.slf4j.org/api/org/slf4j/Marker.html
  • http://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Marker.html

我得到的更多深入信息:

More in-depth info I get from:

  • http://logback.qos.ch/manual/filters.html

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

but still confused... Note that I ask why, not how to use them, so this is not a duplicate of:

  • Best practices for using Markers in SLF4J/Logback

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

UPDATE Seems that when you use markers you also required to write custom Java code instead doing configuration in XML or .property files...

更新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"));


推荐答案

这是一个重新散列的版本我的回答问题使用标记的最佳做法SLF4J / Logback

This is a rehashed version my answer to the question "Best practices for using Markers in SLF4J/Logback".

标记可用于颜色或标记单个日志语句。你用这些颜色做什么,即标记,完全取决于你。但是,对于标记的使用,两种模式似乎是常见的(第一种比第二种更常见)。

Markers can be used to color or mark a single log statement. What you do with these colors, i.e. markers, is entirely up to you. However, two patterns seem to be common (the first more common than the second) for marker usage.


  1. 触发:可以指示一些appender在某个标记存在的情况下采取行动。例如, SMTPAppender 可以配置为每当使用 NOTIFY_ADMIN 标记记录日志事件时发送电子邮件,无论日志级别。请参阅logback文档中的基于标记的触发。您还可以将日志级别和标记组合起来进行触发。

  1. Triggering: Some appender could be instructed to take an action in the presence of a certain marker. 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. See marker-based triggering in the logback documentation. You may also combine log levels and markers for triggering.

过滤:例如,您可以对所有与持久性相关的日志进行着色/标记(在各种和多个类文件中),颜色为DB。然后,您可以过滤DB:禁用日志记录,但标记为DB的日志语句除外。有关详细信息,请参阅logback文档中的过滤器章节(搜索MarkerFilter)。

Filtering: You could for example color/mark all your persistence related logs (in various and multiple class files) with the color "DB". You could then filter for "DB": disable logging except for log statements marked with DB. See the chapter on filters in the logback documentation for more information (search for MarkerFilter).

在Markers问世之前,为了达到类似的行为,您可以选择1)使用自定义级别2)使用修改记录器名称。 SLF4J API目前不支持自定义级别。对于选项2,如果需要修改一个或两个记录器,则后缀(或前缀)记录器名称是可行的。这种方法变得不实用,因为很快就会有3个或更多的记录器被分类,因为相关的配置文件变得无法管理。

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 unpractical as soon 3 or more loggers need to be "sub-classed" because the associated configuration files become unmanageable.

这篇关于Java Logging框架中的标记是什么以及使用它们的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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