A logging filter - no log file is created

Posted on 2005-04-16
Last Modified: 2010-04-01

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

package moreservlets.filters;

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...
Question by:dkim18
    LVL 19

    Expert Comment

    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.

    Author Comment

    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.)

    Author Comment

    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.)
    LVL 19

    Accepted Solution

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Synchronize a new Active Directory domain with an existing Office 365 tenant
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now