Using @Log annotation example

In this post under Lombok, I will show with example what is the purpose of “@Log” annotation and how to use it.

In our software development, logging data (whether it is just an info, error, etc) to log files is important.

These log data help developers to figure out what is happening in the application, what data is passed at runtime, what is a cause of an issue etc.

Since logging data is very important every class which handles business logic will have logger field defined as shown below

private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

Here for our example I am using Logging framework from Java but we can use other logging frameworks.

Lombok has similar annotations for each logging framework for example for “log4j” it is “@Log4j”, for “log4j2” it is “@Log4j2”, for “Slf4j” it is “@Slf4j” etc.

By default the name of the logger variable will be “log”.

Once the annotation is applied at the class level, we can log messages exactly the way we used it till now for example;

Below is the complete main code for your reference

Main class

1  package package20;
3  import;
5  @Log
6  public class Example20 {
7      public static void main(String[] args) {
8          Person person1 = new Person("Ellis", "M", "Robertson", 1000.50f);
10     }
11 }

In the above code, at line 5, we annotate the class with “@Log” annotation.

At line 9, we log a message in the same way we used to do till now.

In this way we can use “@Log” annotation.

Leave a Reply