Question

I have a small project that is written in Groovy and deployed as a groovlet on Tomcat 6.0. My issue now that I need to monitor all incoming requests and perform on each request custom logging.

First I thought to include into each class a short code that outputs what I need, but then I figured may be Tomcat can send/duplicate each incoming http request to my custom logging class that will parse it to my liking and perform few more things.

Is it possible to do so? If so how?

P.S: Tomcat must be original - no modifications/custom compilations.

Thank you.

Was it helpful?

Solution

You can add a custom filter that will do the logging.

Here is a sample Filter implementation:

public class YourFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        // do your initialization here
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        // do your custom logging here
    }

    public void destroy() {
        // do whats necessary on end of life
    }
}

Configure/activate the Filter inside the web.xml.

...
<filter>
    <filter-name>yourFilter</filter-name>
    <filter-class>org.example.YourFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>yourFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>  
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top