Log4j
>> Wednesday, May 25, 2011
How To write Log4J in our application?
In Log4j there are Appender, Layouts,Levels
Appenders:Console,File,Jdbc,SMTP......
Layouts:Simple, Pattern,Date,Html,Xml
Levels:DEBUG,FATAL,WARN,INFO,ERROR
STEP:1
Write a property file and place under src folder. and in D or C drive as your wish.
For example com.blog.bhaskar.log4j.properties
Log4J.rootLogger=debug,info,myAppender1
Log4J.appender.myAppender1=org.apache.log4J.Fileappender
Log4J.appender.myAppender1.File=c://text.html
Log4J.appender.myAppender1.Layout=org.apache.log4j.SimpleLayout
Log4J.appender.myAppender1.Layout=org.apache.log4j.HTMLLayout.
STEP:2
Write a log4j class and place in utili package
public class Log4jutility{
public static Logger testLogger=null;
static{
try{
Properties p=new Properties();
p.load(new FileInputStream(c://log4j.properties));
PropertyConfigurator.configure(p);
}
catch(Exception e)
{
testLogger=Logger.getLogger(Log4jUtility.class);
testLogger.fatal("log4j is not configure");
e.printStackTrace();
}
}
public static void logInfoMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logErrorMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logFatalMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logDebugMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logWarnMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
}
Step:3
For example if you want to use log4j in your class means (form,actionclass,daoimpl any where) follow the below example.
Take a class provide log4j.
public class Lo4jDemo{
public static void main(String[] args)
{
Log4jUtility.logInfoMessage(Log4jDemo.class,"information");
Log4jUtility.logErroeMessage(Log4jDemo.class,"information"); Log4jUtility.logDebugMessage(Log4jDemo.class,"information"); Log4jUtility.logFatalMessage(Log4jDemo.class,"information");
}
}
NOTE:The output of logger message will be displayed in c://text.html(configured in log4j properties) location in the form of file Layout and an HTML Layout is created automatically in C://text.html
In Log4j there are Appender, Layouts,Levels
Appenders:Console,File,Jdbc,SMTP......
Layouts:Simple, Pattern,Date,Html,Xml
Levels:DEBUG,FATAL,WARN,INFO,ERROR
STEP:1
Write a property file and place under src folder. and in D or C drive as your wish.
For example com.blog.bhaskar.log4j.properties
Log4J.rootLogger=debug,info,myAppender1
Log4J.appender.myAppender1=org.apache.log4J.Fileappender
Log4J.appender.myAppender1.File=c://text.html
Log4J.appender.myAppender1.Layout=org.apache.log4j.SimpleLayout
Log4J.appender.myAppender1.Layout=org.apache.log4j.HTMLLayout.
STEP:2
Write a log4j class and place in utili package
public class Log4jutility{
public static Logger testLogger=null;
static{
try{
Properties p=new Properties();
p.load(new FileInputStream(c://log4j.properties));
PropertyConfigurator.configure(p);
}
catch(Exception e)
{
testLogger=Logger.getLogger(Log4jUtility.class);
testLogger.fatal("log4j is not configure");
e.printStackTrace();
}
}
public static void logInfoMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logErrorMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logFatalMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logDebugMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
public static void logWarnMessage(class objclass, String sMessage){
testLogger=Logger.getLogger(objclass);
testLogger.info(sMessage);
}
}
Step:3
For example if you want to use log4j in your class means (form,actionclass,daoimpl any where) follow the below example.
Take a class provide log4j.
public class Lo4jDemo{
public static void main(String[] args)
{
Log4jUtility.logInfoMessage(Log4jDemo.class,"information");
Log4jUtility.logErroeMessage(Log4jDemo.class,"information"); Log4jUtility.logDebugMessage(Log4jDemo.class,"information"); Log4jUtility.logFatalMessage(Log4jDemo.class,"information");
}
}
NOTE:The output of logger message will be displayed in c://text.html(configured in log4j properties) location in the form of file Layout and an HTML Layout is created automatically in C://text.html
0 comments:
Post a Comment