001    /*
002     * Copyright 2007-2008 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.ken.util;
017    
018    import org.apache.commons.lang.time.DurationFormatUtils;
019    import org.apache.commons.lang.time.StopWatch;
020    import org.apache.log4j.Logger;
021    
022    /**
023     * Wrapper for the Log4J performance log
024     * @author Kuali Rice Team (rice.collab@kuali.org)
025     */
026    public final class PerformanceLog {
027        private static final Logger LOG = Logger.getLogger("Performance");
028    
029        /**
030         * This class 
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032         */
033        public static final class PerformanceStopWatch {
034            private StopWatch stopWatch = new StopWatch();
035            private String message;
036            
037            /**
038             * Constructs a PerformanceLog.java.
039             * @param message
040             */
041            public PerformanceStopWatch(String message) {
042                this.message = message;
043                stopWatch.start();
044            }
045    
046            /**
047             * This method records the duration of how long a message delivery takes.
048             */
049            public void recordDuration() {
050                logDuration(message, stopWatch.getTime());
051            }
052        }
053    
054        /**
055         * This method returns an instance of the logger object.
056         * @return Logger
057         */
058        public static Logger getInstance() {
059            return LOG;
060        }
061    
062        /**
063         * This method returns a new stop watch instance.
064         * @param message
065         * @return PerformanceStopWatch
066         */
067        public static PerformanceStopWatch startTimer(String message) {
068            return new PerformanceStopWatch(message);
069        }
070        
071        /**
072         * This method logs the duration information for a given message.
073         * @param message
074         * @param duration
075         */
076        public static void logDuration(String message, long duration) {
077            LOG.info(message + ": " + DurationFormatUtils.formatDurationHMS(duration) + " (" + duration+ " ms)");
078        }
079    }