Spring has its own Expression Language as of version 3.0

Recently I have switched from the log4j logging framework to the logback framework. logback seems to have evolved into something better than log4j. It has cool features like the ability of logging into context specific logging files almost out-of-the-box (Using a sifting appender).
However the application has to provide the context (usually as MDC variables) which is kind of boring task to do.

So, I have implemented a fairly generic servlet that is able to provide almost any servlet request information for an active HTTP request. This servlet accepts a set of SpEL expressions (SpEL will be available with Spring 3.0) which are parsed and evaluated.
Finally it produces the evaluation results as MDC variables through the org.slf4j.MDC api (So I guess that might be usable for log4j as well).


	slf4jmdc
	at...SpringExpressionSlf4jMdcRequestFilter
	
mdcKeyExpression
contextPath=${request.contextPath};username=${request.userPrincipal?.name}
	

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
	StandardEvaluationContext context = new StandardEvaluationContext(new RootObject(request));
	ExpressionParser parser = new SpelExpressionParser();

	String[] mdc = StringUtils.delimitedListToStringArray(mdcKeyExpression, ";");
	Properties expressions = StringUtils.splitArrayElementsIntoProperties(mdc, "=");

	Enumeration keyNames = expressions.propertyNames();
	while (keyNames.hasMoreElements()) {
		String key = keyNames.nextElement().toString();
		try {
			Expression exp = parser.parseExpression(expressions.getProperty(key), new TemplateParserContext());
			Object value = exp.getValue(context);
			if (value != null) {
				MDC.put(key, value.toString());
			}
		} catch (ParseException e) {
			LOGGER.error("Parsing expression", e);
		}
	}

	try {
		filterChain.doFilter(request, response);
	} finally {
		while (keyNames.hasMoreElements()) {
			MDC.remove(keyNames.nextElement().toString());
		}
	}
}

This servlet can be used about anywhere MDC variables are needed!

Leave a Reply

green red blue grey