Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are markers in Java Logging frameworks and what is a reason to use them?

People also ask

What is marker in logger Java?

Markers. One of the primary purpose of a logging framework is to provide the means to generate debugging and diagnostic information only when it is needed, and to allow filtering of that information so that it does not overwhelm the system or the individuals who need to make use of it.

What is the purpose of logging framework?

A logging framework is a utility specifically designed to standardize the process of logging in your application. This can come in the form of a third party tool, such as log4j or its . NET cousin, log4net. But a lot of organizations also roll their own.

Why do we use loggers in Java?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.

What is marker in Logback?

For this example, we will be using logback as logger with SLF4J. Logback was conceived and created as a successor to Log4J. Logback supports markers for the logging calls. These Markers allow association of tags with log statements.


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.