• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

A logging filter - no log file is created


I am testing LogFilter class, but nothing appear under logs directory. I have this class.

package moreservlets.filters;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class LogFilter implements Filter {
  protected FilterConfig config;
  private ServletContext context;
  private String filterName;
  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain)
      throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest)request;
    context.log(req.getRemoteHost() +
                " tried to access " +
                req.getRequestURL() +
                "(Reported by " + filterName + ".)");

  public void init(FilterConfig config)
      throws ServletException {
    this.config = config; // In case it is needed by subclass.
    context = config.getServletContext();
    filterName = config.getFilterName();
  public void destroy() {}
and I put this in web.xml file.



After I request any servlet or jsp page, I don’t see anything log file. What am I doing wrong?

thanks in advance...
  • 2
  • 2
1 Solution
First try to log directly to stdout to see that your filter is called.
Replace context.log(.. by System.out.println(..
If you see on servlet runner stdout your your lines then it is something wrong with logging configuration of servlet runner itself. Check logging config of your web application server.
dkim18Author Commented:
thanks for your help.

I replaced context.log by System.out.println and I got this in tomcat windows and still no log file has been created. Is that mean logging config of my web application server is something wrong? tried to access http://localhost:8080/~dkim18/plugSite/CEONamePage1.js
p(Reported by Logger.)
dkim18Author Commented:
When I tried  context.log again, I got the following.

2005. 4. 17. &#50724;&#54980; 5:13:12 org.apache.catalina.core.ApplicationContext log
info: tried to access http://localhost:8080/~dkim18/plugSite/CEONamePa
ge1.jsp(Reported by Logger.)
in tomcat server.xml file you can provide logging file per context:
for example in default tomcat server.xml following lines exist:
        <Context path="/examples" docBase="examples" debug="0"
                 reloadable="true" crossContext="true">
          <Logger className="org.apache.catalina.logger.FileLogger"
                     prefix="localhost_examples_log." suffix=".txt"

it means that for context /examples context log will be logged to file localhost_examples.log....
You should add appropriate <Logger> element to your context. Or you can put Logger for <Host> element

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now