Java tutorial
/* * spring-mvc-logger logs requests/responses * * Copyright (c) 2013. Israel Zalmanov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.isrsal.logging; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; public class LoggingFilter extends OncePerRequestFilter { protected static final Logger logger = LoggerFactory.getLogger(LoggingFilter.class); private static final String REQUEST_PREFIX = "Request: "; private static final String RESPONSE_PREFIX = "Response: "; private AtomicLong id = new AtomicLong(1); @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException { if (logger.isDebugEnabled()) { long requestId = id.incrementAndGet(); request = new RequestWrapper(requestId, request); response = new ResponseWrapper(requestId, response); } try { filterChain.doFilter(request, response); } finally { if (logger.isDebugEnabled()) { logRequest(request); logResponse((ResponseWrapper) response); } } } private void logRequest(final HttpServletRequest request) { StringBuilder msg = new StringBuilder(); msg.append(REQUEST_PREFIX); if (request instanceof RequestWrapper) { msg.append("request id=").append(((RequestWrapper) request).getId()).append("; "); } HttpSession session = request.getSession(false); if (session != null) { msg.append("session id=").append(session.getId()).append("; "); } if (request.getMethod() != null) { msg.append("method=").append(request.getMethod()).append("; "); } if (request.getContentType() != null) { msg.append("content type=").append(request.getContentType()).append("; "); } msg.append("uri=").append(request.getRequestURI()); if (request.getQueryString() != null) { msg.append('?').append(request.getQueryString()); } if (request instanceof RequestWrapper && !isMultipart(request) && !isBinaryContent(request)) { RequestWrapper requestWrapper = (RequestWrapper) request; try { String charEncoding = requestWrapper.getCharacterEncoding() != null ? requestWrapper.getCharacterEncoding() : "UTF-8"; msg.append("; payload=").append(new String(requestWrapper.toByteArray(), charEncoding)); } catch (UnsupportedEncodingException e) { logger.warn("Failed to parse request payload", e); } if (!requestWrapper.getParameterMap().isEmpty()) { msg.append("; parameters="); Map<String, String[]> parameterMap = requestWrapper.getParameterMap(); for (String key : parameterMap.keySet()) { msg.append(key + ":" + Arrays.toString(parameterMap.get(key))).append(";"); } } } logger.debug(msg.toString()); } private boolean isBinaryContent(final HttpServletRequest request) { if (request.getContentType() == null) { return false; } return request.getContentType().startsWith("image") || request.getContentType().startsWith("video") || request.getContentType().startsWith("audio"); } private boolean isMultipart(final HttpServletRequest request) { return request.getContentType() != null && request.getContentType().startsWith("multipart/form-data"); } private void logResponse(final ResponseWrapper response) { StringBuilder msg = new StringBuilder(); msg.append(RESPONSE_PREFIX); msg.append("request id=").append((response.getId())); try { msg.append("; payload=").append(new String(response.toByteArray(), response.getCharacterEncoding())); } catch (UnsupportedEncodingException e) { logger.warn("Failed to parse response payload", e); } logger.debug(msg.toString()); } }