08. 《Lombok 实战 —— @Log》

x33g5p2x  于2021-12-25 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(204)

lombok v0.10中添加了各种@Log变体。 lombok 0.10中的添加此功能,使用@Log对任何类进行注释,以使lombok生成的logger字段对代码进行注释操作。

1. @Log 体系简介

你把@Log的变体放在你的类上,但是对于不同的日志体系可以对号入座:

  • @CommonsLog
private static final org.apache.commons.logging.Log log = 
    org.apache.commons.logging.LogFactory.getLog(LogExample.class);
  • @Flogger
private static final com.google.common.flogger.FluentLogger log = 
    com.google.common.flogger.FluentLogger.forEnclosingClass();
  • @JBossLog
private static final org.jboss.logging.Logger log = 
    org.jboss.logging.Logger.getLogger(LogExample.class);
  • @Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
  • @Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
  • @Log4j2
private static final org.apache.logging.log4j.Logger log = 
    org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
  • @Slf4j
private static final org.slf4j.Logger log = 
    org.slf4j.LoggerFactory.getLogger(LogExample.class);
  • @XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

2. @Log 实战使用

以上便是lombok为我们提供的日志模块,我常用的会有以下几种:@Log@Slf4j@CommonsLog

在进行测试之前需要引用两个依赖,分别是slf4j的依赖和Apache的commons-logging

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

下面我们简单实现代码:

@Log
public class LogExample {
    public static void main(String[] args) {
        log.severe("this is @Log testing.");
    }
}

@Slf4j
class Slf4jExample {
    public static void main(String[] args) {
        log.info("this is @Slf4j testing.");
    }
}

@CommonsLog
class CommonsLogExample{
    public static void main(String[] args) {
        log.info("this is @CommonsLog testing.");
    }
}

以上的代码编译之后为:

public class LogExample {
    private static final Logger log = Logger.getLogger(LogExample.class.getName());
    public static void main(String[] args) {
        log.severe("this is @Log testing.");
    }
}

class Slf4jExample {
    private static final Logger log = LoggerFactory.getLogger(Slf4jExample.class);
    public static void main(String[] args) {
        log.info("this is @Slf4j testing.");
    }
}

class CommonsLogExample {
    private static final Log log = LogFactory.getLog(CommonsLogExample.class);
    public static void main(String[] args) {
        log.info("this is @CommonsLog testing.");
    }
}

使用不同的注解,lonbok将为我们选择不同的日志框架,但是前提一定要添加此日志框架的依赖。

3. @Log 注解详解

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Log {
	// 指定Logger在上下文的名称,默认名称是:类的全包名。
	String topic() default "";
}
  • 测试 topic 属性
// 默认名称是:类的全包名,e.g. com.gitee.jdkong.lombok.log.LogExample
@Log(topic = "qwe")	
public class LogExample {
    public static void main(String[] args) {
        System.out.println(log.getName());
        log.severe("this is @Log testing.");
    }
}

// 输出内容
qwe
一月 08, 2019 1:10:23 下午 com.gitee.jdkong.lombok.log.LogExample main
严重: this is @Log testing.
  • 编译后的代码
public class LogExample {
    private static final Logger log = Logger.getLogger("qwe");
    //默认:private static final Logger log = Logger.getLogger(LogExample.class.getName());
    public LogExample() {}
    public static void main(String[] args) {
        System.out.println(log.getName());
        log.severe("this is @Log testing.");
    }
}

4. @Log 全局配置

# 生成的logger 的 fieldname 默认为“log”,你可以使用此设置将其更改为其他名称,比如:testLog。
# 那么你在代码中使用时,就要使用 testLog.info(""); 之类的。
lombok.log.fieldName = an identifier (default: log).
# 使用的 field 是否是静态的调用,默认静态调用的。
lombok.log.fieldIsStatic = [true | false] (default: true)
# 是否启用@Log注解。(以下类似)
lombok.log.flagUsage = [warning | error] (default: not set)
lombok.log.apacheCommons.flagUsage = [warning | error] (default: not set)
lombok.log.flogger.flagUsage = [warning | error] (default: not set)
lombok.log.jbosslog.flagUsage = [warning | error] (default: not set)
lombok.log.javaUtilLogging.flagUsage = [warning | error] (default: not set)
lombok.log.log4j.flagUsage = [warning | error] (default: not set)
lombok.log.log4j2.flagUsage = [warning | error] (default: not set)
lombok.log.slf4j.flagUsage = [warning | error] (default: not set)
lombok.log.xslf4j.flagUsage = [warning | error] (default: not set)
  • 测试lombok.log.fieldIsStatic

  • 全局配置

config.stopBubbling = true
clear lombok.log.fieldIsStatic
lombok.log.fieldIsStatic = false
  • 代码实现
@Log
public class LogExample {

    private void testFieldIsNotStatic() {
        log.severe("this is @Slf4j testing.");
    }
	// 由于取消了 log 的静态属性,所以在main方法中不能直接调用
    public static void main(String[] args) {
        LogExample example = new LogExample();
        example.testFieldIsNotStatic();
    }
}
// 编译后:
public class LogExample {
    private final Logger log = Logger.getLogger(LogExample.class.getName());
    public LogExample() {}

    private void testFieldIsNotStatic() {
        this.log.severe("this is @Slf4j testing.");
    }
    public static void main(String[] args) {
        LogExample example = new LogExample();
        example.testFieldIsNotStatic();
    }
}

这样我们就可以使用非静态的Log对象来帮我们操作日志了。

  • 测试 lombok.log.fieldName

  • 全局配置

# 全局配置
config.stopBubbling = true
clear lombok.log.fieldName
lombok.log.fieldName = testLog
  • 代码实现

上面指定了日志对象的变量名,所以

@Log
public class LogExample {
    public static void main(String[] args) {
        testLog.severe("this is @Log testing.");
    }
}
// 编译后:
public class LogExample {
    private static final Logger testLog = Logger.getLogger(LogExample.class.getName());
    public LogExample() { }

    public static void main(String[] args) {
        testLog.severe("this is @Log testing.");
    }
}

参考文档

相关文章