1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.log4j.xml.examples; 19 20 import org.apache.log4j.xml.DOMConfigurator; 21 import org.apache.log4j.Logger; 22 23 /** 24 25 This <a href="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="sample1.xml">sample1.xml</a> 29 and <a href="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ülcü 36 37 */ 38 public class XMLSample { 39 40 static Logger cat = Logger.getLogger(XMLSample.class); 41 42 public 43 static main(String argv[])44 void main(String 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 Usage(String msg)54 void Usage(String 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 init(String configFile)62 void init(String configFile) { 63 DOMConfigurator.configure(configFile); 64 } 65 66 static sample()67 void sample() { 68 int i = -1; 69 cat.debug("Message " + ++i); 70 cat.warn ("Message " + ++i); 71 cat.error("Message " + ++i); 72 Exception e = new Exception("Just testing"); 73 cat.debug("Message " + ++i, e); 74 } 75 } 76