PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/helpers/UtilLoggingLevel.java

#
Java | 238 lines | 100 code | 33 blank | 105 comment | 10 complexity | 3c495c9ea620f452cf8605a57b95333f MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.log4j.helpers;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.log4j.Level;
  21. /**
  22. * An extension of the Level class that provides support for java.util.logging
  23. * Levels.
  24. *
  25. *
  26. * @author Scott Deboy (sdeboy@apache.org)
  27. */
  28. public class UtilLoggingLevel extends Level {
  29. /**
  30. * Serialization version id.
  31. */
  32. private static final long serialVersionUID = 909301162611820211L;
  33. /**
  34. * Numerical value for SEVERE.
  35. */
  36. public static final int SEVERE_INT = 22000;
  37. /**
  38. * Numerical value for WARNING.
  39. */
  40. public static final int WARNING_INT = 21000;
  41. //INFO level defined in parent as 20000..no need to redefine here
  42. /**
  43. * Numerical value for CONFIG.
  44. */
  45. public static final int CONFIG_INT = 14000;
  46. /**
  47. * Numerical value for FINE.
  48. */
  49. public static final int FINE_INT = 13000;
  50. /**
  51. * Numerical value for FINER.
  52. */
  53. public static final int FINER_INT = 12000;
  54. /**
  55. * Numerical value for FINEST.
  56. */
  57. public static final int FINEST_INT = 11000;
  58. /**
  59. * Numerical value for UNKNOWN.
  60. */
  61. public static final int UNKNOWN_INT = 10000;
  62. /**
  63. * SEVERE.
  64. */
  65. public static final UtilLoggingLevel SEVERE =
  66. new UtilLoggingLevel(SEVERE_INT, "SEVERE", 0);
  67. /**
  68. * WARNING.
  69. */
  70. public static final UtilLoggingLevel WARNING =
  71. new UtilLoggingLevel(WARNING_INT, "WARNING", 4);
  72. /**
  73. * INFO.
  74. */
  75. //note: we've aligned the int values of the java.util.logging INFO level with log4j's level
  76. public static final UtilLoggingLevel INFO =
  77. new UtilLoggingLevel(INFO_INT, "INFO", 5);
  78. /**
  79. * CONFIG.
  80. */
  81. public static final UtilLoggingLevel CONFIG =
  82. new UtilLoggingLevel(CONFIG_INT, "CONFIG", 6);
  83. /**
  84. * FINE.
  85. */
  86. public static final UtilLoggingLevel FINE =
  87. new UtilLoggingLevel(FINE_INT, "FINE", 7);
  88. /**
  89. * FINER.
  90. */
  91. public static final UtilLoggingLevel FINER =
  92. new UtilLoggingLevel(FINER_INT, "FINER", 8);
  93. /**
  94. * FINEST.
  95. */
  96. public static final UtilLoggingLevel FINEST =
  97. new UtilLoggingLevel(FINEST_INT, "FINEST", 9);
  98. /**
  99. * Create new instance.
  100. * @param level numeric value for level.
  101. * @param levelStr symbolic name for level.
  102. * @param syslogEquivalent Equivalent syslog severity.
  103. */
  104. protected UtilLoggingLevel(final int level,
  105. final String levelStr,
  106. final int syslogEquivalent) {
  107. super(level, levelStr, syslogEquivalent);
  108. }
  109. /**
  110. * Convert an integer passed as argument to a level. If the
  111. * conversion fails, then this method returns the specified default.
  112. * @param val numeric value.
  113. * @param defaultLevel level to be returned if no level matches
  114. * numeric value.
  115. * @return matching level or default level.
  116. */
  117. public static UtilLoggingLevel toLevel(final int val,
  118. final UtilLoggingLevel defaultLevel) {
  119. switch (val) {
  120. case SEVERE_INT:
  121. return SEVERE;
  122. case WARNING_INT:
  123. return WARNING;
  124. case INFO_INT:
  125. return INFO;
  126. case CONFIG_INT:
  127. return CONFIG;
  128. case FINE_INT:
  129. return FINE;
  130. case FINER_INT:
  131. return FINER;
  132. case FINEST_INT:
  133. return FINEST;
  134. default:
  135. return defaultLevel;
  136. }
  137. }
  138. /**
  139. * Gets level matching numeric value.
  140. * @param val numeric value.
  141. * @return matching level or UtilLoggerLevel.FINEST if no match.
  142. */
  143. public static Level toLevel(final int val) {
  144. return toLevel(val, FINEST);
  145. }
  146. /**
  147. * Gets list of supported levels.
  148. * @return list of supported levels.
  149. */
  150. public static List getAllPossibleLevels() {
  151. ArrayList list = new ArrayList();
  152. list.add(FINE);
  153. list.add(FINER);
  154. list.add(FINEST);
  155. list.add(INFO);
  156. list.add(CONFIG);
  157. list.add(WARNING);
  158. list.add(SEVERE);
  159. return list;
  160. }
  161. /**
  162. * Get level with specified symbolic name.
  163. * @param s symbolic name.
  164. * @return matching level or Level.DEBUG if no match.
  165. */
  166. public static Level toLevel(final String s) {
  167. return toLevel(s, Level.DEBUG);
  168. }
  169. /**
  170. * Get level with specified symbolic name.
  171. * @param sArg symbolic name.
  172. * @param defaultLevel level to return if no match.
  173. * @return matching level or defaultLevel if no match.
  174. */
  175. public static Level toLevel(final String sArg,
  176. final Level defaultLevel) {
  177. if (sArg == null) {
  178. return defaultLevel;
  179. }
  180. String s = sArg.toUpperCase();
  181. if (s.equals("SEVERE")) {
  182. return SEVERE;
  183. }
  184. //if(s.equals("FINE")) return Level.FINE;
  185. if (s.equals("WARNING")) {
  186. return WARNING;
  187. }
  188. if (s.equals("INFO")) {
  189. return INFO;
  190. }
  191. if (s.equals("CONFI")) {
  192. return CONFIG;
  193. }
  194. if (s.equals("FINE")) {
  195. return FINE;
  196. }
  197. if (s.equals("FINER")) {
  198. return FINER;
  199. }
  200. if (s.equals("FINEST")) {
  201. return FINEST;
  202. }
  203. return defaultLevel;
  204. }
  205. }