Skip to content

Instantly share code, notes, and snippets.

@beckje01
Created July 29, 2014 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beckje01/4893d073cac399228f45 to your computer and use it in GitHub Desktop.
Save beckje01/4893d073cac399228f45 to your computer and use it in GitHub Desktop.
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