4

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.
Adam Gent
  • 47,843
  • 23
  • 153
  • 203

1 Answers1

3

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.

hfs
  • 2,433
  • 24
  • 37