[slf4j-user] Correct usage of Marker interfaces
Jeffrey Sinclair
jeff at cooljeff.co.uk
Tue Jul 14 22:28:39 CEST 2009
Ceki,
Thanks for your response.
This relates to a thread my colleague started a little while ago [1].
As Zoltan points out we are looking to use SLF4J as an alerting
abstraction. This has been implemented using logback as the back end
without any issues.
Personally I'm a very big fan of logback because unlike most other
logging implementations, logback has not only addressed the façade but
also addresses the more complex use-cases (such as filters, mdc, etc)
which are essential for making something as important as a logging
framework really work well in an enterprise environment.
Having said this, I don't want to loose sight of the importance of the
façade which SLF4J provides. Whilst I know it has been mentioned a
couple of times that all logback cares about is that what is passed is
an implementation of Marker, what I really want someone to say is that
all SLF4J stipulates is that what is passed through the Logger interface
only needs to implement Marker.
This is not clear to me from the design of the APIs. My concern is that
subtle ambiguities like this can result in compatibility issues with
other logging frameworks which might implement Markers, which assume
that what they will get back has been created from their own, statically
bound, MarkerFactory.
The javadocs for the MarkerFactory has the following:
"MarkerFactory is a utility class producing Marker instances as
appropriate for the logging system currently in use."
If you are free to provide your own Marker implementations, then what is
the real purpose of the MarkerFactory?
I want to avoid two scenarios in the future being caused by legitimate
use of Markers by an SLF4J implementation:
1. Getting ClassCastExceptions because a backend implementation has
tried to cast to its own type created by its own MarkerFactory.
2. My marker instance not being preserved to the backend implementation.
For example I don't want the backend implementation to try to pass me
something which returns the same name through Marker.getName() but is
not my original Marker instance since I will loose additional state
associated to my custom Marker.
Jeff
[1] http://www.mail-archive.com/dev@slf4j.org/msg02249.html
> Jeffrey Sinclair wrote:
>
> > slf4j-user,
> >
> > I'm looking to use Markers to supplement the information that is
> being
> > logged. Most of the examples I've seen have fixed markers which don't
> > contain any direct context information, instead they are tags for
> doing
> > a fixed enrichment of the log message at the backend. All of the
> > examples I've seen also create the Markers from the MarkerFactory,
> > leaving it up to the backend implementation (e.g. logback) to supply
> a
> > concrete named marker instance.
> >
> > What I would like to do is enrich the information going to the back
> end
> > on a per log message basis (for example to be dealt with by a logback
> > appender that is aware of my marker, otherwise it is ignored).
> >
> > In order to do this I would need to pass through my own Marker
> > implementation which is not created by the MarkerFactory.
> >
> > For example:
> >
> > {
> > CustomMarker marker = new CustomMarker();
> > marker.setProperty1(...);
> > marker.setProperty2(...);
> > marker.setProperty3(...);
> >
> > logger.debug(marker, "The cat sat on the mat.");
> > }
> >
> > After reading through various mail threads it appears that logback is
> > fine doing the above. All it cares is that the object implements
> > Marker:
> >
> > http://www.mail-archive.com/logback-user@.../msg00642.html
> >
> > However it is not clear from the slf4j APIs if markers sent through
> the
> > logger must be created from the MarkerFactory.
> >
> > Could someone clarify if Markers must be created from the
> MarkerFactory
> > or if it is fine to pass in your own implementation of Marker to be
> > passed to the backend (bypassing the MarkerFactory).
> ... [show rest of quote]
>
> Hello Jeffrey,
>
> As long as your custom marker implements the org.slf4j.Marker
> interface, logback does not care. You do not need to necessarily go
> through MarkerFactory to manufacture custom markers. MarkerFactory
> offers an API to obtain markers, but otherwise there is nothing
> special about MarkerFactory.
>
> I am quite intrigued. Could you kindly provide more details about what
> you are trying to do? What is your use case? What do property1,
> property2 and property3 mentioned above contain? Of which type are
> they, e.g. Integer, String, ...?
>
> HTH,
>
> > Jeff
More information about the slf4j-user
mailing list