Example4: (log msgs into Database file)



Develop a application for storing logging msgs using Database using JDBCAppender class.
the following steps are ...

step1:

create table logs(thread varchar2(20),classname varchar2(20),logmsg varchar2(20));

step2: develop a property file
log4j.properties



log4j.rootLogger=DEBUG,DB
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.driver=oracle.jdbc.driver.OracleDriver
log4j.appender.DB.URL=jdbc:oracle:thin:@localhost:1521:xe
log4j.appender.DB.user=praveen
log4j.appender.DB.password=praveen
log4j.appender.DB.sql=insert into logs values('%t','%c','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
 




program for JDBCAppender:

package com.dustbin.test;
import java.io.IOException;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
public class Sample {
static Logger logger=Logger.getLogger(Sample.class);
public static void main(String[] args)throws IOException {
PropertyConfigurator.configure("C:/logs/log4j.properties");
logger.debug("debug msg");
logger.info("info message");
logger.warn("warn message");
logger.error("error message");
logger.fatal("fatal message");
}
}

Output:

-> select * from logs;
log messages are printed in table.
 

0 comments:

Post a Comment