125
votes

The first time I heard about markers was while reading:

http://slf4j.org/faq.html

I checked available methods for the Logger object:

and found interfaces:

The more in-depth info I got from:

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

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

UPDATE 2 From 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"));
1
To use markers you need to use the marker API provided by SLF4J. Is that what you mean by "custom Java code"?Ceki
@Ceki Seems that using just Logger.warn(Marker, msg) didn't give you any benefit until you wrote custom class for filters and register them... Plain syntax of logback.xml have no special abilities to work with Marker (just print them by %marker)gavenkoa
What do find "special" in what is presented at logback.qos.ch/manual/appenders.html#OnMarkerEvaluator ?Ceki
@Ceki Thanks for point! Is it only one place where markers can be used?gavenkoa
@Ceki Now that we are using Elasticsearch it is the way to mark special events. We keep predefined markers in a dedicated class for reuse. It wasn't obvious when you just use grep over text logs.gavenkoa

1 Answers

130
votes

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 for marker usage.

  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.

  2. Filtering: Markers are very useful for making certain valuable log statements stand out. For example, you can 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). Note that filtering on markers can be performed not just by logback but log analysis tools as well.

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

Even though a single marker can be already very useful, the next version of SLF4J, i.e. version 2.0, will allow multiple markers per log statement.