package org.zalando.nakadi.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
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 java.io.IOException;
import java.security.Principal;
import java.util.Optional;

@Component
public class LoggingFilter extends OncePerRequestFilter {

    private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class);

    @Override
    protected void doFilterInternal(final HttpServletRequest request,
                                    final HttpServletResponse response, final FilterChain filterChain)
            throws ServletException, IOException {
        final long start = System.currentTimeMillis();
        final String userAgent = Optional.ofNullable(request.getHeader("User-Agent")).orElse("-");
        final String user = Optional.ofNullable(request.getUserPrincipal()).map(Principal::getName).orElse("-");
        final String method = request.getMethod();
        final String path = request.getRequestURI();
        LOG.info("{} \"{}\" \"{}\" \"{}\"",
                method,
                path,
                userAgent,
                user);
        //execute request
        try {
            filterChain.doFilter(request, response);
        } finally {
            final long time = System.currentTimeMillis();
            final Long timing = time - start;
            LOG.info("{} \"{}\" \"{}\" \"{}\" {} {} ms",
                    method,
                    path,
                    userAgent,
                    user,
                    response.getStatus(),
                    timing);
        }
    }
}

@@ -1225,32 +1225,51 @@
       LOG.info(%22
+%5BACCESS_LOG_BEGIN%5D 
 %7B%7D %5C%22%7B%7D%5C%22 %5C%22%7B%7D%5C%22
@@ -1613,16 +1613,33 @@
 G.info(%22
+%5BACCESS_LOG_END%5D 
 %7B%7D %5C%22%7B%7D%5C

add start and end tags