@slf4j注解找不到log變量


問題描述:使用了@Slf4j注解但是沒有找到log對象。

  

 

解決辦法:

  eclipse安裝lombok.jar。

  • 1.下載lombok.jar包https://projectlombok.org/download.html

  • 2.運行Lombok.jar: Java -jar D:\software\lombok.jar D:\software\lombok.jar這是windows下lombok.jar所在的位置

      數秒后將彈出一框,以確認eclipse的安裝路徑</code>
  • 3.確認完eclipse的安裝路徑后,點擊install/update按鈕,即可安裝完成

  • 4.安裝完成之后,請確認eclipse安裝路徑下是否多了一個lombok.jar包,並且其

    配置文件eclipse.ini中是否 添加了如下內容: </code>
        -javaagent:lombok.jar 
        -Xbootclasspath/a:lombok.jar 
    那么恭喜你已經安裝成功,否則將缺少的部分添加到相應的位置即可 </code>
  • 5.重啟eclipse或myeclipse

 日志注解的解釋和用法說明:

Overview

You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized to the name of your class, which you can then use to write log statements.

There are several choices available:

@CommonsLog
Creates  private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@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);

 

By default, the topic (or name) of the logger will be the class name of the class annotated with the @Log annotation. This can be customised by specifying the topic parameter. For example: @XSlf4j(topic="reporting").

With Lombok

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

Vanilla 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");
  }
}

Supported configuration keys:

lombok.log.fieldName =  an identifier (default:  log).
The generated logger fieldname is by default ' log', but you can change it to a different name with this setting.
lombok.log.fieldIsStatic = [ true |  false] (default: true)
Normally the generated logger is a  static field. By setting this key to  false, the generated field will be an instance field instead.
lombok.log.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of any of the various log annotations as a warning or error if configured.
lombok.log.apacheCommons.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.apachecommons.CommonsLog as a warning or error if configured.
lombok.log.javaUtilLogging.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.java.Log as a warning or error if configured.
lombok.log.jbosslog.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.jbosslog.JBossLog as a warning or error if configured.
lombok.log.log4j.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.log4j.Log4j as a warning or error if configured.
lombok.log.log4j2.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.log4j.Log4j2 as a warning or error if configured.
lombok.log.slf4j.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.slf4j.Slf4j as a warning or error if configured.
lombok.log.xslf4j.flagUsage = [ warning |  error] (default: not set)
Lombok will flag any usage of  @lombok.extern.slf4j.XSlf4j as a warning or error if configured.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM