1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.context.support;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.context.ApplicationEvent;
  20. import org.springframework.context.ApplicationListener;
  21. import org.springframework.util.ResponseTimeMonitorImpl;
  22. /**
  23. * Listener that logs the response times of web requests.
  24. * To be registered in a WebApplicationContext.
  25. * @author Rod Johnson
  26. * @since January 21, 2001
  27. * @see RequestHandledEvent
  28. */
  29. public class PerformanceMonitorListener implements ApplicationListener {
  30. protected final Log logger = LogFactory.getLog(getClass());
  31. protected final ResponseTimeMonitorImpl responseTimeMonitor = new ResponseTimeMonitorImpl();
  32. public void onApplicationEvent(ApplicationEvent event) {
  33. if (event instanceof RequestHandledEvent) {
  34. RequestHandledEvent rhe = (RequestHandledEvent) event;
  35. // Could use one monitor per URL
  36. this.responseTimeMonitor.recordResponseTime(rhe.getTimeMillis());
  37. if (logger.isInfoEnabled()) {
  38. // Stringifying objects is expensive. Don't do it unless it will show.
  39. logger.info("PerformanceMonitorListener: last=" + rhe.getTimeMillis() + "ms; " +
  40. this.responseTimeMonitor + "; client was " + rhe.getIpAddress());
  41. }
  42. }
  43. }
  44. }