KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > Trivial


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;
18
19
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.BasicConfigurator;
22 import org.apache.log4j.NDC;
23
24 /**
25    View the <a HREF="doc-files/Trivial.java">source code</a> of this a
26    trivial usage example. Running <code>java examples.Trivial</code>
27    should output something similar to:
28
29    <pre>
30       0 INFO [main] examples.Trivial (Client #45890) - Awake awake. Put on thy strength.
31       15 DEBUG [main] examples.Trivial (Client #45890 DB) - Now king David was old.
32       278 INFO [main] examples.Trivial$InnerTrivial (Client #45890) - Entered foo.
33       293 INFO [main] examples.Trivial (Client #45890) - Exiting Trivial.
34    </pre>
35    
36    <p> The increasing numbers at the beginning of each line are the
37    times elapsed since the start of the program. The string between
38    the parentheses is the nested diagnostic context.
39
40    <p>See {@link Sort} and {@link SortAlgo} for sligtly more elaborate
41    examples.
42
43    <p>Note thent class files for the example code is not included in
44    any of the distributed log4j jar files. You will have to add the
45    directory <code>/dir-where-you-unpacked-log4j/classes</code> to
46    your classpath before trying out the examples.
47
48  */

49 public class Trivial {
50
51   static Logger logger = Logger.getLogger(Trivial.class);
52
53   public static void main(String JavaDoc[] args) {
54     BasicConfigurator.configure();
55     NDC.push("Client #45890");
56
57     logger.info("Awake awake. Put on thy strength.");
58     Trivial.foo();
59     InnerTrivial.foo();
60     logger.info("Exiting Trivial.");
61   }
62
63   static
64   void foo() {
65     NDC.push("DB");
66     logger.debug("Now king David was old.");
67     NDC.pop();
68   }
69
70   static class InnerTrivial {
71     static Logger logger = Logger.getLogger(InnerTrivial.class);
72
73     static
74     void foo() {
75       logger.info("Entered foo.");
76     }
77   }
78 }
79
Popular Tags