Created
July 29, 2014 11:48
-
-
Save beckje01/4893d073cac399228f45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.log4j.MDC | |
class LoggingFilters { | |
public static final String CORRELATION_HEADER = "X-CorrelationId" //Our Standard | |
def filters = { | |
correlationId(controller: '*', action: '*') { //For all requests | |
before = { | |
String correlationId = request.getHeader(CORRELATION_HEADER) | |
if (!correlationId) { | |
correlationId = UUID.randomUUID() | |
log.warn "message=CorrelationId does not exist along the request and hence autogenerated., CorrelationId=${correlationId}" | |
} | |
MDC.put 'correlationId', ", CorrelationId=${correlationId}" | |
//If we still have an uncommited response which we should and no correlation header yet set one. | |
if (!response.isCommitted() && !response.getHeader(CORRELATION_HEADER)) { | |
response.setHeader(CORRELATION_HEADER, correlationId) | |
//This header can be overridden by controller logic if needed | |
} | |
} | |
after = { Map model -> | |
MDC.remove 'correlationId' | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment