2014-10-03 102 views
0

這是我10年來的第一個java程序。我試圖做格式化打印,而與Log4J日誌錯誤2.java中的log4j格式化打印

我的代碼是

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 

public class Feeder { 

    private static final Logger logger = LogManager.getLogger(Feeder.class); 

    public static void main(String args[]) throws InterruptedException { 

     try { 
      for (int i = 0; i < 100; i++) { 
       logger.info("Hello World " + i); 
       Thread.sleep(4000); 
       int x = 100/0; 
      } 
     } 
     catch (Exception ex){ 
      StackTraceElement var[] = ex.getStackTrace(); 
      for(StackTraceElement s: var){ 
       logger.error("Error in Filename = %s", s.getFileName()); 
       logger.error("Error in Class = " + s.getClassName()); 
       logger.error("Error in line number " + s.getLineNumber()); 
       logger.error("Error in method " + s.getMethodName()); 
      } 

      //logger.error(ex.); 
      logger.error(ex.getMessage()); 
     } 
    } 
} 

日誌吐以下

03 Oct 2014 12:12:46,302 INFO com.bluepoint.Feeder - Hello World 0 
03 Oct 2014 12:12:50,304 ERROR com.bluepoint.Feeder - Error in Filename = %s 

如何獲得文件的名稱,而不是

%s

回答