Log Level for HTTP Requests
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
I treat HTTP traffic as structured access logging rather than forcing every request into INFO. A useful event has the method, route template, response status, duration, and correlation ID. The route template avoids high-cardinality paths such as one unique URL per order. Whether all successful requests are retained depends on traffic, audit rules, cost, and whether the gateway already records them; a busy service may sample successes but retain errors and slow requests. I classify failures by meaning—a normal 404 or validation response is not automatically an ERROR. I also redact Authorization and Cookie headers, tokens, passwords, sensitive query values, and request or response bodies unless an approved, minimized field is specifically required.
Log Level for HTTP Requests
Direct Answer
There is no universal rule that every HTTP request belongs at INFO. Use a dedicated access-log or structured request event according to traffic, audit, privacy, and cost needs; high-volume success traffic may be sampled or kept at a lower level. Record failures at a level that reflects their meaning, and never log credentials, tokens, sensitive headers, or unredacted bodies.
Code Example
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
public class RequestLogInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(RequestLogInterceptor.class);
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) {
req.setAttribute("start", System.currentTimeMillis());
return true;
}
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object h, Exception e) {
long ms = System.currentTimeMillis() - (long) req.getAttribute("start");
// Metadata only — never the body or auth header
log.info("{} {} -> {} ({} ms)", req.getMethod(), req.getRequestURI(), res.getStatus(), ms);
}
}
Common Mistakes & Pitfalls
- Requests at ERROR: treating normal traffic as errors, making ERROR meaningless and paging people for routine load.
- DEBUG left on in prod: enabling
logging.level.org.springframework.web=DEBUGto chase one bug and forgetting it, dumping every header and param — including secrets. - No redaction: logging whole request objects with
passwordorcreditCardfields, a security and compliance violation.
Check your understanding
Answer these without looking back at the article.
- How do you configure log levels per package in Spring Boot?
- How would you log request and response bodies without exposing sensitive data?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.