Log4j Over Slf4j Maven

Posted on  by 

NOTE ON PERFORMANCE Contrary to other bridging modules, namely jcl-over-slf4j and log4j-over-slf4j, which reimplement JCL and respectively log4j, the jul-to-slf4j module does not reimplement the java.util.logging because packages under the java. namespace cannot be replaced. The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. Java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time. More information can be found on the SLF4J website. Here is a real life use case in which this is used. In this example we want to exclude any commons-logging and log4j JARs, but we do not want to exclude the log4j-over-slf4j JAR. So we want to exclude log4j.jar but keep the log4j-over-slf4j.jar. Log4j Implemented Over SLF4J - Log4j implemented over SLF4J org.slf4j: log4j-over-slf4j: 1.7.30 - Official search of Maven Central Repository Maven Central Repository Search Quick Stats Report A.

1. Overview

In this tutorial, we're going to examine Lombok log annotations like @Slf4j, @Log4j or @Log.

2. Use Log Annotations

Lombok provides several log annotations to work with different logging libraries. In the end, they all generate a logger instance named as log which we can use in our methods.

2.1. Use @CommonsLog

@CommonsLog generates a logger for the Apache Commons Logging library.

Firstly let's add the commons-logging Maven dependency:

Now we can annotate our classes with @CommonsLog:

Log4j2 Over Slf4j Maven

Here, we're accessing the logger with the log variable.

Lombok generates this log variable during the compilation.

2.2. Use @Slf4j

@Slf4j generates a logger using the SLF4J API. In order to create a logger instance, we must also include a library that implements the Slf4j API.

We'll use the Logback library:

The logback-classic dependency pulls in other required dependencies.

Now, we can annotate our class with @Slf4j:

This, in return, generates a Logback-backed logger:

2.3. Use @Log4j

@Log4j creates a logger using the log4j library.

We'll add the log4j Maven dependency:

Similar to previous examples we must annotate our class:

Then Lombok generates the logger accordingly:

2.4. Use @Log4J2

We can also use @Log4j2 to use the newer version of log4j.

Firstly, we must pull in the required dependencies:

When we annotate our class with @Log4j2:

Lombok generates:

2.5. Use @FLogger

The FLogger is the standard logging library used in Google. @FLogger creates an instance of FLogger.

We can add FLogger to our application through Maven:

flogger and flogger-system-backend are the required dependencies.

When a class is annotated with @FLogger:

Lombok generates:

2.6. Use @JBossLog

We can also use @JBossLog for logging purposes.

To use it, we must first add the jboss-logging Maven dependency:

Then we must annotate our class with @JBossLog:

This results in:

2.7. Use @Log

@Log creates a logger using Java util logging. To use it, we don't need an additional dependency.

We must annotate our class with @Log:

Lombok generates a logger:

2.8. Use @XSlf4j

@XSlf4j creates a logger using Slf4j extensions.

We must add the slf4j-ext Maven dependency:

Then we annotate the class with @XSlf4j:

As a result, the logger is generated:

3. Set the Logger Topic

We can change the logger category with the topic attribute. By default, it is the enclosing class name.

4. Global Configuration

Lombok provides several configuration properties for log annotations.

4.1. lombok.log.fieldName

By default, the logger field name is log. But we can change the field name using the lombok.log.fieldName property:

We're changing the name as logger.

Log4j Over Slf4j Maven

4.2. lombok.log.fieldIsStatic

Lombok generates the logger as a static field in the class. If we set lombok.log.fieldIsStatic to false, it will be an instance field instead:

4.3. lombok.log.flagUsage

lombok.log.flagUsage flags the usages of log annotations.

For example, we can forbid the use of any log annotations by setting its value to error:

Note that this property doesn't have a default value.

4.4. Library Specific Flag Usage

We can also flag the usages of the library-specific annotations.

5. Summary

In this tutorial, we looked at various log annotations Lombok provides.

Finally, check out the source code for all examples over on Github.

SLF4J(Simple Logging Façade for java) is an API designed to give generic access to many logging frameworks, log4j being one of them.

It is basically an abstraction layer. It is not a logging implementation. It means that if you are writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J, e.g., log4j or the Java logging API. It is used to prevent applications from being dependent on different logging APIs just as they use libraries that are dependent on them.

However, we elaborate the difference between Log4J and SLF4J that deserves only one line answer. i.e., the question itself is wrong. SLF4J and Log4J are different, or they are not similar components. As the name specified, SLF4J is a simple logging façade for java. It is not a logging component, and even it does not do the actual logging. It is only an abstraction layer to an underlying logging component.

Log4j-over-slf4j Maven Example

In the case of Log4j, it is a logging component, and it does the logging instructed to do. So we can say that SLF4J and Log4J are logically two different things.

Now, all you have to select, which logging framework you need to use in runtime. For that, you will need to include two jar files:

  • SLF4J binding jar file
  • Desired logging framework jar files

For example, to use log4j in your project, you will need to include given below jar files:

  • slf4j-log4j12-1.7.12.jar
  • log4j-1.2.17.jar

Once you have placed both jar files in your application classpath, SLF4J will automatically detect it and start using log4j for processing the log statements based on the configuration you provided in log4j configuration file.

For example, below code you may write in your project class file:

Why is SLF4J better than Log4J?

It is always difficult to prefer one between the SLF4J and Log4j. If you have a choice, I would suggest you; logging abstraction is always preferable than logging framework. If you use a logging abstraction, SLF4J in particular, we can migrate to any logging framework we require at the time of deployment without opting for single dependency.

Following are the reasons, which are good enough to choose SLF4J over Log4j:

  • It is always better to use abstraction.
  • SLF4J is an open-source library or internal library that makes it independent of any particular logging implementation, which means no need to manage multiple logging configurations for multiple libraries.
  • SLF4J provides placeholder based logging, which improves the readability of code by removing checks like isInforEnabled(), isDebugEnabled(), etc.
  • By using the logging method of SLF4J, we defer the cost of constructing logging messages (string), until you need it, which is both CPU and memory efficient.
  • Since SLF4J uses less number of temporary strings means less work for the garbage collector, which means better throughput and performance for your application.

So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.


Coments are closed