Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Slf4j annotation for logging

Tags:

java

slf4j

lombok

First of all I will give a brief explanation about the scenario. In my application I am using SLF4J as the logging facade. For logging, I am using Log4j2 and I have my customized log4j2.xml as well. When I log in my classes, I create a logger as mentioned below:

private static final Logger LOG = LogManager.getLogger(TestController.class);

Later, I found out that there is a @Slf4j annotation and then I can log without creating a logger instance manually.

log.info("Info log in getHello() in TestController");

I did a minor research about the topic where is there any drawbacks of using @Slf4j annotation, instead of creating a logger instance within the class. I did not come across any reason why shouldn't use @Slf4j annotation.

However, before proceeding I wanted to ask from this community to confirm is there any drawback of using @Slf4j?

like image 363
Sankalpa Wijewickrama Avatar asked Aug 28 '21 13:08

Sankalpa Wijewickrama


People also ask

What does the annotation @slf4j do?

Annotation Type Slf4j Causes 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.

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 Lombok slf4j vulnerable?

Lombok is a build-time only dependency; there is no need for lombok. jar to be available when your application is run, it just needs to be there when you compile your code. Therefore, lombok is highly unlikely to be a source of security vulnerabilities.

What logging does Lombok use?

Lombok's @Slf4j generates a logger instance with default name log using the SLF4J API.


Video Answer


1 Answers

From the documentation of @Log and friends:

...

@Slf4j

Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

...

We see that this is equivalent to

private static final Logger LOG = LogManager.getLogger(TestController.class);

aside from the name of the field.

like image 153
Turing85 Avatar answered Oct 22 '22 10:10

Turing85