Where does the ServletContext.log messages go in t

2019-07-23 13:41发布

This question already has an answer here:

I am using Java 7, tomcat 7. While learning about servlet logging I wrote a simple program.

package net.codejava;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class LogCheck
 */
@WebServlet("/LogCheck")
public class LogCheck extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public LogCheck() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    ServletContext context = getServletContext();
    String par = request.getParameter("msg");

    if (par == null || par.equals("")){
        context.log("No Message received",new IllegalStateException("Missing Parameter"));
    }else{
        context.log("Here is visitor's message:"+par);
    }

    PrintWriter out = response.getWriter();
    out.println("Log Testing.");

}

}

Now I am able to see the log messages in my Eclipse console. But I do not see any files under Tomcat Home/logs folder ? But I read that it should store the log messages in some log files. I do not see any log file in my case.

Am I missing some configuraions? Please help

2条回答
Summer. ? 凉城
2楼-- · 2019-07-23 14:06

They will be logged to the file localhost.${today}.log.

As Maks said, ServletContext#log will logged with the category org.apache.catalina.core.ContainerBase. and when you look at Tomcat's logging configuration in conf/logging.properties, you'll see that these messages go to files with the prefix localhost.

查看更多
等我变得足够好
3楼-- · 2019-07-23 14:19

https://tomcat.apache.org/tomcat-7.0-doc/logging.html

The calls to javax.servlet.ServletContext.log(...) to write log messages are handled by internal Tomcat logging. Such messages are logged to the category named

org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}] This logging is performed according to the Tomcat logging configuration. You cannot overwrite it in a web application.

Where the logs live:

When running Tomcat on unixes, the console output is usually redirected to the file named catalina.out. The name is configurable using an environment variable. When running as a service on Windows, the console output is also caught and redirected, but the file names are different.

查看更多
登录 后发表回答