2016-07-26 75 views
0

我想使用log4j的記錄它的文件 這裏是代碼log4j的嘗試登錄文件

protected static Logger logger = Logger.getLogger(Application.class); 
    private static final String DIRECTORY = "/Users/me/Desktop"; 
    private static final String EXTENSION = ".log"; 

    protected void setupLogger(String fileName) { 
     SimpleLayout layout = new SimpleLayout(); 
     FileAppender appender = new FileAppender(layout, DIRECTORY + "/logs/" + fileName + EXTENSION, false); 
     logger.addAppender(appender); 
     logger.setLevel((Level) Level.DEBUG); 
    } 

,並在這裏是POM中,我使用http://pastebin.com/vXdFtzSU

,我的堆棧跟蹤得到的是這裏

Error:(40, 28) java: incompatible types: org.apache.log4j.FileAppender cannot be converted to org.apache.log4j.Appender 

我想按照這個答案configure log4j to log to custom file at runtime

+0

can you s你的導入聲明如何 – Raghu

+0

import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; import org.apache.log4j.FileAppender; –

+0

嘗試從下面的命令mvn dependency:tree中找出依賴關係樹,並將其發佈到此處。有一個log4j jars.i的版本衝突,我認爲你需要從其他工件依賴中排除log4j。 –

回答

2

嗨嘗試了變化Maven的依賴性增加這種依賴性:

<dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.17</version> 
    </dependency> 

,改變彈簧啓動依賴排除日誌依賴關係:

 <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
      <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-logging</artifactId> 
      </exclusion> 
      </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
      <exclusions> 
     <exclusion> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-logging</artifactId> 
     </exclusion> 
     </exclusions> 
    </dependency> 

再加入try/catch語句:

protected void setupLogger(String fileName) { 

     try { 
      SimpleLayout layout = new SimpleLayout(); 
      FileAppender appender; 
      appender = new FileAppender(layout, DIRECTORY + "/logs/" +  fileName + EXTENSION, false); 
      logger.addAppender(appender); 
      logger.setLevel((Level) Level.DEBUG); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

仍然是相同的錯誤 –

+1

檢查我的答案更新 –

+0

現在文件保存在我指定的路徑,但我從setFile http: //pastebin.com/FZ6nmTeY –

0

它幾乎看起來像在運行時使用不同版本的libraray,而不是在編譯時。如果這些類型真的不兼容會導致編譯器錯誤。如果您正在像Tomcat等特殊環境中運行程序,請檢查是否安裝了相同版本的log4j。

0

爲什麼不使用log4j.properties這是一個非常簡單的配置打印到文件。

log4j.logger.register=INFO,R7 
log4j.appender.R7=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.R7.DatePattern='.'yyyyMMdd 
log4j.appender.R7.File=/appLogs/address/logFile.log 
log4j.appender.R7.layout=org.apache.log4j.PatternLayout 
log4j.appender.R7.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} | %m%n 


private static final Logger logger = Logger.getLogger("register"); 

logger.info("print to file and console"); 
+0

我已經試過,但它不讀我的屬性。這裏的屬性http://pastebin.com/Y8JKCuTY也許是因爲春天 –