KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > code_example1


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 // Here is a code example to configure the JDBCAppender with a configuration-file
18

19 import org.apache.log4j.*;
20 import java.sql.*;
21 import java.lang.*;
22 import java.util.*;
23
24 public class Log4JTest
25 {
26    // Create a category instance for this class
27
static Category cat = Category.getInstance(Log4JTest.class.getName());
28
29    public static void main(String JavaDoc[] args)
30    {
31       // Ensure to have all necessary drivers installed !
32
try
33       {
34          Driver d = (Driver)(Class.forName("oracle.jdbc.driver.OracleDriver").newInstance());
35          DriverManager.registerDriver(d);
36       }
37       catch(Exception JavaDoc e){}
38
39       // Set the priority which messages have to be logged
40
cat.setPriority(Priority.INFO);
41
42       // Configuration with configuration-file
43
PropertyConfigurator.configure("log4jtestprops.txt");
44
45       // These messages with Priority >= setted priority will be logged to the database.
46
cat.debug("debug"); //this not, because Priority DEBUG is less than INFO
47
cat.info("info");
48       cat.error("error");
49       cat.fatal("fatal");
50    }
51 }
52
53
Popular Tags