KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > logging > LossyTCLogger


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.logging;
6
7 /*
8  * Useful while developement Note:: This class is not synchronized
9  */

10 public class LossyTCLogger implements TCLogger {
11
12   public static final int DEFAULT_LOG_TIME_INTERVAL = 5000; // 5 sec
13
public static final int DEFAULT_LOG_COUNT_INTERVAL = 10000; // 10000 Messages once
14

15   public static final int TIME_BASED = 0;
16   public static final int COUNT_BASED = 1;
17
18   private final TCLogger logger;
19   private LogOrNot decider;
20
21   public LossyTCLogger(TCLogger logger) {
22     this(logger, DEFAULT_LOG_TIME_INTERVAL);
23   }
24
25   public LossyTCLogger(TCLogger logger, int logInterval) {
26     this(logger, logInterval, TIME_BASED);
27   }
28
29   public LossyTCLogger(TCLogger logger, int logInterval, int type) {
30     this.logger = logger;
31     if (type == DEFAULT_LOG_TIME_INTERVAL) {
32       this.decider = new TimeBasedDecider(logInterval);
33     } else {
34       this.decider = new CountBasedDecider(logInterval);
35     }
36   }
37
38   public void debug(Object JavaDoc message) {
39     if (decider.canLog()) {
40       logger.debug(message);
41     }
42   }
43
44   public void debug(Object JavaDoc message, Throwable JavaDoc t) {
45     if (decider.canLog()) {
46       logger.debug(message, t);
47     }
48
49   }
50
51   // XXX:: Maybe errors should always be logged
52
public void error(Object JavaDoc message) {
53     if (decider.canLog()) {
54       logger.error(message);
55     }
56   }
57
58   public void error(Object JavaDoc message, Throwable JavaDoc t) {
59     if (decider.canLog()) {
60       logger.error(message, t);
61     }
62   }
63
64   public void fatal(Object JavaDoc message) {
65     if (decider.canLog()) {
66       logger.fatal(message);
67     }
68   }
69
70   public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
71     if (decider.canLog()) {
72       logger.fatal(message, t);
73     }
74   }
75
76   public void info(Object JavaDoc message) {
77     if (decider.canLog()) {
78       logger.info(message);
79     }
80   }
81
82   public void info(Object JavaDoc message, Throwable JavaDoc t) {
83     if (decider.canLog()) {
84       logger.info(message, t);
85     }
86   }
87
88   public void warn(Object JavaDoc message) {
89     if (decider.canLog()) {
90       logger.warn(message);
91     }
92   }
93
94   public void warn(Object JavaDoc message, Throwable JavaDoc t) {
95     if (decider.canLog()) {
96       logger.warn(message, t);
97     }
98   }
99
100   public void log(LogLevel level, Object JavaDoc message) {
101     if (decider.canLog()) {
102       logger.log(level, message);
103     }
104   }
105
106   public void log(LogLevel level, Object JavaDoc message, Throwable JavaDoc t) {
107     if (decider.canLog()) {
108       logger.log(level, message, t);
109     }
110   }
111
112   public boolean isDebugEnabled() {
113     return logger.isDebugEnabled();
114   }
115
116   public boolean isInfoEnabled() {
117     return logger.isInfoEnabled();
118   }
119
120   public void setLevel(LogLevel level) {
121     logger.setLevel(level);
122   }
123
124   public LogLevel getLevel() {
125     return logger.getLevel();
126   }
127
128   public String JavaDoc getName() {
129     return logger.getName();
130   }
131
132   interface LogOrNot {
133     boolean canLog();
134   }
135
136   static class TimeBasedDecider implements LogOrNot {
137     private int logInterval;
138     private long then;
139
140     TimeBasedDecider(int logInterval) {
141       this.logInterval = logInterval;
142     }
143
144     public boolean canLog() {
145       long now = System.currentTimeMillis();
146       if (now > (then + logInterval)) {
147         then = now;
148         return true;
149       }
150       return false;
151     }
152   }
153
154   static class CountBasedDecider implements LogOrNot {
155     private int logInterval;
156     private int count;
157
158     CountBasedDecider(int logInterval) {
159       this.logInterval = logInterval;
160     }
161
162     public boolean canLog() {
163       if (++count >= logInterval) {
164         count = 0;
165         return true;
166       }
167       return false;
168     }
169   }
170
171 }
172
Popular Tags