728x90 AdSpace

Sunday, 1 September 2013

Log4j – JDBCAppender using property configuration


In this tutorials we are going to see how to create simple log4j application with JDBCAppender using property 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.properties in your project directory or source directory, add the following configuration

log4j.properties

log4j.rootLogger = DEBUG, JDBC

log4j.appender.JDBC=org.apache.log4j.jdbc.JDBCAppender

log4j.appender.JDBC.URL=jdbc:mysql://localhost:3306/TEST

log4j.appender.JDBC.driver=com.mysql.jdbc.Driver

log4j.appender.JDBC.user=root

log4j.appender.JDBC.password=root

log4j.appender.JDBC.sql=INSERT INTO DBLog VALUES('%C','%p','%m','%d')

log4j.appender.JDBC.layout=org.apache.log4j.PatternLayout

rootLogger 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.PropertyConfigurator;

public class JDBCAppender {

private static final String FILE_PATH = "log4j.properties";
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 {
PropertyConfigurator.configure(FILE_PATH);
log.info("Log4j Configured using Property 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 property file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  PropertyConfigurator.configure(FILE_PATH)  loads the configuration from property 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

jdbc
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

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 property configuration Rating: 5 Reviewed By: Annamalai Thangaraj