Logging custom attributes in log4j.

Hi,

I googled and surfed the forum for a long time, but I couldn’t find how to log custom attributes by log4j in Vaadin 7.

Usually this goal is accomplished using MDC.put(‘key’) and MDC.remove(‘key’) at the beginning and end of a transaction. In Vaadin 6 there was a TransactionListener interface that could be implemented by a class extending Application, where we could trap begin and end of transaction.

Now, I cannot understand how to do the same in Vaadin 7: can anyone help me?

Alessandro.

Vaadin uses java.util.logging, not log4j.

Thus, log4j specific APIs do not affect the logging performed by Vaadin - though you should be able to configure your system to forward logs from java.util.logging to log4j, and perhaps that is what you were already doing with Vaadin 6.

As for intercepting the beginning and end of a request/transaction/…, this also depends on which level you want to intercept it. If you want to do this for all kinds of requests, overriding VaadinServlet.service() or using a servlet request listener (which has some limitations when using server push) are among the available options.

Thanks, Henry

Following your advice, and reading thread 3214685, I could solve my problem extending VaadinServlet.

My code:

public static class Servlet extends VaadinServlet
{
    @Override
    protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException
    {
        VaadinServletService servletService = new VaadinServletService(this, deploymentConfiguration)
        {           
            
            @Override
            public void requestStart(VaadinRequest request, VaadinResponse response)
            {
                super.requestStart(request, response);
                MDC.put("address", request.getRemoteAddr());
            }

            @Override
            public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session)
            {
                super.requestEnd(request, response, session);
                MDC.remove("address");
            }
        };
        servletService.init();
        return servletService;
    }
}