【每天学个新注解】Day 3 Lombok注解简解(二)—@Log

@Log

自动创建并初始化日志记录器
日志系列注解包括:@CommonsLog、@Flogger、@JBossLog、@Log、@Log4j、@Log4j2、@Slf4j、@XSlf4j、@CustomLog,对应于不同的日志框架。每个注解都会在编译时生成一个名为 log 的静态字段,该字段被初始化为对应的日志框架的 Logger 实例。

@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(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);
@CustomLog
Creates private static final com.foo.your.Logger log = com.foo.your.LoggerFactory.createYourLogger(LogExample.class);

1、如何使用

  • 为每个类添加重复的日志记录器代码时,可以用@Log注解代替,根据项目架构选取的日志实现选择具体的日志注解。
    @CustomLog:区分场景日志,单独的日志记录器来采集日志。可以将多个日志记录器整合在一起,也可以将自动定义的日志处理器整合到一起,实现一个注解通过不通代码代码将日志不同位置或者不同格式进行输出。

2、代码示例

例:推荐使用 @Slf4j 注解。
SLF4J 是一个抽象的日志框架,它可以在运行时绑定到具体的日志实现(例如 Logback、Log4j2 等),无需管项目中具体使用的日志框架。

@Slf4j
@ToString
public class People {
    private String name;
    private int age;
    private String sex;


    public void testLog() {
        log.info(toString());
    }
}

编译后:自动生成了日志记录器。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值