java編寫日誌

來源:互聯網
上載者:User

最近做j2ee的時候:運行tomcat後總是出現:

log4j:WARN No appenders could be found for logger (samp.Test1).
log4j:WARN Please initialize the log4j system properly.

 

查閱資料:搞清問題還是出在日誌設定檔的初始化的時候,也就是該應用程式找不到log4j.properties.那麼我們針對這個癥結來解決!

查了很多資料,解決方案一大堆,不過真要解決實際問題,那還得以實而論,我這個程式就只是為瞭解決這個警告,最簡單的就是將debug資訊輸出到控制台,那就可以這樣寫:

 

下面這個是先初始化log

import com.foo.Bar;

// Import log4j classes.
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;


public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
}
接著我們就可以直接用了:

 package com.foo;
import org.apache.log4j.Logger;

public class Bar {
   static Logger logger = Logger.getLogger(Bar.class);

   public void doIt() {
     logger.debug("Did it again!");
   }
}

 

 

說到底最關鍵的還是BasicConfigurator.configure();

在我們的web合適的地方加上這句,就可以在控制台看到我們的調試資訊了!

另外一種我們要寫入到我們的調試檔案:就這樣寫了.

我們寫個這樣的初始化properties的類:

package com.foo;import org.apache.log4j.PropertyConfigurator;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.PrintWriter;import java.io.IOException;public class Log4jInit extends HttpServlet {  public  void init() {    String prefix =  getServletContext().getRealPath("/");    String file = getInitParameter("log4j-init-file");    // if the log4j-init-file is not set, then no point in trying    if(file != null) {      PropertyConfigurator.configure(prefix+file);    }  }  public  void doGet(HttpServletRequest req, HttpServletResponse res) {  }}

在設定檔中這樣寫:

<servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>com.foo.Log4jInit</servlet-class>

    <init-param>
      <param-name>log4j-init-file</param-name>
      <param-value>WEB-INF/classes/log4j.lcf</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

 

這樣警告就離我遠去了!
上面這兩個例子只是針對Hibernate中加的log,當然可能因為所用的eclipse版本不一樣會有些差別.

比如在用spring的時候也會有這個問題,那麼我們解決的時候,就可以按照大蝦們介紹的:

 

<context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>

······

<!-- 定義LOG4J監聽器 -->
<listener>
   <listener-class>
org.springframework.web.util.Log4jConfigListener
   </listener-class>
</listener>


當然還有的說在ContextLoaderListener改為SpringContextServle等等,這些問題還是哪句話,事實而論!

 

更多介紹參見:

http://logging.apache.org/log4j/1.2/manual.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.