KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > log > LogLevel


1 /*_############################################################################
2   _##
3   _## SNMP4J - LogLevel.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.log;
23
24 public class LogLevel {
25
26   public static final int LEVEL_NONE = 0;
27   public static final int LEVEL_OFF = 1;
28   public static final int LEVEL_ALL = 2;
29   public static final int LEVEL_TRACE = 3;
30   public static final int LEVEL_DEBUG = 4;
31   public static final int LEVEL_INFO = 5;
32   public static final int LEVEL_WARN = 6;
33   public static final int LEVEL_ERROR = 7;
34   public static final int LEVEL_FATAL = 8;
35
36   private static final String JavaDoc[] LEVEL_STRINGS = {
37     "NONE","OFF","ALL","TRACE","DEBUG","INFO","WARN","INFO","ERROR","FATAL"
38   };
39
40   public static final LogLevel NONE = new LogLevel(LEVEL_NONE);
41   public static final LogLevel OFF = new LogLevel(LEVEL_OFF);
42   public static final LogLevel ALL = new LogLevel(LEVEL_ALL);
43   public static final LogLevel TRACE = new LogLevel(LEVEL_TRACE);
44   public static final LogLevel DEBUG = new LogLevel(LEVEL_DEBUG);
45   public static final LogLevel INFO = new LogLevel(LEVEL_INFO);
46   public static final LogLevel WARN = new LogLevel(LEVEL_WARN);
47   public static final LogLevel ERROR = new LogLevel(LEVEL_ERROR);
48   public static final LogLevel FATAL = new LogLevel(LEVEL_FATAL);
49
50   private int level;
51
52   public LogLevel(int level) {
53     if ((level < 0) || (level > 8)) {
54       throw new IllegalArgumentException JavaDoc("Unknown log level "+level);
55     }
56     this.level = level;
57   }
58
59   public LogLevel(String JavaDoc levelString) {
60     this(levelFromString(levelString));
61   }
62
63   /**
64    * Convert a level string "OFF", "ALL", "DEBUG", etc. into a level number.
65    * @param levelString
66    * one of the level strings "OFF", "ALL", "TRACE", "DEBUG", "INFO", "WARN",
67    * "INFO", "ERROR", and "FATAL".
68    * @return
69    * a number > 0 if the level string could be converted into a level,
70    * 0 otherwise.
71    */

72   public static int levelFromString(String JavaDoc levelString) {
73     int ind = -1;
74     for (int i=0; i<LEVEL_STRINGS.length; i++) {
75       if (LEVEL_STRINGS[i].equals(levelString)) {
76         ind = i;
77         break;
78       }
79     }
80     return ind;
81   }
82
83   /**
84    * Returns a <code>LogLevel</code> object for the specified level string.
85    * @param levelString
86    * one of the level strings "OFF", "ALL", "TRACE", "DEBUG", "INFO", "WARN",
87    * "INFO", "ERROR", and "FATAL".
88    * @return
89    * one of the <code>LogLevel</code> constants defined by this class.
90    * @since 1.7.2
91    */

92   public static LogLevel toLevel(String JavaDoc levelString) {
93     return new LogLevel(levelFromString(levelString));
94   }
95
96   public int getLevel() {
97     return level;
98   }
99
100   public String JavaDoc toString() {
101     return LEVEL_STRINGS[level];
102   }
103
104 }
105
Popular Tags