001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.util;
017    
018    /**
019     * Records and logs performance information about an elapsed time period.
020     * 
021     * @author Kuali Rice Team (rice.collab@kuali.org)
022     */
023    public class PerformanceLogger {
024    
025        private static final org.apache.log4j.Logger LOG =
026            org.apache.log4j.Logger.getLogger(PerformanceLogger.class);
027        private long startTime;
028        private String documentId;
029        
030        public PerformanceLogger() {
031            recordStartTime();
032        }
033        
034        public PerformanceLogger(String documentId) {
035            this();
036            this.documentId = documentId;
037        }
038        
039        private void recordStartTime() {
040            this.startTime = System.currentTimeMillis();
041        }
042        
043        public void log(String message) {
044            log(message, false);
045        }
046    
047        public void log(String message, boolean terminalPoint) {
048            if ( LOG.isInfoEnabled() ) {
049                    long endTime = System.currentTimeMillis();
050                    long totalTime = endTime - startTime;
051                    String logMessage = "Time: "+totalTime+" ms, ";
052                    if (documentId != null) {
053                        logMessage+="docId="+documentId+", ";
054                    }
055                    logMessage += message;
056                    if (terminalPoint) {
057                        logMessage += "\n";
058                    }
059                    LOG.info(logMessage);
060            }
061        }
062        
063    }