001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.util;
017
018import java.util.Formatter;
019
020/**
021 * Records and logs performance information about an elapsed time period.
022 * 
023 * @author Kuali Rice Team (rice.collab@kuali.org)
024 */
025public class PerformanceLogger {
026
027    private static final org.apache.log4j.Logger LOG =
028        org.apache.log4j.Logger.getLogger(PerformanceLogger.class);
029    private long startTime;
030    private String documentId;
031    
032    public PerformanceLogger() {
033        recordStartTime();
034    }
035    
036    public PerformanceLogger(String documentId) {
037        this();
038        this.documentId = documentId;
039    }
040    
041    private void recordStartTime() {
042        this.startTime = System.currentTimeMillis();
043    }
044    
045    public void log(String message) {
046        log(message, false);
047    }
048
049    public void log(String message, boolean terminalPoint) {
050        if ( LOG.isInfoEnabled() ) {
051                long endTime = System.currentTimeMillis();
052                long totalTime = endTime - startTime;
053            String logMessage = new Formatter().format("Time: %7dms, ", totalTime).toString();
054            if (documentId != null) {
055                    logMessage+="docId="+documentId+", ";
056                }
057                logMessage += message;
058                if (terminalPoint) {
059                    logMessage += "\n";
060                }
061                LOG.info(logMessage);
062        }
063    }
064
065    public void debug(String message) {
066        this.debug(message, false);
067    }
068
069    public void debug(String message, boolean terminalPoint) {
070        if ( LOG.isDebugEnabled() ) {
071            long endTime = System.currentTimeMillis();
072            long totalTime = endTime - startTime;
073            String logMessage = new Formatter().format("Time: %7dms, ", totalTime).toString();
074            if (documentId != null) {
075                logMessage+="docId="+documentId+", ";
076            }
077            logMessage += message;
078            if (terminalPoint) {
079                logMessage += "\n";
080            }
081            LOG.debug(logMessage);
082        }
083    }
084}