KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > lf5 > UsingLogMonitorAdapter > CustomizedLogLevels


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

16
17 package examples.lf5.UsingLogMonitorAdapter;
18
19 import org.apache.log4j.lf5.LogLevel;
20 import org.apache.log4j.lf5.util.LogMonitorAdapter;
21
22 /**
23  * This class is a simple example of how use the LogMonitorAdapter to
24  * bypass the Log4JAppender and post LogRecords directly to the LogMonitor
25  * using customized LogLevels
26  *
27  * To make this example work, ensure that the lf5.jar and lf5-license.jar
28  * files are in your classpath, and then run the example at the command line.
29  *
30  * @author Richard Hurst
31  */

32
33 // Contributed by ThoughtWorks Inc.
34

35 public class CustomizedLogLevels {
36     //--------------------------------------------------------------------------
37
// Constants:
38
//--------------------------------------------------------------------------
39
public final static LogLevel LEVEL_ONE = new LogLevel("LEVEL 1", 1);
40     public final static LogLevel LEVEL_TWO = new LogLevel("LEVEL 2", 2);
41     public final static LogLevel LEVEL_THREE = new LogLevel("LEVEL 3", 3);
42     public final static LogLevel LEVEL_FOUR = new LogLevel("LEVEL 4", 4);
43     public final static LogLevel DEFAULT = new LogLevel("DEFAULT", 0);
44
45     //--------------------------------------------------------------------------
46
// Protected Variables:
47
//--------------------------------------------------------------------------
48

49     //--------------------------------------------------------------------------
50
// Private Variables:
51
//--------------------------------------------------------------------------
52
private static LogMonitorAdapter _adapter;
53
54     static {
55         // The first LogLevel in the Array will be used as the default LogLevel.
56
_adapter = LogMonitorAdapter.newInstance(new LogLevel[]{DEFAULT, LEVEL_ONE,
57                                                                 LEVEL_TWO, LEVEL_THREE, LEVEL_FOUR, LogLevel.FATAL});
58         // if a different log level is to be used it can be specified as such
59
// _adapter.setDefaultLevel(LEVEL_THREE);
60
}
61     //--------------------------------------------------------------------------
62
// Constructors:
63
//--------------------------------------------------------------------------
64

65     //--------------------------------------------------------------------------
66
// Public Methods:
67
//--------------------------------------------------------------------------
68

69     public static void main(String JavaDoc[] args) {
70         CustomizedLogLevels test = new CustomizedLogLevels();
71         test.doMyBidding();
72     }
73
74     public void doMyBidding() {
75         // tell the LogMonitorAdapter which LogLevel is the severe Level if necessary
76
_adapter.setSevereLevel(LEVEL_ONE);
77
78         String JavaDoc levels = this.getClass().getName();
79
80         // will used the default Level
81
_adapter.log(levels, "Using the customized LogLevels");
82
83         _adapter.log(levels, LEVEL_FOUR, "This is a test");
84         _adapter.log(levels, LEVEL_THREE, "Hmmm fobidden doughnut");
85         _adapter.log(levels, LEVEL_ONE, "Danger Danger Will Robinson",
86                 new RuntimeException JavaDoc("DANGER"), "32");
87         _adapter.log(levels, LEVEL_TWO, "Exit stage right->");
88         _adapter.log(levels, LEVEL_FOUR, "What's up Doc?",
89                 new NullPointerException JavaDoc("Unfortunate exception"));
90
91     }
92
93     //--------------------------------------------------------------------------
94
// Protected Methods:
95
//--------------------------------------------------------------------------
96

97     //--------------------------------------------------------------------------
98
// Private Methods:
99
//--------------------------------------------------------------------------
100

101     //--------------------------------------------------------------------------
102
// Nested Top-Level Classes or Interfaces:
103
//--------------------------------------------------------------------------
104
}
105
106
107
108
109
110
Popular Tags