I'm using Servlet
based web application and want to have proper logging support. I'm using Log4j
and when I do receive any requests I want to log it with it's whole properties (not everything, just headers and parameters).
For example:
@WebServlet(name = "Login", value = "/Login")
public class Login extends HttpServlet {
final static Logger logger = Logger.getLogger(Login.class);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.info(req); // I want to log request with its
// properties: such as request parameters, headers and so on
// TODO some logic here
}
}
So, how can I mange to override somehow HttpServletRequest
s toString()
method which actually is an interface. Is it good idea to create Wrapper
class for it and can override toString()
method there?
You can implement a Filter like this and you can get all request headers and paramters and you can logged into your file
@Component
public class HttpFilter implements Filter {
private static final Logger LOGGER = LogManager.getLogger(HttpLogging.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
LOGGER.info("logger before method aop FROM FILTER CLASS");
LOGGER.info("content type " + httpServletRequest.getContentType());
LOGGER.info("Local Name" + httpServletRequest.getLocalName());
LOGGER.info("content type " + httpServletRequest.getContentType());
LOGGER.info("request uri " + httpServletRequest.getRequestURI());
LOGGER.info("get Method " + httpServletRequest.getMethod());
LOGGER.info("query string " +httpServletRequest.getQueryString() );
LOGGER.info("server name " + httpServletRequest.getServerName());
LOGGER.info("server port " + httpServletRequest.getServerPort());
LOGGER.info("Parameter names " + httpServletRequest.getParameter("empid"));
System.out.println(request.getContentType());
chain.doFilter(httpServletRequest, httpServletResponse);
}
}
Here's what I've used in the past. It uses SLF4J but the statements should be nearly identical to Log4J.
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RequestLogger {
public static void logRequestAttributes(Logger logger, HttpServletRequest request) {
logger.debug("Attributes:");
for (Enumeration<?> attributeNames = request.getAttributeNames(); attributeNames.hasMoreElements(); ) {
String nextAttributeName = (String) attributeNames.nextElement();
logger.debug("attribute \"" + nextAttributeName + "\" value is \"" + request.getAttribute(nextAttributeName) + "\"");
}
logger.debug("Parameters:");
for (Enumeration<?> parameterNames = request.getParameterNames(); parameterNames.hasMoreElements(); ) {
String nextParameterName = (String) parameterNames.nextElement();
logger.debug("parameter \"" + nextParameterName + "\" value is \"" + request.getParameter(nextParameterName) + "\"");
}
logger.debug("Headers:");
for (Enumeration<?> e = request.getHeaderNames(); e.hasMoreElements(); ) {
String nextHeaderName = (String) e.nextElement();
logger.debug("header name: \"" + nextHeaderName + "\" value is \"" + request.getHeader(nextHeaderName) + "\"");
}
String logStatement = "Server Name: " + request.getServerName() + "\n";
logStatement += "\tServer Port: " + request.getServerPort() + "\n";
logStatement += "\tServlet Path: " + request.getServletPath() + "\n";
logStatement += "\tMethod: " + request.getMethod() + "\n";
logStatement += "\tPath info: " + request.getPathInfo() + "\n";
logStatement += "\tPath translated: " + request.getPathTranslated() + "\n";
logStatement += "\tRequest URI: " + request.getRequestURI() + "\n";
logStatement += "\tRequest URL: " + request.getRequestURL() + "\n";
logStatement += "\tQuery String: " + request.getQueryString() + "\n";
logStatement += "\tContext path: " + request.getContextPath() + "\n";
logger.debug(logStatement);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.