3

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

user3427540
  • 1,162
  • 1
  • 14
  • 30

2 Answers2

4

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.

Peter Clause
  • 1,132
  • 9
  • 22
1

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.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Maks
  • 202
  • 1
  • 9
  • 1
    `When running as a service on Windows, the console output is also caught and redirected, but the file names are different`. Where can I find these files? – user3427540 Jun 26 '15 at 03:40
  • well, I'm mac user, so I don't know, but you can check this: http://stackoverflow.com/questions/8362280/where-is-tomcat-console-output-on-windows – Maks Jun 27 '15 at 06:35