/**
*  JAVA Logging Class
*/
package com.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
* @author imran
*
*/
public class ValidationLogger {

private static Properties loggerProperties = ValidationLogger.read(“log4j.properties”);

static Logger validation =
Logger.getLogger(ValidationLogger.class);

static {
PropertyConfigurator.configure(loggerProperties);
validation.setLevel(Level.DEBUG);
}

public static void logInfo(String msg){
validation.info(msg);
}

public static void logDebug(String msg){
validation.debug(msg);
}

public static void logError(String msg){
validation.error(msg);
}

public static void main(String[] args) {

}

private static Properties read(String aPropertyName) {
String propertiesLocation = System.getProperty(aPropertyName);
Properties props = new Properties();

try {
if (propertiesLocation != null) {
props.load(new FileInputStream(propertiesLocation));
}
else {
System.out.println(“Property ” + aPropertyName
+ ” returned null. If this message is displayed during a batch run then it can be safely ignored.”);
}
}
catch (IOException ioe) {
String msg = “Unable to load properties file at location ” + propertiesLocation;
System.out.println(msg + ioe);
}
return props;
}

}

Tags: , ,

1 Comment on Java Logging with log4j sample class

  1. waiting for next post

Leave a Reply