1 22 package org.jboss.logging; 23 24 35 public class DynamicLogger extends Logger 36 { 37 38 private static final long serialVersionUID = -5963699806863917370L; 39 40 41 public static final int LOG_LEVEL_NONE = 0; 42 43 44 public static final int LOG_LEVEL_FATAL = 1; 45 46 47 public static final int LOG_LEVEL_ERROR = 2; 48 49 50 public static final int LOG_LEVEL_WARN = 3; 51 52 53 public static final int LOG_LEVEL_INFO = 4; 54 55 56 public static final int LOG_LEVEL_DEBUG = 5; 57 58 59 public static final int LOG_LEVEL_TRACE = 6; 60 61 62 public final static String [] LOG_LEVEL_STRINGS = 63 { "NONE", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" }; 64 65 66 private int logLevel = LOG_LEVEL_DEBUG; 67 68 74 public static DynamicLogger getDynamicLogger(String name) 75 { 76 return new DynamicLogger(name); 77 } 78 79 88 public static DynamicLogger getDynamicLogger(String name, String suffix) 89 { 90 return new DynamicLogger(name + "." + suffix); 91 } 92 93 100 public static DynamicLogger getDynamicLogger(Class clazz) 101 { 102 return new DynamicLogger(clazz.getName()); 103 } 104 105 114 public static DynamicLogger getDynamicLogger(Class clazz, String suffix) 115 { 116 return new DynamicLogger(clazz.getName() + "." + suffix); 117 } 118 119 124 protected DynamicLogger(final String name) 125 { 126 super(name); 127 } 128 129 134 public void setLogLevel(int logLevel) 135 { 136 if (logLevel >= LOG_LEVEL_NONE && logLevel <= LOG_LEVEL_TRACE) 137 { 138 this.logLevel = logLevel; 139 } 140 } 141 142 147 public int getLogLevel() 148 { 149 return logLevel; 150 } 151 152 157 public void setLogLevelAsString(String logLevelString) 158 { 159 if (logLevelString != null) 160 { 161 logLevelString = logLevelString.toUpperCase().trim(); 162 163 for (int i = 0; i <= LOG_LEVEL_TRACE; i++) 164 { 165 if (logLevelString.equals(LOG_LEVEL_STRINGS[i])) 166 { 167 this.logLevel = i; 169 break; 170 } 171 } 172 } 173 } 174 175 180 public String getLogLevelAsString() 181 { 182 return LOG_LEVEL_STRINGS[logLevel]; 183 } 184 185 190 public void log(Object message) 191 { 192 switch (logLevel) 193 { 194 case LOG_LEVEL_TRACE: 195 super.trace(message); 196 break; 197 198 case LOG_LEVEL_DEBUG: 199 super.debug(message); 200 break; 201 202 case LOG_LEVEL_INFO: 203 super.info(message); 204 break; 205 206 case LOG_LEVEL_WARN: 207 super.warn(message); 208 break; 209 210 case LOG_LEVEL_ERROR: 211 super.error(message); 212 break; 213 214 case LOG_LEVEL_FATAL: 215 super.fatal(message); 216 break; 217 218 case LOG_LEVEL_NONE: 219 default: 220 break; 222 } 223 } 224 225 231 public void log(Object message, Throwable t) 232 { 233 switch (logLevel) 234 { 235 case LOG_LEVEL_TRACE: 236 super.trace(message, t); 237 break; 238 239 case LOG_LEVEL_DEBUG: 240 super.debug(message, t); 241 break; 242 243 case LOG_LEVEL_INFO: 244 super.info(message, t); 245 break; 246 247 case LOG_LEVEL_WARN: 248 super.warn(message, t); 249 break; 250 251 case LOG_LEVEL_ERROR: 252 super.error(message, t); 253 break; 254 255 case LOG_LEVEL_FATAL: 256 super.fatal(message, t); 257 break; 258 259 case LOG_LEVEL_NONE: 260 default: 261 break; 263 } 264 } 265 } 266 | Popular Tags |