View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ken.util;
17  
18  import org.apache.commons.lang.time.DurationFormatUtils;
19  import org.apache.commons.lang.time.StopWatch;
20  import org.apache.log4j.Logger;
21  
22  /**
23   * Wrapper for the Log4J performance log
24   * @author Kuali Rice Team (rice.collab@kuali.org)
25   */
26  public final class PerformanceLog {
27      private static final Logger LOG = Logger.getLogger("Performance");
28  
29      /**
30       * This class 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32       */
33      public static final class PerformanceStopWatch {
34          private StopWatch stopWatch = new StopWatch();
35          private String message;
36          
37          /**
38           * Constructs a PerformanceLog.java.
39           * @param message
40           */
41          public PerformanceStopWatch(String message) {
42              this.message = message;
43              stopWatch.start();
44          }
45  
46          /**
47           * This method records the duration of how long a message delivery takes.
48           */
49          public void recordDuration() {
50              logDuration(message, stopWatch.getTime());
51          }
52      }
53  
54      /**
55       * This method returns an instance of the logger object.
56       * @return Logger
57       */
58      public static Logger getInstance() {
59          return LOG;
60      }
61  
62      /**
63       * This method returns a new stop watch instance.
64       * @param message
65       * @return PerformanceStopWatch
66       */
67      public static PerformanceStopWatch startTimer(String message) {
68          return new PerformanceStopWatch(message);
69      }
70      
71      /**
72       * This method logs the duration information for a given message.
73       * @param message
74       * @param duration
75       */
76      public static void logDuration(String message, long duration) {
77          LOG.info(message + ": " + DurationFormatUtils.formatDurationHMS(duration) + " (" + duration+ " ms)");
78      }
79  }