我正在使用基于Servlet
的Web应用程序,并希望获得适当的日志记录支持。我正在使用Log4j
,当我收到任何请求时,都希望使用它的全部属性(不是所有内容,仅是标题和参数)来记录它。
例如:
@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
}
}
因此,我该如何管理以某种方式覆盖HttpServletRequest
的{{1}}方法,该方法实际上是一个接口。为它创建toString()
类并可以在那里覆盖Wrapper
方法是个好主意吗?
答案 0 :(得分:1)
您可以实现这样的过滤器,并且可以获得所有请求标头和参数 您可以登录文件
@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);
}
}
答案 1 :(得分:0)
这是我过去使用的内容。它使用SLF4J,但语句应与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);
}
}