We are moved to new domain
Click -> www.ehowtonow.com
Friday, 6 September 2013

Log4j – JDBCAppender using XML configuration


In this tutorials we are going to see how to create simple log4j application with JDBCAppender using xml configuration.
JDBCAppender
The JDBCAppender is used to save log message into database,the JDBCAppender available configurations are given below
Property
Description
bufferSize Sets the size of log messages wait in buffer.The Default size is 1.
driver Sets the driver class of database.The default driver is sun.jdbc.odbc.JdbcOdbcDriver
layout Set the layout used with logger.The Default layout is org.apache.log4j.PatternLayout
password Set the password of database
user Set the user name of database
URL Set the database URL
sql The sql statement to inset log into database for every logging event
Create Table
Create the following table in your database,here I used MySQL as my database.Query for create table given below.
CREATE TABLE `DBLog` (

`LOGGER` VARCHAR(200) NOT NULL,

`LEVEL` VARCHAR(45) NOT NULL,

`MESSAGE` VARCHAR(1000) NOT NULL,

`DATE` VARCHAR(45) NOT NULL

)

In this table Logger is used to save fully qualified class name.Level is used to save log4j level.Message is used to save log message,Data is used to date of log message created.

Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.jdbcappender under log4j example,add log4j jar and mysql-connector.jar in your build path.I used log4j-1.2.17.jar jar for this application.you can download this jar from Apache web site.Create log4j.xml in your project directory or source directory, add the following configuration

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="JDBC" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:mysql://localhost:3306/test" />
<param name="user" value="root" />
<param name="password" value="root" />
<param name="driver" value="com.mysql.jdbc.Driver" />
<param name="sql" value="INSERT INTO dblog VALUES('%C','%p','%m','%d')" />
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="JDBC" />
</root>
</log4j:configuration>

root used to configure Level and Appender, in this example I used DEBUG as Level and org.apache.log4j.jdbc.JDBCAppender is used as appender(JDBC is name of appender).The detailed configuration explained in above table

PatternLayout is used to configure log4j message format.

Now create java class called JDBCAppender under com.javatutorialscorner.log4j.jdbcappender package.

JDBCAppender.java

package com.javatutorialscorner.log4j.jdbcappender;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class JDBCAppender {

private static final String FILE_PATH = "log4j.xml";
private static final Logger log = Logger.getLogger(JDBCAppender.class);

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (FILE_PATH == null) {
BasicConfigurator.configure();
log.info("Log4j Configured using Basic Configurator");
} else {
DOMConfigurator.configure(FILE_PATH);
log.info("Log4j Configured using XML configurator");
}

log.debug("Debug message by Java Tutorials Corner");
log.info("Info message by Java Tutorials Corner");
log.warn("Warn message by Java Tutorials Corner");
log.error("Error message by Java Tutorials Corner");
log.fatal("Fatal message by Java Tutorials Corner");
}

}

Create instance for Logger class , getLogger(JDBCAppender.class) method accept fully qualified class name as its argument. In this example if xml file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  DOMConfigurator.configure(FILE_PATH)  loads the configuration from xml file .

now you can run the application, you will see the following output at specified database. 

Note: you are unable to edit table data.

OUTPUT

jdbcappender

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Log4j – JDBCAppender using XML configuration Rating: 5 Reviewed By: eHowToNow