lemonzone2010 / javamelody

Automatically exported from code.google.com/p/javamelody
0 stars 0 forks source link

Logging of user access to /monitoring needed #322

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Our application logs all user access to authenticated pages.  We would like to 
be able to log user access to /monitoring as well, but this does not currently 
seem to be possible.  I would appreciate it if more logging statements were 
added to HTMLCoreReport.java, so that we can tie that into our application 
logging.

Original issue reported on code.google.com by ConorWSu...@gmail.com on 18 Jul 2013 at 7:00

GoogleCodeExporter commented 9 years ago
You can add a servlet filter to log whatever you want, with whatever logging 
framework you want.

For example, with the following class:
package mypackage;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

public class LogFilter implements Filter {
    private Logger logger;

    public void init(FilterConfig filterConfig) throws ServletException {
        logger = Logger.getLogger(getClass());
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        // it doesn't seem necessary to log dozens of requests on resources or graphics
        if (httpRequest.getParameter("resource") == null
                && httpRequest.getParameter("graph") == null) {
            String queryString = httpRequest.getQueryString();
            logger.info("request from " + httpRequest.getRemoteAddr() + ": "
                    + httpRequest.getRequestURI() + (queryString == null ? "" : '?' + queryString));
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
    }
}

You can add the following in the web.xml file of your webapp to log access to 
"/monitoring":
    <filter>
        <filter-name>logmonitoring</filter-name>
        <filter-class>mypackage.LogFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>logmonitoring</filter-name>
        <url-pattern>/monitoring</url-pattern>
    </filter-mapping>

Original comment by evernat@free.fr on 20 Jul 2013 at 10:08