SLF4J - What is a dangling or detached marker?

旧街凉风 提交于 2019-12-23 20:33:19

问题


In SLF4J I'm not entirely sure what a detached marker is. The JavaDoc is rather ambiguous.

Some questions:

  • Is it purely memory management of Markers ie to stop/start markers being garbage collected?
  • What happens when you log with a dangled/detached marker?
  • If the marker is detached can you still filter on it or is it off?
  • Why would you detach a marker during runtime? I can understand creating free form dynamic markers and thus save memory (bullet point 1) but to detach later I find bizarre.

回答1:


I had the same question and tried to find some information on it.

In the SLF4J release notes for 1.3.1 it says

In response to a enhancement request made by Michael Newcomb, a marker can now be detached from the internal list of the MarkerFactory that generated it.

It links to a Bugzilla bug #39, which I can’t find anymore.

The thread Marker for object identification? on mailing list slf4j-user is about the topic, but doesn’t contain the request verbatim.

My interpretation is this:

  • A detached Marker is not referenced in an internal list and can be garbage collected.
  • As Markers can have children: If you create a detached marker A with children B and C, and later re-retrieve marker A' with the same name, you are not guaranteed A == A' and A' might not have the same children, if you didn’t add them yourself.
  • I would expect logging and filtering to behave the same for normal and detached Markers, caveat the children thing.
  • The main application seems to be markers with dynamic identifiers. In the mailing list thread above the example is session names, which are always different. Different components in the same session use it as Marker, which you can put into the log pattern and later identify associated log messages.

By the way, in the log4j 2 documentation of Log4jMarkerFactory, the log4j implementation of slf4j, it says about method getDetachedMarker() that

Log4j does not support detached Markers for performance reasons. The returned Marker is attached.

So maybe detaching Markers is only useful for certain logging libraries.



来源:https://stackoverflow.com/questions/22745221/slf4j-what-is-a-dangling-or-detached-marker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!