1 /* 2 * Copyright (c) 2002-2007 by OpenSymphony 3 * All rights reserved. 4 */ 5 package com.opensymphony.xwork2.interceptor; 6 7 import com.opensymphony.xwork2.ActionInvocation; 8 import com.opensymphony.xwork2.util.logging.Logger; 9 import com.opensymphony.xwork2.util.logging.LoggerFactory; 10 11 12 /** 13 * <!-- START SNIPPET: description --> 14 * This interceptor logs the start and end of the execution an action (in English-only, not internationalized). 15 * <br/> 16 * <b>Note:</b>: This interceptor will log at <tt>INFO</tt> level. 17 * <p/> 18 * <!-- END SNIPPET: description --> 19 * 20 * <!-- START SNIPPET: parameters --> 21 * There are no parameters for this interceptor. 22 * <!-- END SNIPPET: parameters --> 23 * 24 * <!-- START SNIPPET: extending --> 25 * There are no obvious extensions to the existing interceptor. 26 * <!-- END SNIPPET: extending --> 27 * 28 * <pre> 29 * <!-- START SNIPPET: example --> 30 * <!-- prints out a message before and after the immediate action execution --> 31 * <action name="someAction" class="com.examples.SomeAction"> 32 * <interceptor-ref name="completeStack"/> 33 * <interceptor-ref name="logger"/> 34 * <result name="success">good_result.ftl</result> 35 * </action> 36 * 37 * <!-- prints out a message before any more interceptors continue and after they have finished --> 38 * <action name="someAction" class="com.examples.SomeAction"> 39 * <interceptor-ref name="logger"/> 40 * <interceptor-ref name="completeStack"/> 41 * <result name="success">good_result.ftl</result> 42 * </action> 43 * <!-- END SNIPPET: example --> 44 * </pre> 45 * 46 * @author Jason Carreira 47 */ 48 public class LoggingInterceptor extends AbstractInterceptor { 49 private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class); 50 private static final String FINISH_MESSAGE = "Finishing execution stack for action "; 51 private static final String START_MESSAGE = "Starting execution stack for action "; 52 53 @Override 54 public String intercept(ActionInvocation invocation) throws Exception { 55 logMessage(invocation, START_MESSAGE); 56 String result = invocation.invoke(); 57 logMessage(invocation, FINISH_MESSAGE); 58 return result; 59 } 60 61 private void logMessage(ActionInvocation invocation, String baseMessage) { 62 if (LOG.isInfoEnabled()) { 63 StringBuilder message = new StringBuilder(baseMessage); 64 String namespace = invocation.getProxy().getNamespace(); 65 66 if ((namespace != null) && (namespace.trim().length() > 0)) { 67 message.append(namespace).append("/"); 68 } 69 70 message.append(invocation.getProxy().getActionName()); 71 LOG.info(message.toString()); 72 } 73 } 74 75 }