2012-07-23 44 views
0

我正在開發一個應用程序,我想配置log4j,因爲我想爲我的項目完成單獨的日誌記錄,以便創建單獨的日誌文件..下面是我的項目的圖片點擊鏈接直接看到它.. snapshot of the project structure 現在,這裏是servlet關於servlet的日誌配置

package com.saral; 

import java.io.IOException; 
import org.apache.log4j.Logger; 
import org.apache.log4j.PropertyConfigurator; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

/** 
* Servlet implementation class First 
*/ 
//@WebServlet("/First") 
public class MyServlet extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 
    static final Logger logger = Logger.getLogger(MyServlet.class); 


    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     PropertyConfigurator.configure("log4j.properties"); 
     logger.info("before---->"); 

     // TODO Auto-generated method stub 
     String name=request.getParameter("txtName"); 
     response.setContentType("text/html"); 
     PrintWriter out=response.getWriter(); 
     out.println("Hello,"+name); 
     out.println("<br> this output is generated by a simple servlet."); 
     out.println("Total Number of users logged in--->"+request.getSession().getServletContext().getAttribute("sessionCount")); 
     out.close(); 
}} 

和web.xml中的結構是

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>FirstDemo</display-name> 



    <servlet> 
    <servlet-name>hello</servlet-name> 
    <servlet-class>com.saral.MyServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/helloServlet</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>home.html</welcome-file> 
    </welcome-file-list> 


    <listener> 
    <listener-class>com.saral.SessionCounter</listener-class> 
</listener> 

</web-app> 

但在Tomcat上運行的應用程序它顯示了以下錯誤..

 INFO: Server startup in 888 ms 
log4j:ERROR Could not read configuration file [log4j.properties]. 
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:306) 
    at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:324) 
    at com.saral.MyServlet.doGet(MyServlet.java:29) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
log4j:ERROR Ignoring configuration file [log4j.properties]. 

請指教如何生成單獨的日誌,請大家指教

回答

0

這意味着log4j.properties文件是不是在你的類路徑中。您需要將其放在「src」文件夾下,然後將應用程序重新部署到tomcat。

+0

沒有人我已經清理項目並重新部署,但仍然出現錯誤我生成分離日誌的主要目的是研究在後臺發生的事件 – user1538526 2012-07-23 15:07:29

+0

嘗試將文件放在與servlet相同的級別上(即在src/com下/ SARAL)。如果這不起作用,請嘗試給出絕對路徑:PropertyConfigurator.configure(「c:/path/to/log4j.properties」); – Tomer 2012-07-23 15:10:25