1 5 package com.tc.logging; 6 7 10 public class LossyTCLogger implements TCLogger { 11 12 public static final int DEFAULT_LOG_TIME_INTERVAL = 5000; public static final int DEFAULT_LOG_COUNT_INTERVAL = 10000; 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 message) { 39 if (decider.canLog()) { 40 logger.debug(message); 41 } 42 } 43 44 public void debug(Object message, Throwable t) { 45 if (decider.canLog()) { 46 logger.debug(message, t); 47 } 48 49 } 50 51 public void error(Object message) { 53 if (decider.canLog()) { 54 logger.error(message); 55 } 56 } 57 58 public void error(Object message, Throwable t) { 59 if (decider.canLog()) { 60 logger.error(message, t); 61 } 62 } 63 64 public void fatal(Object message) { 65 if (decider.canLog()) { 66 logger.fatal(message); 67 } 68 } 69 70 public void fatal(Object message, Throwable t) { 71 if (decider.canLog()) { 72 logger.fatal(message, t); 73 } 74 } 75 76 public void info(Object message) { 77 if (decider.canLog()) { 78 logger.info(message); 79 } 80 } 81 82 public void info(Object message, Throwable t) { 83 if (decider.canLog()) { 84 logger.info(message, t); 85 } 86 } 87 88 public void warn(Object message) { 89 if (decider.canLog()) { 90 logger.warn(message); 91 } 92 } 93 94 public void warn(Object message, Throwable t) { 95 if (decider.canLog()) { 96 logger.warn(message, t); 97 } 98 } 99 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 } 172 | Popular Tags |