Lombok 之 Log
2016-06-15 22:26
316 查看
LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:
@Cleanup
@Getter, @Setter
@ToString
@EqualsAndHashCode
@Constructor
@Data & @Value
@SneakyThrows
@Synchronized
@Getter(lazy=true)
@Log
这是本系列最后一个annotation了,也是Lombok里面最好用的一个了,我们每天写项目都会有很多日志需要记录,很多人都写过这样的代码:
Java代码
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代码
@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);
就用其中的几个举个例子吧:
Java代码
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");
}
}
翻译一下,代码如下:
Java代码
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啊
@Cleanup
@Getter, @Setter
@ToString
@EqualsAndHashCode
@Constructor
@Data & @Value
@SneakyThrows
@Synchronized
@Getter(lazy=true)
@Log
这是本系列最后一个annotation了,也是Lombok里面最好用的一个了,我们每天写项目都会有很多日志需要记录,很多人都写过这样的代码:
Java代码
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代码
@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);
就用其中的几个举个例子吧:
Java代码
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");
}
}
翻译一下,代码如下:
Java代码
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啊
相关文章推荐
- MySQL Server 日志
- MSSQL 2005 LOG备份webshell的方法
- 使用MySQL Slow Log来解决MySQL CPU占用高的问题
- 清理SQL Server 2008日志文件Cannot shrink log file 2 的解决方案
- Android系统开发中log的使用方法及简单的原理
- googlemap 之 javascript实现方法
- android杂记:C++文件的添加log方法分享
- 提取oralce当天的alert log的shell脚本代码
- mysql正确安全清空在线慢查询日志slow log的流程分享
- PHP error_log()将错误信息写入一个文件(定义和用法)
- Apache启动错误Permission denied: httpd: could not open error log file解决方法
- Android开发笔记之:Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
- Android编程之基于Log演示一个activity生命周期实例详解
- PHP如何将log信息写入服务器中的log文件
- SQL Server中减小Log文件尺寸的方法分享
- Python中使用logging模块打印log日志详解
- python根据文件大小打log日志
- Android将应用调试log信息保存在SD卡的方法
- Hadoop配置常见Log错误指导
- BIND(三)—— DNS工具和BIND日志.md