1 16 package org.apache.log4j.lf5; 17 18 import java.awt.*; 19 import java.util.*; 20 import java.util.List ; 21 22 35 36 38 public class LogLevel implements java.io.Serializable { 39 43 public final static LogLevel FATAL = new LogLevel("FATAL", 0); 45 public final static LogLevel ERROR = new LogLevel("ERROR", 1); 46 public final static LogLevel WARN = new LogLevel("WARN", 2); 47 public final static LogLevel INFO = new LogLevel("INFO", 3); 48 public final static LogLevel DEBUG = new LogLevel("DEBUG", 4); 49 50 public final static LogLevel SEVERE = new LogLevel("SEVERE", 1); 52 public final static LogLevel WARNING = new LogLevel("WARNING", 2); 53 public final static LogLevel CONFIG = new LogLevel("CONFIG", 4); 54 public final static LogLevel FINE = new LogLevel("FINE", 5); 55 public final static LogLevel FINER = new LogLevel("FINER", 6); 56 public final static LogLevel FINEST = new LogLevel("FINEST", 7); 57 58 protected String _label; 62 protected int _precedence; 63 private static LogLevel[] _log4JLevels; 67 private static LogLevel[] _jdk14Levels; 68 private static LogLevel[] _allDefaultLevels; 69 private static Map _logLevelMap; 70 private static Map _logLevelColorMap; 71 private static Map _registeredLogLevelMap = new HashMap(); 72 73 static { 77 _log4JLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG}; 78 _jdk14Levels = new LogLevel[]{SEVERE, WARNING, INFO, 79 CONFIG, FINE, FINER, FINEST}; 80 _allDefaultLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG, 81 SEVERE, WARNING, CONFIG, FINE, FINER, FINEST}; 82 83 _logLevelMap = new HashMap(); 84 for (int i = 0; i < _allDefaultLevels.length; i++) { 85 _logLevelMap.put(_allDefaultLevels[i].getLabel(), _allDefaultLevels[i]); 86 } 87 88 _logLevelColorMap = new HashMap(); 90 for (int i = 0; i < _allDefaultLevels.length; i++) { 91 _logLevelColorMap.put(_allDefaultLevels[i], Color.black); 92 } 93 } 94 95 public LogLevel(String label, int precedence) { 96 _label = label; 97 _precedence = precedence; 98 } 99 100 104 107 public String getLabel() { 108 return _label; 109 } 110 111 117 public boolean encompasses(LogLevel level) { 118 if (level.getPrecedence() <= getPrecedence()) { 119 return true; 120 } 121 122 return false; 123 } 124 125 133 public static LogLevel valueOf(String level) 134 throws LogLevelFormatException { 135 LogLevel logLevel = null; 136 if (level != null) { 137 level = level.trim().toUpperCase(); 138 logLevel = (LogLevel) _logLevelMap.get(level); 139 } 140 141 if (logLevel == null && _registeredLogLevelMap.size() > 0) { 143 logLevel = (LogLevel) _registeredLogLevelMap.get(level); 144 } 145 146 if (logLevel == null) { 147 StringBuffer buf = new StringBuffer (); 148 buf.append("Error while trying to parse (" + level + ") into"); 149 buf.append(" a LogLevel."); 150 throw new LogLevelFormatException(buf.toString()); 151 } 152 return logLevel; 153 } 154 155 161 public static LogLevel register(LogLevel logLevel) { 162 if (logLevel == null) return null; 163 164 if (_logLevelMap.get(logLevel.getLabel()) == null) { 166 return (LogLevel) _registeredLogLevelMap.put(logLevel.getLabel(), logLevel); 167 } 168 169 return null; 170 } 171 172 public static void register(LogLevel[] logLevels) { 173 if (logLevels != null) { 174 for (int i = 0; i < logLevels.length; i++) { 175 register(logLevels[i]); 176 } 177 } 178 } 179 180 public static void register(List logLevels) { 181 if (logLevels != null) { 182 Iterator it = logLevels.iterator(); 183 while (it.hasNext()) { 184 register((LogLevel) it.next()); 185 } 186 } 187 } 188 189 public boolean equals(Object o) { 190 boolean equals = false; 191 192 if (o instanceof LogLevel) { 193 if (this.getPrecedence() == 194 ((LogLevel) o).getPrecedence()) { 195 equals = true; 196 } 197 198 } 199 200 return equals; 201 } 202 203 public int hashCode() { 204 return _label.hashCode(); 205 } 206 207 public String toString() { 208 return _label; 209 } 210 211 public void setLogLevelColorMap(LogLevel level, Color color) { 213 _logLevelColorMap.remove(level); 215 if (color == null) { 217 color = Color.black; 218 } 219 _logLevelColorMap.put(level, color); 220 } 221 222 public static void resetLogLevelColorMap() { 223 _logLevelColorMap.clear(); 225 226 for (int i = 0; i < _allDefaultLevels.length; i++) { 228 _logLevelColorMap.put(_allDefaultLevels[i], Color.black); 229 } 230 } 231 232 236 public static List getLog4JLevels() { 237 return Arrays.asList(_log4JLevels); 238 } 239 240 public static List getJdk14Levels() { 241 return Arrays.asList(_jdk14Levels); 242 } 243 244 public static List getAllDefaultLevels() { 245 return Arrays.asList(_allDefaultLevels); 246 } 247 248 public static Map getLogLevelColorMap() { 249 return _logLevelColorMap; 250 } 251 252 256 protected int getPrecedence() { 257 return _precedence; 258 } 259 260 264 268 } 269 270 271 272 273 274 275 | Popular Tags |