What is SLF4J?

Logging is a common issue for development teams. And right now there are a lot of options on what exactly to use for logging in Java. The most famous frameworks are: Log4j, Logback, Commons-logging, java.util.logging. But what happens if we want to change a logging framework by another one, did we have to change the whole application? If the project is high coupled with the logging framework, there’s no choice.

Here comes the role of The Simple Logging Facade for Java or (SLF4J) who serves as a simple facade or abstraction for various logging frameworks, allowing the end user to plug in the desired logging framework at deployment time. In this post we will discover how SLF4J works by analyzing it by JArchitect.

JArchitect Overview

JArchitect is a static analysis tool for Java codebases that provides interactive GUI(s) and HTML reports for finding overly complex or problematic areas of your code, performing analysis for refactoring and comparing changes over time.

The cool and powerful feature of the JArchitect is the support for Code Query Linq (CQLinq). The CQLinq lets the developers to query the Java code using LINQ queries, for example CQlinq can answer to the following requests:

Which methods create objects of a particular class?

from m in Methods where m.CreateA ("MyPackage.MyClass") select m

Which methods assign a particular field?

from m in Methods

where m.AssignField("MyPackage.MyClass.m_Field") select m

Which complex method is not enough commented?


from m in Application.Methods  where m.CyclomaticComplexity >  15 && m.PercentageComment <  10

select new { m, m.CyclomaticComplexity, m.PercentageComment }

JArchitect provides more than 80 metrics that are related to your code organization, code quality and code structure. These metrics could be used in CQLinq to create your coding custom rules, JArchitect could be integrated into your build system to enforce the quality of your codebase.

SLAF4J Analysis

We analyze a solution containing SLF4JSample.jar a sample project using SLF4J, slf4j-api.jar and slf4j-log4j.jar. Here’s the dependency graph after the analysis:


This dependency graph reveals two remarks:

– SLF4JSample is not coupled directly with log4j, it knows only the facade slf4j-api.

– There’s a dependency cycle between slf4j-api and slf-log4j

And to have more infos about dependencies between all jars, the Dependency matrix shows also the dependency weight between them.


For example SLF4JSample use two members from slf4j-api.

To understand better how SLF4J works, let’s discover how all these jars collaborates with each other, and we can begin with SLF4jSample and search which methods it uses, for that we can execute the following CQLinq query:

from m in Methods where m.IsUsedBy ("com.srccodes.examples.SLF4JHello")

select new { m, m.NbBCInstructions }

Here’s the result:


SLF4JHello ask for a logger from slf4j-api by invoking LoggerFactory.getLogger method, and after use it. What’s interesting is that SLF4JHello is not coupled directly with the logger implementation, but pass by the interface Logger.

How SLF4J got the logger?

To discover that let’s see which methods are invocked by LoggerFactory.getLogger


The getLogger method try to get a LoggerFactory from StaticLoggerBinder to ask it for a logger, the StaticLoggerBinder exist in slf4j-log4j.jar, this jar act as a binder between SLF4J and Log4j.

Let’s discover which methods are invoked by LoggerFactory.getILoggerFactory

from m in Methods where m.IsUsedBy ("org.slf4j.LoggerFactory.getILoggerFactory()")

select new { m, m.NbBCInstructions }


So getILoggerFactory search for org.slf4j.impl.StaticLoggerBinder to create the logger factory. What means that if we create a jar containing this method and copy it where slf4j-api exists, this method will be invoked when a logger factory is asked.

And in this case to switch from a logging framework to another we need only to copy the right binder corresponding to the framework used. What explains also the dependency cycle between slf4j-api and slf4j-log4j.

slf4j use a simple technique to act as a logging facade, and permit to have a low coupling between the application and the logging framework, and  we can also change at runtime the logging framework.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!