KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > xml > examples > XMLSample


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 org.apache.log4j.xml.examples;
18
19 import org.apache.log4j.xml.DOMConfigurator;
20 import org.apache.log4j.Logger;
21 import java.net.*;
22
23 /**
24
25    This <a HREF="doc-files/XMLSample.java">example code</a> shows how to
26    read an XML based configuration file using a DOM parser.
27
28    <p>Sample XML files <a HREF="doc-files/sample1.xml">sample1.xml</a>
29    and <a HREF="doc-files/sample2.xml">sample2.xml</a> are provided.
30
31
32    <p>Note that the log4j.dtd is not in the local directory.
33    It is found by the class loader.
34
35    @author Ceki G&uuml;lc&uuml;
36
37 */

38 public class XMLSample {
39
40   static Logger cat = Logger.getLogger(XMLSample.class);
41
42   public
43   static
44   void main(String JavaDoc argv[]) {
45
46     if(argv.length == 1)
47       init(argv[0]);
48     else
49       Usage("Wrong number of arguments.");
50     sample();
51   }
52
53   static
54   void Usage(String JavaDoc msg) {
55     System.err.println(msg);
56     System.err.println( "Usage: java " + XMLSample.class.getName() +
57             "configFile");
58     System.exit(1);
59   }
60
61   static
62   void init(String JavaDoc configFile) {
63     DOMConfigurator.configure(configFile);
64   }
65
66   static
67   void sample() {
68     int i = -1;
69     Logger root = Logger.getRootLogger();
70     cat.debug("Message " + ++i);
71     cat.warn ("Message " + ++i);
72     cat.error("Message " + ++i);
73     Exception JavaDoc e = new Exception JavaDoc("Just testing");
74     cat.debug("Message " + ++i, e);
75   }
76 }
77
Popular Tags