KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > lf5 > InitUsingDefaultConfigurator


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 package examples.lf5.InitUsingDefaultConfigurator;
17
18 import org.apache.log4j.Logger;
19 import org.apache.log4j.NDC;
20 import org.apache.log4j.lf5.DefaultLF5Configurator;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  * This class is a simple example of how to configure the LogFactor5
26  * logging window using the DefaultLF5Configurator.
27  *
28  * The DefaultLF5Configurator uses a default configuration file stored
29  * in the log4j.jar in order to provide a default configuration for
30  * the LF5Appender.
31  *
32  * @author Brent Sprecher
33  */

34
35 // Contributed by ThoughtWorks Inc.
36

37 public class InitUsingDefaultConfigurator {
38     //--------------------------------------------------------------------------
39
// Constants:
40
//--------------------------------------------------------------------------
41

42     //--------------------------------------------------------------------------
43
// Protected Variables:
44
//--------------------------------------------------------------------------
45

46     //--------------------------------------------------------------------------
47
// Private Variables:
48
//--------------------------------------------------------------------------
49
private static Logger logger =
50             Logger.getLogger(InitUsingDefaultConfigurator.class);
51
52     //--------------------------------------------------------------------------
53
// Constructors:
54
//--------------------------------------------------------------------------
55

56     //--------------------------------------------------------------------------
57
// Public Methods:
58
//--------------------------------------------------------------------------
59

60     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
61         // Configure the LF5Appender using the DefaultLF5Configurator. This
62
// will add the LF5Appender to the root of the Category tree.
63
DefaultLF5Configurator.configure();
64
65         // Add an NDC to demonstrate how NDC information is output.
66
NDC.push("#23856");
67         // Log some information.
68
for (int i = 0; i < 10; i++) {
69             logger.debug("Hello, my name is Homer Simpson.");
70             logger.info("Mmmmmm .... Chocolate.");
71             logger.warn("Mmm...forbidden donut.");
72         }
73         // Clean up NDC
74
NDC.pop();
75         NDC.remove();
76
77         NDC.push("Another NDC");
78         // Log some information.
79
logger.fatal("Hello, my name is Bart Simpson.");
80         logger.error("Hi diddly ho good neighbour.");
81         // Clean up NDC
82
NDC.pop();
83         NDC.remove();
84
85         // Call methods on both classes.
86
InitUsingDefaultConfigurator.foo();
87         InnerInitUsingDefaultConfigurator.foo();
88
89         logger.info("Exiting InitUsingDefaultConfigurator.");
90
91     }
92
93     public static void foo() {
94         logger.debug("Entered foo in InitUsingDefaultConfigurator class");
95
96         NDC.push("#123456");
97         logger.debug("Hello, my name is Marge Simpson.");
98         logger.info("D'oh!! A deer! A female deer.");
99         // Clean up NDC
100
NDC.pop();
101         NDC.remove();
102     }
103
104     //--------------------------------------------------------------------------
105
// Protected Methods:
106
//--------------------------------------------------------------------------
107

108     //--------------------------------------------------------------------------
109
// Private Methods:
110
//--------------------------------------------------------------------------
111

112     //--------------------------------------------------------------------------
113
// Nested Top-Level Classes or Interfaces:
114
//--------------------------------------------------------------------------
115

116     public static class InnerInitUsingDefaultConfigurator {
117         static Logger logger =
118                 Logger.getLogger(InnerInitUsingDefaultConfigurator.class.getName());
119
120         static void foo() throws IOException JavaDoc {
121             // Configure the LF5Appender again. You can call
122
// DefaultLF5Configurator.configure() as often as you want
123
// without unexpected behavior.
124
DefaultLF5Configurator.configure();
125
126             logger.info("Entered foo in InnerInitUsingDefaultConfigurator class.");
127         }
128     }
129 }
130
131
132
133
134
135
Popular Tags