| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.logging.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.logging.SnapshotLogger; |
| 22 | import org.apache.log4j.Logger; |
| 23 | import java.util.*; |
| 24 | |
| 25 | /** |
| 26 | * This implementation uses configuration to get the output interval of |
| 27 | * categories, and uses apache logger for output. |
| 28 | * @author Brautigam Robert |
| 29 | * @version CVS Revision: $Revision$ |
| 30 | */ |
| 31 | public class SnapshotLoggerImpl implements SnapshotLogger |
| 32 | { |
| 33 | private static Logger logger = Logger.getLogger(SnapshotLogger.class); |
| 34 | private static int PROFILE_INTERVAL = 5000; |
| 35 | |
| 36 | private long lastOutputTime = 0; |
| 37 | private Map eventEntries; |
| 38 | |
| 39 | public void init(Map parameters) |
| 40 | { |
| 41 | eventEntries = Collections.synchronizedMap(new HashMap()); |
| 42 | try |
| 43 | { |
| 44 | ResourceBundle config = ResourceBundle.getBundle("beankeeper"); |
| 45 | PROFILE_INTERVAL = Integer.valueOf(config.getString("beankeeper.profile.interval")).intValue(); |
| 46 | } catch ( Exception e ) { |
| 47 | logger.error("could not read configuration file, using hardcoded defaults.",e); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public void release() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Log an event. |
| 57 | * @param category The category identifier of the event. |
| 58 | * @param message The message of given type. |
| 59 | */ |
| 60 | public void log(String category,String message) |
| 61 | { |
| 62 | // If not enabled, do nothing |
| 63 | if ( ! logger.isDebugEnabled() ) |
| 64 | return; |
| 65 | // Adjust count |
| 66 | ProfileEntry entry = (ProfileEntry) eventEntries.get(category); |
| 67 | if ( entry == null ) |
| 68 | { |
| 69 | entry = new ProfileEntry(); |
| 70 | eventEntries.put(category,entry); |
| 71 | } |
| 72 | entry.count++; |
| 73 | // Decide what to do |
| 74 | long currentTime = System.currentTimeMillis(); |
| 75 | if ( currentTime > PROFILE_INTERVAL + entry.lastOutputTime ) |
| 76 | { |
| 77 | // Time's up, output now |
| 78 | logger.debug("["+category+":"+entry.count+"] "+message); |
| 79 | entry.lastOutputTime = currentTime; |
| 80 | // Clear entry |
| 81 | entry.count=0; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private static class ProfileEntry |
| 86 | { |
| 87 | public long lastOutputTime; |
| 88 | public int count; |
| 89 | |
| 90 | public ProfileEntry() |
| 91 | { |
| 92 | lastOutputTime = 0; |
| 93 | count = 0; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |