01: /**
02: * Copyright (C) 2007 NetMind Consulting Bt.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 3 of the License, or (at your option) any later version.
08: *
09: * 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: */package hu.netmind.persistence;
18:
19: import org.apache.log4j.Logger;
20: import java.util.*;
21:
22: /**
23: * This profile logger logs events in given intervals.
24: * @author Brautigam Robert
25: * @version CVS Revision: $Revision$
26: */
27: public class ProfileLogger {
28: private static Logger logger = Logger
29: .getLogger(ProfileLogger.class);
30: private static ProfileLogger instance = null;
31: private static int PROFILE_INTERVAL = 5000;
32:
33: private long lastOutputTime = 0;
34: private Map eventEntries;
35:
36: private ProfileLogger() {
37: eventEntries = Collections.synchronizedMap(new HashMap());
38: }
39:
40: public static ProfileLogger getLogger() {
41: return instance;
42: }
43:
44: /**
45: * Log an event.
46: * @param category The category identifier of the event.
47: * @param message The message of given type.
48: */
49: public void profile(String category, String message) {
50: // If not enabled, do nothing
51: if (!logger.isDebugEnabled())
52: return;
53: // Adjust count
54: ProfileEntry entry = (ProfileEntry) eventEntries.get(category);
55: if (entry == null) {
56: entry = new ProfileEntry();
57: eventEntries.put(category, entry);
58: }
59: entry.count++;
60: // Decide what to do
61: long currentTime = System.currentTimeMillis();
62: if (currentTime > PROFILE_INTERVAL + entry.lastOutputTime) {
63: // Time's up, output now
64: logger.debug("[" + category + ":" + entry.count + "] "
65: + message);
66: entry.lastOutputTime = currentTime;
67: // Clear entry
68: entry.count = 0;
69: }
70: }
71:
72: private static class ProfileEntry {
73: public long lastOutputTime;
74: public int count;
75:
76: public ProfileEntry() {
77: lastOutputTime = 0;
78: count = 0;
79: }
80: }
81:
82: static {
83: instance = new ProfileLogger();
84: try {
85: ResourceBundle config = ResourceBundle
86: .getBundle("beankeeper");
87: PROFILE_INTERVAL = Integer.valueOf(
88: config.getString("profile.interval")).intValue();
89: } catch (Exception e) {
90: logger
91: .error(
92: "could not read configuration file, using hardcoded defaults.",
93: e);
94: }
95: }
96: }
|