Friday, January 04, 2008

Log4j Properties File

# The possible values here are debug, info, warn, error, fatal

log4j.rootLogger=info,R
log4j.logger.com.abc.xyz.projectName.action=debug,A1
log4j.logger.com.abc.xyz.projectName.bean=debug,A2
log4j.logger.com.abc.xyz.projectName.form=debug,A3
log4j.logger.com.abc.xyz.projectName.dao=debug,A4
log4j.logger.com.abc.xyz.projectName.dto=debug,A5
log4j.logger.com.abc.xyz.projectName.interfaces=debug,A6
log4j.logger.com.abc.xyz.projectName.services=debug,A7
log4j.logger.com.abc.xyz.projectName.servlets=debug,A8
log4j.logger.com.abc.xyz.projectName.utils=debug,A9

##ConsoleAppender (writes to system console).

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A3=org.apache.log4j.ConsoleAppender
log4j.appender.A4=org.apache.log4j.ConsoleAppender
log4j.appender.A5=org.apache.log4j.ConsoleAppender
log4j.appender.A6=org.apache.log4j.ConsoleAppender
log4j.appender.A7=org.apache.log4j.ConsoleAppender
log4j.appender.A8=org.apache.log4j.ConsoleAppender
log4j.appender.A9=org.apache.log4j.ConsoleAppender

# Uses PatternLayout.Dont change this

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A5.layout=org.apache.log4j.PatternLayout
log4j.appender.A6.layout=org.apache.log4j.PatternLayout
log4j.appender.A7.layout=org.apache.log4j.PatternLayout
log4j.appender.A8.layout=org.apache.log4j.PatternLayout
log4j.appender.A9.layout=org.apache.log4j.PatternLayout

##Defines the way comments are written to the log file, for e.g. %d tells the logger to write the date
# for a full list see the log4j manual. %p is the priority (debug, info etc as defined above)

log4j.appender.R.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L %m%n
log4j.appender.A1.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Action ---> %m%n
log4j.appender.A2.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Bean ---> %m%n
log4j.appender.A3.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Form ---> %m%n
log4j.appender.A4.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Dao ---> %m%n
log4j.appender.A5.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Dto ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Interfaces ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Services ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Servlets ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Utils ---> %m%n


# Additivity Flag

log4j.additivity.com.abc.xyz.projectName.action=false
log4j.additivity.com.abc.xyz.projectName.form=false
log4j.additivity.com.abc.xyz.projectName.bean=false
log4j.additivity.com.abc.xyz.projectName.dao=false
log4j.additivity.com.abc.xyz.projectName.dto=false
log4j.additivity.com.abc.xyz.projectName.interfaces=false
log4j.additivity.com.abc.xyz.projectName.services=false
log4j.additivity.com.abc.xyz.projectName.servlets=false
log4j.additivity.com.abc.xyz.projectName.utils=false
log4j.additivity.jsp_servlet=false

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>

CONTACT

p> You can reach me @ My EMail ID

Powered by Blogger