Logger Implementation
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class ApplicationNameLogger {
Example:
// public class ABCLogger {
private static Logger logger;
public static boolean TRACING = false;
public static final String DEBUG = "DEBUG";
public static final String INFO = "INFO";
public static final String WARNING = "WARNING";
public static final String ERROR = "ERROR";
public static void ApplicationNameLog(Class className, String logMsg, String priority)
{
logger = Logger.getLogger(className);
if(priority.equalsIgnoreCase(DEBUG) && TRACING)
{
logger.debug(logMsg);
}
else if (priority.equalsIgnoreCase(INFO) && TRACING)
{
logger.info(logMsg);
}
else if (priority.equalsIgnoreCase(WARNING) && TRACING)
{
logger.warn(logMsg);
}
else if (priority.equalsIgnoreCase(ERROR) && TRACING)
{
logger.error(logMsg);
}
}
public static void setTracing(boolean tracingValue) {
TRACING = tracingValue;
}
}
Invoking the Logger from Java classes.
public class ABCAction extends Action {
public static final Class CLASS_NAME = ABCAction.class;
public void getCountryName {
ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, " Enter into getCountryName () ", ApplicationNameLogger.INFO);
try {
---
--
--
} catch(Exception exp) {
ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, " In getCountryName ()->Exception: "
+ exp.getMessage(), ApplicationNameLogger.ERROR);
}
ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, "Exit from getCountryName ()",
ApplicationNameLogger.INFO);
}
Loading log4j properties file on application start-up.
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class ApplicationNameLoggerServlet extends HttpServlet {
public void init() {
try {
InputStream is = this.getClass().getResourceAsStream(
"log4j.properties");
Properties p = new Properties();
p.load(is);
PropertyConfigurator.configure(p);
} catch (Exception ex) {
System.out.println("Exception occurred during log4j initialization");
}
}
}
Web.xml mapping for loading the logger Servlet on application start-up.
<servlet>
<servlet-name> ABCLoggerServlet </servlet-name>
<servlet-class>com.abc.xyz.mno.applicationName.servlets.ABCLoggerServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
0 Comments:
Post a Comment
<< Home