Lombok 之 Log
LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:
@Cleanup
@Getter, @Setter
@ToString
@EqualsAndHashCode
@Constructor
@Data & @Value
@SneakyThrows
@Synchronized
@Getter(lazy=true)
@Log
这是本系列最后一个annotation了,也是Lombok里面最好用的一个了,我们每天写项目都会有很多日志需要记录,很多人都写过这样的代码:
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会代表哪些特定的类 :
@CommonsLog Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class); @Log Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); @Log4j Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); @Log4j2 Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class); @Slf4j Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); @XSlf4j Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
就用其中的几个举个例子吧:
import lombok.extern.java.Log; import lombok.extern.slf4j.Slf4j; @Log public class LogExample { public static void main(String... args) { log.error("Something's wrong here"); } } @Slf4j public class LogExampleOther { public static void main(String... args) { log.error("Something else is wrong here"); } } @CommonsLog(topic="CounterLog") public class LogExampleCategory { public static void main(String... args) { log.error("Calling the 'CounterLog' with a message"); } }
翻译一下,代码如下:
public class LogExample { private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); public static void main(String... args) { log.error("Something's wrong here"); } } public class LogExampleOther { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class); public static void main(String... args) { log.error("Something else is wrong here"); } } public class LogExampleCategory { private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog"); public static void main(String... args) { log.error("Calling the 'CounterLog' with a message"); } }
想必不用多说了,例子一看就懂了,唠叨一句就是annotation提供一个topic选项,可以定制化getLog方法的参数。 为什么没提供backlog呢? 一直用backlog啊
相关推荐
zhuxue 2020-10-14
zhangbingb 2020-09-21
HeronLinuxampARM 2020-09-14
美丽的泡沫 2020-09-08
goodstudy 2020-08-19
luvhl 2020-08-17
littleFatty 2020-08-16
gamestart0 2020-08-15
URML 2020-08-15
sfkong 2020-08-02
82941732 2020-07-27
偏头痛杨 2020-07-18
timewind 2020-07-04
89407707 2020-06-27
xiaoxiaoCNDS 2020-06-26
lyjava 2020-06-26
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算。以上实例中 7、5 和 12 是操作数。关系运算符用于计算结果是否为 true 或者 false。逻辑运算符用于测定变量或值之间的逻辑。
ChaITSimpleLove 2020-06-25
Strongding 2020-06-25