a. Download the log4j.jar file from the following web page.
http://logging.apache.org/log4j/docs/download.html
b. I have used apache-log4j-1.2.15.jar version in my example.
c. Add this jar file into your java project.
d. Import the log4j classes(Logger) into your java classes.
org.apache.log4j.Logger;
e. i have created a logger class in which i have a static method which return the logger object. In that static method,it has a Hashtable object which will hold the logger object.If second time called, the method will check in the Hashtable and if any logger object available, it will be retured. otherwise it will create a new object and stored in the Hashtable object and return the Logger object created.
public static Logger getLogger(String strLogName)
{
objHashTable = new Hashtable();
Logger log = (Logger)objHashTable.get(strLogName);
if(log == null)
{
log = Logger.getLogger(strLogName);
objHashTable.put(strLogName, log);
}
return log;
}
f. The configuration in the log4j.properties is as follows.
#log4j.rootLogger=DEBUG, A1, A2log4j.logger.A1=ALL,A1log4j.logger.A2=ALL,A2
# This is to print on the Console# log4j.appender.A1=org.apache.log4j.ConsoleAppender
# This is to write to a Log File
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.MaxBackupIndex=10
log4j.appender.A1.MaxFileSize=10MB
log4j.appender.A1.File=D:/OHIOLog.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d - %m%n# --Print the date in ISO 8601 format# log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#---- Below for A2 configuration# This is to write to a Log File
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.MaxBackupIndex=10
log4j.appender.A2.MaxFileSize=10MB
log4j.appender.A2.File=D:/BadDatas.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d - %m%n# --Print the date in ISO 8601 format# log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
In this example iam using 2 different log files, one for logging the error and debug messages and the other for collecting the bad datas which cannot be inserted into the table with any of thefollowing failures.
1. Primary Key constraints2. FK constrains3. Null values
Iam capturing the entire sql statement if any error occured during insert. In this example iam using 2 files A1 and A2.A1 will capture the debug and error messages.
In my java classes i will be using like the folloing statement.
try{
String sPersonInsert = "Insert into persons(person_id, name)values(iPersonId,sName)";
// Example of debug message logging into log file.
UnivLogger.getLogger("A1").debug("Database connection established ");
}catch(SQLException eSQLException)
{
// Example of error message logging into log file.
UnivLogger.getLogger("A2").error("The failed insert SQL statement is ==>"+sPersonInsert);
}
If you have any question, please update this post with your comments, i will get back to you with answers. Happy Coding.
If you want to print only the log message, with out any time stamp in it, in use the following conversion pattern
log4j.appender.A2.layout.ConversionPattern=%m%n
No comments:
Post a Comment