Sankalpa Wijewickrama
Sankalpa Wijewickrama

Reputation: 1255

Using @Slf4j annotation for logging

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?

Upvotes: 23

Views: 57945

Answers (2)

Ganeson Ravichandran
Ganeson Ravichandran

Reputation: 114

@Slf4j

The above annotation from lombok creates a slf4j based Logger, but you would be requiring a log4j based logger. So don't feel you could use slf4j based logger.

If you plan to user slf4j based logger, lombok modifies the code and creates code during the compile phase not during runtime like cglib or using reflections. This would mean there won't be any dip in the performance.

Upvotes: 9

Turing85
Turing85

Reputation: 20185

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.

Upvotes: 23

Related Questions