Lombok之@Log使用

一. 為什么要用?

為啥用@Log ,簡(jiǎn)化代碼。是代碼更簡(jiǎn)潔。

二. 如何使用?

@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);class Test {
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
@Log
public class LogExample {
    public static void main(String... args) {
        log.severe("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");
    }
}

反編譯后的代碼如下: 發(fā)現(xiàn)了沒,和單例懶漢模型,雙檢查鎖機(jī)制是不是很像啊

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.severe("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");
  }
}

三. 源碼

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
public @interface Log {
    String topic() default "";
}

四. 特別說明

本文已經(jīng)收錄在Lombok注解系列文章總覽中,并繼承上文中所提的特別說明

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. 為什么要用? 現(xiàn)在需要對(duì)一個(gè)成員變量初始化,單初始化很復(fù)雜需要一個(gè)初始化函數(shù)。要怎么做呢。一直方法publi...
    問道九九閱讀 1,737評(píng)論 0 0
  • 一. 為什么要用? 首先wither 是在in lombok v1.18.10. 之前,在in lombok v1...
    問道九九閱讀 6,752評(píng)論 0 0
  • 一. 為什么要用@Cleanup? 在IO流的學(xué)習(xí)中,每次都要在finally里面關(guān)閉資源,是不是很讓人頭疼?那么...
    Cauchy6317在簡(jiǎn)書閱讀 3,100評(píng)論 0 0
  • 一、lombok是什么 ??java中的javabean需要添加相應(yīng)的getter/setter,即使idea等都...
    chushiyan閱讀 829評(píng)論 0 0
  • 1.Lombok簡(jiǎn)介 大概的意思:Lombok是一個(gè)Java庫(kù),能自動(dòng)插入編輯器并構(gòu)建工具,簡(jiǎn)化Java開發(fā)。通過...
    奇點(diǎn)一氪閱讀 1,112評(píng)論 1 2

友情鏈接更多精彩內(nèi)容