[slf4j-dev] Usage of Markers
Ceki Gulcu
ceki at qos.ch
Thu Jun 4 17:01:58 CEST 2009
If we are talking about a small number of markers, they could be
manufactured in a utility class and assigned to constants. Other
classes would simply reference those constant Marker objects. That's a
trivial problem with a trivial solution.
However, if the number of markers are too numerous to be enumerated as
constants, then we need to work a little harder.
Assume the alert data consists of two fields: alertKey and level. You
could create and use a marker as follows:
// merge fields alertKey and level
Marker m = MarkerFactory.getMarker(alertKey+","+level);
logger.info(m, "the aircondition is down);
The above is not very elegant but for a two field structure would
probably work fine. I offhandedly am assuming the the cross product of
alertKey and level is smaller than 1'000.
Also note that markers can be nested. Thus, you could also write
// merge fields alertKey and level
Marker m = MarkerFactory.getMarker(alertKey+","+level);
Marker alertkeyMarker = MarkerFactory.getMarker(alertkey);
if(!m.containts(alertkeyMarker) {
m.add(alertkeyMarker);
}
Marker levelMarker = MarkerFactory.getMarker(level);
if(!m.containts(levelMarker) {
m.add(levelMarker);
}
logger.info(m, "the aircondition is down);
This can be refactored as
static Marker myMarkerMaker(String alertKey, int level) {
Marker m = MarkerFactory.getMarker(alertKey+","+level);
Marker alertkeyMarker = MarkerFactory.getMarker(alertkey);
if(!m.containts(alertkeyMarker) {
m.add(alertkeyMarker);
}
Marker levelMarker = MarkerFactory.getMarker(level);
if(!m.containts(levelMarker) {
m.add(levelMarker);
}
return m;
}
and reduced into two lines per invocation:
Marker m = myMarkerMaker("alert.aircondition", 1);
logger.info(m, "the aircondition is down);
HTH,
Szel, Zoltan wrote:
>> The issue is in what the benefit is in having Markers for each of the
> various kinds of alerts
>
> There would be only one Marker implementation which would contain any
> information required(alertkey, level1/level2 classification etc) to send
> an alert. This would allow the flexibility to send different alerts with
> the same API(they can provide defaults, but it would not be enough given
> that different infrastructure components will send alerts with different
> properties).
>
--
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
More information about the slf4j-dev
mailing list