Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok @slf4j not logging

I have a Spring application that uses @slf4j annotation from Lombok, but everything I log with it is not shown in the console.

My application includes the following library slf4j-api-1.7.21 jcl-over-slf4j-1.7.21 lombok-1.16.10

Everything I log is being passed to NOPLogger (No Operation logger) that does exactly what it's supposed to do... nothing!

Do I need to configure a factory somewhere in order to use a logger that will actually log something?

like image 579
pmartin8 Avatar asked Dec 03 '16 02:12

pmartin8


People also ask

What does @SLF4J annotation do?

Annotation Type Slf4jCauses lombok to generate a logger field. Complete documentation is found at the project lombok features page for lombok log annotations. This annotation is valid for classes and enumerations.

How does @SLF4J work?

This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.

What is @SLF4J annotation in spring boot?

@Slf4j – Creates the logger with following statement: Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.

Is log4j and SLF4J same?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both.


2 Answers

I figured, adding slf4j-api is not enough, you also need a proper implementation of the APi.

Adding this will work:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
</dependency>
like image 102
pmartin8 Avatar answered Nov 02 '22 01:11

pmartin8


If you are importing @slf4j with Lombok, then you need 3 dependencies in your pom.xml file.

As @pmartin8 indicated, you need to add slf4j-simple, but all three required dependencies will look like this:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.24</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.26</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.21</version>
</dependency>
like image 1
chris Avatar answered Nov 02 '22 00:11

chris