关于servlet的日志配置

时间:2012-07-23 14:01:17

标签: java java-ee tomcat servlets log4j

我正在开发一个应用程序,我想配置log4j,因为我想为我的项目完成单独的日志记录,以便创建一个单独的日志文件..下面是我项目的图片请点击网址看到它.. snapshot of the project structure 现在这里是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].

请告知如何生成单独的日志,请告知

1 个答案:

答案 0 :(得分:0)

这意味着log4j.properties文件不在您的类路径中。您需要将它放在“src”文件夹下,然后将应用程序重新部署到tomcat。