Share

August 8, 2010

Add Spring Webflow flowExecutionKey to your logs

When using spring webflow you may want to mark your log lines with which flow session they belong to. When you need to debug just "grep" for a key to filter the logs.
/**
 * customize the appender so that we can include FlowExceution Key in every log statement. This extends the
 * {@link DailyRollingFileAppender}
 *  * 
 */
public class CustomAppender extends DailyRollingFileAppender {

    /**
     * after the appender writes to the Stream using the layout defined, we want to append FlowExecution key. Hence call
     * super and then using the same Writer, add the flow execution key.
     * 
     */
    @Override
    protected void subAppend(LoggingEvent event) {
        // call base class's subAppend
        super.subAppend(event);

        // fetch the request context
        RequestContext context = RequestContextHolder.getRequestContext();
        if (context != null) { // if available
            String flowExecKey = context.getRequestParameters().get("_flowExecutionKey");
            if (StringUtils.hasText(flowExecKey)) {
                qw.write(" | " + flowExecKey);
            }
        }
        // carriage return
        qw.write(Layout.LINE_SEP);
    }
}