Normally, when we use a Logging framework like Log4J, Logback or Simple Logging Facade for Java (SLF4J) for these Logging frameworks in a class, we need declare for example as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.lombok; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Example { public static final Logger LOGGER = LoggerFactory.getLogger(Example.class); public void print(String message) { LOGGER.info(message); } } |
But with Project Lombok, we don’t need to do that.
When using Project Lombok, we only need declare an annotation for using a Logging framework, then we can use this Logging framework as usual. For example, I can declare @Slf4j annotation for using Simple Logging Facade for Java in my example as below:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.lombok; import lombok.extern.slf4j.Slf4j; @Slf4j public class Example { public void print(String message) { log.info(message); } } |
In this case, if you check the /target/classes/com/huongdanjava/lombok/Example.class, you will see the content of Example class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.huongdanjava.lombok; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Example { private static final Logger log = LoggerFactory.getLogger(Example.class); public Example() { } public void print(String message) { log.info(message); } } |
Obviously, you can see Project Lombok will generate the code to declare Logger object in class Example automatically.
Below is some others annotation for other Logging frameworks:
- @CommonsLog: org.apache.commons.logging.Log
- @JBossLog: org.jboss.logging.Logger
- @Log: java.util.logging.Logger
- @Log4j: org.apache.log4j.Logger
- @Log4j2: org.apache.logging.log4j.Logger
- @XSlf4j: org.slf4j.ext.XLogger
Pandi
Is there any way to change Logging level based on User:
Ex:
log.setLevel(Level.WARN)
currently not supporting