001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5; 018 019import java.awt.Color; 020import java.util.Arrays; 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.List; 024import java.util.Map; 025 026/** 027 * The LogLevel class defines a set of standard logging levels. 028 * 029 * The logging Level objects are ordered and are specified by ordered 030 * integers. Enabling logging at a given level also enables logging at all 031 * higher levels. 032 * 033 * @author Michael J. Sikorsky 034 * @author Robert Shaw 035 * @author Brent Sprecher 036 * @author Richard Hurst 037 * @author Brad Marlborough 038 */ 039 040// Contributed by ThoughtWorks Inc. 041 042public class LogLevel implements java.io.Serializable { 043 //-------------------------------------------------------------------------- 044 // Constants: 045 //-------------------------------------------------------------------------- 046 047 // log4j log levels. 048 public final static LogLevel FATAL = new LogLevel("FATAL", 0); 049 public final static LogLevel ERROR = new LogLevel("ERROR", 1); 050 public final static LogLevel WARN = new LogLevel("WARN", 2); 051 public final static LogLevel INFO = new LogLevel("INFO", 3); 052 public final static LogLevel DEBUG = new LogLevel("DEBUG", 4); 053 054 // jdk1.4 log levels NOTE: also includes INFO 055 public final static LogLevel SEVERE = new LogLevel("SEVERE", 1); 056 public final static LogLevel WARNING = new LogLevel("WARNING", 2); 057 public final static LogLevel CONFIG = new LogLevel("CONFIG", 4); 058 public final static LogLevel FINE = new LogLevel("FINE", 5); 059 public final static LogLevel FINER = new LogLevel("FINER", 6); 060 public final static LogLevel FINEST = new LogLevel("FINEST", 7); 061 062 //-------------------------------------------------------------------------- 063 // Protected Variables: 064 //-------------------------------------------------------------------------- 065 protected String _label; 066 protected int _precedence; 067 //-------------------------------------------------------------------------- 068 // Private Variables: 069 //-------------------------------------------------------------------------- 070 private static LogLevel[] _log4JLevels; 071 private static LogLevel[] _jdk14Levels; 072 private static LogLevel[] _allDefaultLevels; 073 private static Map _logLevelMap; 074 private static Map _logLevelColorMap; 075 private static Map _registeredLogLevelMap = new HashMap(); 076 077 //-------------------------------------------------------------------------- 078 // Constructors: 079 //-------------------------------------------------------------------------- 080 static { 081 _log4JLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG}; 082 _jdk14Levels = new LogLevel[]{SEVERE, WARNING, INFO, 083 CONFIG, FINE, FINER, FINEST}; 084 _allDefaultLevels = new LogLevel[]{FATAL, ERROR, WARN, INFO, DEBUG, 085 SEVERE, WARNING, CONFIG, FINE, FINER, FINEST}; 086 087 _logLevelMap = new HashMap(); 088 for (int i = 0; i < _allDefaultLevels.length; i++) { 089 _logLevelMap.put(_allDefaultLevels[i].getLabel(), _allDefaultLevels[i]); 090 } 091 092 // prepopulate map with levels and text color of black 093 _logLevelColorMap = new HashMap(); 094 for (int i = 0; i < _allDefaultLevels.length; i++) { 095 _logLevelColorMap.put(_allDefaultLevels[i], Color.black); 096 } 097 } 098 099 public LogLevel(String label, int precedence) { 100 _label = label; 101 _precedence = precedence; 102 } 103 104 //-------------------------------------------------------------------------- 105 // Public Methods: 106 //-------------------------------------------------------------------------- 107 108 /** 109 * Return the Label of the LogLevel. 110 */ 111 public String getLabel() { 112 return _label; 113 } 114 115 /** 116 * Returns true if the level supplied is encompassed by this level. 117 * For example, LogLevel.SEVERE encompasses no other LogLevels and 118 * LogLevel.FINE encompasses all other LogLevels. By definition, 119 * a LogLevel encompasses itself. 120 */ 121 public boolean encompasses(LogLevel level) { 122 if (level.getPrecedence() <= getPrecedence()) { 123 return true; 124 } 125 126 return false; 127 } 128 129 /** 130 * Convert a log level label into a LogLevel object. 131 * 132 * @param level The label of a level to be converted into a LogLevel. 133 * @return LogLevel The LogLevel with a label equal to level. 134 * @throws LogLevelFormatException Is thrown when the level can not be 135 * converted into a LogLevel. 136 */ 137 public static LogLevel valueOf(String level) 138 throws LogLevelFormatException { 139 LogLevel logLevel = null; 140 if (level != null) { 141 level = level.trim().toUpperCase(); 142 logLevel = (LogLevel) _logLevelMap.get(level); 143 } 144 145 // Didn't match, Check for registered LogLevels 146 if (logLevel == null && _registeredLogLevelMap.size() > 0) { 147 logLevel = (LogLevel) _registeredLogLevelMap.get(level); 148 } 149 150 if (logLevel == null) { 151 StringBuffer buf = new StringBuffer(); 152 buf.append("Error while trying to parse (" + level + ") into"); 153 buf.append(" a LogLevel."); 154 throw new LogLevelFormatException(buf.toString()); 155 } 156 return logLevel; 157 } 158 159 /** 160 * Registers a used defined LogLevel. 161 * 162 * @param logLevel The log level to be registered. Cannot be a default LogLevel 163 * @return LogLevel The replaced log level. 164 */ 165 public static LogLevel register(LogLevel logLevel) { 166 if (logLevel == null) return null; 167 168 // ensure that this is not a default log level 169 if (_logLevelMap.get(logLevel.getLabel()) == null) { 170 return (LogLevel) _registeredLogLevelMap.put(logLevel.getLabel(), logLevel); 171 } 172 173 return null; 174 } 175 176 public static void register(LogLevel[] logLevels) { 177 if (logLevels != null) { 178 for (int i = 0; i < logLevels.length; i++) { 179 register(logLevels[i]); 180 } 181 } 182 } 183 184 public static void register(List logLevels) { 185 if (logLevels != null) { 186 Iterator it = logLevels.iterator(); 187 while (it.hasNext()) { 188 register((LogLevel) it.next()); 189 } 190 } 191 } 192 193 public boolean equals(Object o) { 194 boolean equals = false; 195 196 if (o instanceof LogLevel) { 197 if (this.getPrecedence() == 198 ((LogLevel) o).getPrecedence()) { 199 equals = true; 200 } 201 202 } 203 204 return equals; 205 } 206 207 public int hashCode() { 208 return _label.hashCode(); 209 } 210 211 public String toString() { 212 return _label; 213 } 214 215 // set a text color for a specific log level 216 public void setLogLevelColorMap(LogLevel level, Color color) { 217 // remove the old entry 218 _logLevelColorMap.remove(level); 219 // add the new color entry 220 if (color == null) { 221 color = Color.black; 222 } 223 _logLevelColorMap.put(level, color); 224 } 225 226 public static void resetLogLevelColorMap() { 227 // empty the map 228 _logLevelColorMap.clear(); 229 230 // repopulate map and reset text color black 231 for (int i = 0; i < _allDefaultLevels.length; i++) { 232 _logLevelColorMap.put(_allDefaultLevels[i], Color.black); 233 } 234 } 235 236 /** 237 * @return A <code>List</code> of <code>LogLevel</code> objects that map 238 * to log4j <code>Priority</code> objects. 239 */ 240 public static List getLog4JLevels() { 241 return Arrays.asList(_log4JLevels); 242 } 243 244 public static List getJdk14Levels() { 245 return Arrays.asList(_jdk14Levels); 246 } 247 248 public static List getAllDefaultLevels() { 249 return Arrays.asList(_allDefaultLevels); 250 } 251 252 public static Map getLogLevelColorMap() { 253 return _logLevelColorMap; 254 } 255 256 //-------------------------------------------------------------------------- 257 // Protected Methods: 258 //-------------------------------------------------------------------------- 259 260 protected int getPrecedence() { 261 return _precedence; 262 } 263 264 //-------------------------------------------------------------------------- 265 // Private Methods: 266 //-------------------------------------------------------------------------- 267 268 //-------------------------------------------------------------------------- 269 // Nested Top-Level Classes or Interfaces: 270 //-------------------------------------------------------------------------- 271 272} 273 274 275 276 277 278