Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
If we are now talking about a web application, then this can be done using javax.servlet.Filter .
Those. we can implement this interface, write our implementation in web.xml, forcing our filter to intercept all requests.
Our filter in the doFilter method reaches the class that knows about the SessionId and adds [SessionId] to the name of the current thread (Thread.currentThread().setName(name);).
And in the log4j template, specify the output of the thread name through %t ( PatternLayout )
A crude solution, but nonetheless... First, we
need to create an implementation of ServletRequestListener , in which we will receive the session id and associate it with the thread processing the current request using ThreadLocal .
package ua.home.web.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
public class RequestListener implements ServletRequestListener {
private static final ThreadLocal<String> sessionIds = new ThreadLocal<>();
public static String getCurrentSessionId() {
return sessionIds.get();
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
String id = request.getSession().getId();
sessionIds.set(id);
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) { }
}
<listener>
<listener-class>ua.home.web.listener.RequestListener</listener-class>
</listener>
ServletRequestListener.getCurrentSessionId()
, we can get the session id outside of the servlet. package ua.home.web.log;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
import ua.home.web.listener.RequestListener;
public class CustomPatternLayout extends PatternLayout {
@Override
public String format(LoggingEvent event) {
return super.format(event).replace("%sid", RequestListener.getCurrentSessionId());
}
}
log4j.rootCategory=INFO,S
log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=ua.home.web.log.CustomPatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %sid %m%n
LOG.info("log message")
, then the line will be written to the log:2016-08-23 12:20:18 TestServlet [INFO] 441135221F8A492364C836560BDEE15E log message
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question