|
| 1 | +package io.oasp.module.logging.common.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +import javax.servlet.Filter; |
| 7 | +import javax.servlet.FilterChain; |
| 8 | +import javax.servlet.FilterConfig; |
| 9 | +import javax.servlet.ServletException; |
| 10 | +import javax.servlet.ServletRequest; |
| 11 | +import javax.servlet.ServletResponse; |
| 12 | +import javax.servlet.http.HttpServletRequest; |
| 13 | +import javax.servlet.http.HttpServletResponse; |
| 14 | + |
| 15 | +import org.apache.http.HttpStatus; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * Request logging filter that measures the execution time of a request. |
| 22 | + * |
| 23 | + * @author trippl |
| 24 | + * @since 1.5.0 |
| 25 | + */ |
| 26 | +public class PerformanceLogFilter implements Filter { |
| 27 | + |
| 28 | + private static final Logger LOG = LoggerFactory.getLogger(PerformanceLogFilter.class); |
| 29 | + |
| 30 | + /** |
| 31 | + * Optional filter to only measure execution time of requests that match the filter. |
| 32 | + */ |
| 33 | + private String urlFilter; |
| 34 | + |
| 35 | + /** |
| 36 | + * The constructor. |
| 37 | + */ |
| 38 | + public PerformanceLogFilter() { |
| 39 | + |
| 40 | + super(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void init(FilterConfig config) throws ServletException { |
| 45 | + |
| 46 | + this.urlFilter = null; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, |
| 51 | + ServletException { |
| 52 | + |
| 53 | + long startTime; |
| 54 | + String path = ((HttpServletRequest) request).getServletPath(); |
| 55 | + String url = ((HttpServletRequest) request).getRequestURL().toString(); |
| 56 | + |
| 57 | + if (this.urlFilter == null || path.matches(this.urlFilter)) { |
| 58 | + startTime = System.nanoTime(); |
| 59 | + try { |
| 60 | + chain.doFilter(request, response); |
| 61 | + logPerformance(response, startTime, url, null); |
| 62 | + } catch (Throwable error) { |
| 63 | + logPerformance(response, startTime, url, error); |
| 64 | + } |
| 65 | + } else { |
| 66 | + chain.doFilter(request, response); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Logs the request URL, execution time and {@link HttpStatus}. In case of an error also logs class name and error |
| 72 | + * message. |
| 73 | + * |
| 74 | + * @param response - the {@link ServletResponse} |
| 75 | + * @param startTime - start time of the {@link #doFilter(ServletRequest, ServletResponse, FilterChain)} function |
| 76 | + * @param url - requested URL |
| 77 | + * @param error - error thrown by the requested servlet, {@code null} if execution did not cause an error |
| 78 | + */ |
| 79 | + private void logPerformance(ServletResponse response, long startTime, String url, Throwable error) { |
| 80 | + |
| 81 | + long endTime, duration; |
| 82 | + int statusCode = ((HttpServletResponse) response).getStatus(); |
| 83 | + endTime = System.nanoTime(); |
| 84 | + duration = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS); |
| 85 | + |
| 86 | + String errorClass = ""; |
| 87 | + String errorMessage = ""; |
| 88 | + if (error != null) { |
| 89 | + statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; |
| 90 | + errorClass = error.getClass().getName(); |
| 91 | + errorMessage = error.getMessage(); |
| 92 | + } |
| 93 | + String message = |
| 94 | + createMessage(url, Long.toString(duration), Integer.toString(statusCode), errorClass, errorMessage); |
| 95 | + LOG.info(message); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns a {@link String} representing the log message, which contains the given arguments separated by ';' |
| 100 | + * |
| 101 | + * @param args - the arguments for the log message |
| 102 | + * @return a {@link String} representing the log message |
| 103 | + */ |
| 104 | + private String createMessage(String... args) { |
| 105 | + |
| 106 | + return String.join(";", args); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void destroy() { |
| 111 | + |
| 112 | + // nothing to do... |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments