Lombok 之 Log

来源:互联网 发布:雷科防务怎么样 知乎 编辑:程序博客网 时间:2024/05/17 09:20

LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:

@Cleanup     

@Getter, @Setter

@ToString

@EqualsAndHashCode

@Constructor

@Data & @Value

@SneakyThrows

@Synchronized

@Getter(lazy=true)

@Log

 

这是本系列最后一个annotation了,也是Lombok里面最好用的一个了,我们每天写项目都会有很多日志需要记录,很多人都写过这样的代码:

Java代码  收藏代码
  1. private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);  

 

Lombok的方便不是说说而已的,哪里有重复,哪里就有Lombok(YY的 :) ),Lombok封装了许多主流的Log库,提供了一系列关于Log 的annotation。下面就是所有的annotation会代表哪些特定的类 :

 

Java代码  收藏代码
  1. @CommonsLog  
  2. Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);  
  3. @Log  
  4. Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());  
  5. @Log4j  
  6. Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);  
  7. @Log4j2  
  8. Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);  
  9. @Slf4j  
  10. Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);  
  11. @XSlf4j  
  12. Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);  

 

就用其中的几个举个例子吧:

Java代码  收藏代码
  1. import lombok.extern.java.Log;  
  2. import lombok.extern.slf4j.Slf4j;  
  3.   
  4. @Log  
  5. public class LogExample {  
  6.     
  7.   public static void main(String... args) {  
  8.     log.error("Something's wrong here");  
  9.   }  
  10. }  
  11.   
  12. @Slf4j  
  13. public class LogExampleOther {  
  14.     
  15.   public static void main(String... args) {  
  16.     log.error("Something else is wrong here");  
  17.   }  
  18. }  
  19.   
  20. @CommonsLog(topic="CounterLog")  
  21. public class LogExampleCategory {  
  22.   
  23.   public static void main(String... args) {  
  24.     log.error("Calling the 'CounterLog' with a message");  
  25.   }  
  26. }  

 

翻译一下,代码如下:

Java代码  收藏代码
  1. public class LogExample {  
  2.   private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());  
  3.     
  4.   public static void main(String... args) {  
  5.     log.error("Something's wrong here");  
  6.   }  
  7. }  
  8.   
  9. public class LogExampleOther {  
  10.   private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);  
  11.     
  12.   public static void main(String... args) {  
  13.     log.error("Something else is wrong here");  
  14.   }  
  15. }  
  16.   
  17. public class LogExampleCategory {  
  18.   private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");  
  19.   
  20.   public static void main(String... args) {  
  21.     log.error("Calling the 'CounterLog' with a message");  
  22.   }  
  23. }  

 

想必不用多说了,例子一看就懂了,唠叨一句就是annotation提供一个topic选项,可以定制化getLog方法的参数。 为什么没提供backlog呢? 一直用backlog啊

0 0