1 23 24 package log4j; 25 26 import org.apache.log4j.Category; 27 import org.apache.log4j.Level; 28 import org.apache.slide.util.logger.Logger; 29 30 34 public class Log4jLogger implements Logger { 35 36 37 39 40 41 42 44 45 static { 46 } 48 49 50 52 53 58 public void setLoggerLevel(String channel, int loggerLevel) { 59 Level level = toLevel(loggerLevel); 60 Category cat = getCategory(channel); 61 cat.setLevel(level); 62 } 63 64 69 public void setLoggerLevel(int loggerLevel) { 70 setLoggerLevel(DEFAULT_CHANNEL, loggerLevel); 71 } 72 73 74 79 public int getLoggerLevel(String channel) { 80 Category cat = getCategory(channel); 81 return fromLevel(cat.getLevel()); 82 } 83 84 85 90 public int getLoggerLevel() { 91 return getLoggerLevel(DEFAULT_CHANNEL); 92 } 93 94 95 96 97 99 100 108 public void log(Object data, Throwable throwable, String channel, int level) { 109 Category cat = getCategory(channel); 110 cat.log(toLevel(level), data, throwable); 111 } 112 113 114 121 public void log(Object data, String channel, int level) { 122 Category cat = getCategory(channel); 123 if (data instanceof Throwable ) 124 cat.log(toLevel(level), data, (Throwable )data); 125 else 126 cat.log(toLevel(level), data); 127 } 128 129 130 136 public void log(Object data, int level) { 137 this.log(data, DEFAULT_CHANNEL, level); 138 } 139 140 141 146 public void log(Object data) { 147 this.log(data, DEFAULT_CHANNEL, Logger.DEBUG); 148 } 149 150 151 152 159 160 public boolean isEnabled(String channel, int level) { 161 Category cat = getCategory(channel); 162 return cat.isEnabledFor(toLevel(level)); 163 } 164 165 166 167 172 173 public boolean isEnabled(int level) { 174 return isEnabled(DEFAULT_CHANNEL, level); 175 } 176 177 178 179 180 182 private Category getCategory(String channel) { 183 Category cat = Category.exists(channel); 184 if (cat == null) { 185 cat = Category.getInstance(channel); 186 } 187 return cat; 188 } 189 190 191 194 protected Level toLevel(int level) { 195 Level result = null; 196 197 switch (level) { 198 case EMERGENCY: 199 result = Level.FATAL; 200 break; 201 case CRITICAL: 202 result = Level.FATAL; 203 break; 204 case ERROR: 205 result = Level.ERROR; 206 break; 207 case WARNING: 208 result = Level.WARN; 209 break; 210 case INFO: 211 result = Level.INFO; 212 break; 213 case DEBUG: 214 result = Level.DEBUG; 215 break; 216 } 217 218 if (result == null) 219 result = Level.toLevel(level); 220 return result; 221 } 222 223 224 227 protected int fromLevel(Level level) { 228 int result = INFO; 229 230 if (level.equals(Level.FATAL)){ 231 result = EMERGENCY; 232 } 233 else if (level.equals(Level.ERROR)){ 234 result = ERROR; 235 } 236 else if (level.equals(Level.WARN)){ 237 result = WARNING; 238 } 239 else if (level.equals(Level.INFO)){ 240 result = INFO; 241 } 242 else if (level.equals(Level.DEBUG)){ 243 result = DEBUG; 244 } 245 else { 246 result = INFO; 247 } 248 249 return result; 250 } 251 252 } 253 | Popular Tags |