dk.kvalitetsit.logging.RequestIdGeneratorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-request-id-logger Show documentation
Show all versions of spring-request-id-logger Show documentation
Adds correlation id to all log lines within the same request in a Spring Boot Application.
The newest version!
package dk.kvalitetsit.logging;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
public class RequestIdGeneratorImpl implements RequestIdGenerator {
private final static Logger logger = LoggerFactory.getLogger(RequestIdGeneratorImpl.class);
private final String headerName;
private final HttpServletRequest httpServletRequest;
private String cachedCorrelationId;
public RequestIdGeneratorImpl(String headerName, HttpServletRequest httpServletRequest) {
this.headerName = headerName;
this.httpServletRequest = httpServletRequest;
}
@Override
public String getOrGenerateRequestId() {
if(cachedCorrelationId == null) {
String correlationId = httpServletRequest.getHeader(headerName);
logger.debug("Extracted header: {} with value:{}", headerName, correlationId);
if(correlationId == null) {
correlationId = UUID.randomUUID().toString();
logger.debug("Generated new correlation id as it was not found in header: {}.", correlationId);
}
cachedCorrelationId = correlationId;
}
return cachedCorrelationId;
}
}