View Javadoc

1   /**
2    * Copyright 2005-2014 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.kew.util;
17  
18  import java.util.Formatter;
19  
20  /**
21   * Records and logs performance information about an elapsed time period.
22   * 
23   * @author Kuali Rice Team (rice.collab@kuali.org)
24   */
25  public class PerformanceLogger {
26  
27      private static final org.apache.log4j.Logger LOG =
28          org.apache.log4j.Logger.getLogger(PerformanceLogger.class);
29      private long startTime;
30      private String documentId;
31      
32      public PerformanceLogger() {
33          recordStartTime();
34      }
35      
36      public PerformanceLogger(String documentId) {
37          this();
38          this.documentId = documentId;
39      }
40      
41      private void recordStartTime() {
42          this.startTime = System.currentTimeMillis();
43      }
44      
45      public void log(String message) {
46          log(message, false);
47      }
48  
49      public void log(String message, boolean terminalPoint) {
50      	if ( LOG.isInfoEnabled() ) {
51  	        long endTime = System.currentTimeMillis();
52  	        long totalTime = endTime - startTime;
53              String logMessage = new Formatter().format("Time: %7dms, ", totalTime).toString();
54              if (documentId != null) {
55  	            logMessage+="docId="+documentId+", ";
56  	        }
57  	        logMessage += message;
58  	        if (terminalPoint) {
59  	            logMessage += "\n";
60  	        }
61  	        LOG.info(logMessage);
62      	}
63      }
64  
65      public void debug(String message) {
66          this.debug(message, false);
67      }
68  
69      public void debug(String message, boolean terminalPoint) {
70          if ( LOG.isDebugEnabled() ) {
71              long endTime = System.currentTimeMillis();
72              long totalTime = endTime - startTime;
73              String logMessage = new Formatter().format("Time: %7dms, ", totalTime).toString();
74              if (documentId != null) {
75                  logMessage+="docId="+documentId+", ";
76              }
77              logMessage += message;
78              if (terminalPoint) {
79                  logMessage += "\n";
80              }
81              LOG.debug(logMessage);
82          }
83      }
84  }