001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.logging;
006:
007: /*
008: * Useful while developement Note:: This class is not synchronized
009: */
010: public class LossyTCLogger implements TCLogger {
011:
012: public static final int DEFAULT_LOG_TIME_INTERVAL = 5000; // 5 sec
013: public static final int DEFAULT_LOG_COUNT_INTERVAL = 10000; // 10000 Messages once
014:
015: public static final int TIME_BASED = 0;
016: public static final int COUNT_BASED = 1;
017:
018: private final TCLogger logger;
019: private LogOrNot decider;
020:
021: public LossyTCLogger(TCLogger logger) {
022: this (logger, DEFAULT_LOG_TIME_INTERVAL);
023: }
024:
025: public LossyTCLogger(TCLogger logger, int logInterval) {
026: this (logger, logInterval, TIME_BASED);
027: }
028:
029: public LossyTCLogger(TCLogger logger, int logInterval, int type) {
030: this .logger = logger;
031: if (type == DEFAULT_LOG_TIME_INTERVAL) {
032: this .decider = new TimeBasedDecider(logInterval);
033: } else {
034: this .decider = new CountBasedDecider(logInterval);
035: }
036: }
037:
038: public void debug(Object message) {
039: if (decider.canLog()) {
040: logger.debug(message);
041: }
042: }
043:
044: public void debug(Object message, Throwable t) {
045: if (decider.canLog()) {
046: logger.debug(message, t);
047: }
048:
049: }
050:
051: // XXX:: Maybe errors should always be logged
052: public void error(Object message) {
053: if (decider.canLog()) {
054: logger.error(message);
055: }
056: }
057:
058: public void error(Object message, Throwable t) {
059: if (decider.canLog()) {
060: logger.error(message, t);
061: }
062: }
063:
064: public void fatal(Object message) {
065: if (decider.canLog()) {
066: logger.fatal(message);
067: }
068: }
069:
070: public void fatal(Object message, Throwable t) {
071: if (decider.canLog()) {
072: logger.fatal(message, t);
073: }
074: }
075:
076: public void info(Object message) {
077: if (decider.canLog()) {
078: logger.info(message);
079: }
080: }
081:
082: public void info(Object message, Throwable t) {
083: if (decider.canLog()) {
084: logger.info(message, t);
085: }
086: }
087:
088: public void warn(Object message) {
089: if (decider.canLog()) {
090: logger.warn(message);
091: }
092: }
093:
094: public void warn(Object message, Throwable t) {
095: if (decider.canLog()) {
096: logger.warn(message, t);
097: }
098: }
099:
100: public void log(LogLevel level, Object message) {
101: if (decider.canLog()) {
102: logger.log(level, message);
103: }
104: }
105:
106: public void log(LogLevel level, Object message, Throwable 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 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: }
|