You don't want to retrieve the current session from the thread local variable for each logging line (which is what VaadinSession.getCurrent()
does). The API uses a static String type as that is the fastest.
Vaadin actually does have SessionInitListener
and SessionDestroyListener
, but that is also not what you want: MDC is thread local, but not all requests within the same session are handled within the same thread. So you have to set the value in the MDC at each request in an implementation of RequestHandler
. I don't think Vaadin has a callback interface for the end of a request, so it seems there's no good place to clear the value.
UPDATE: after this answer got accepted, I found out that there is in fact a better way to set and clear the value so that if the server recycles threads for different sessions, it doesn't contain false information. What you should do is subclass VaadinServlet
or VaadinPortlet
and override createServletService()
to return a custom subclass of VaadinServletService
or VaadinPortletService
that in turn overrides both requestStart()
and requestEnd()
to respectively set and remove the values in the MDC.