Hi all,
I am new to log4j. I want to use log4j in my web application. I need to write the debug,info,warn into a file. But i don't know how to do the same. Please send one example with explaination and how to get the logger object in web application.
Thanks suresh.
Please visit the following link:
static final Logger logger=Logger.getLogger(HelloWorld.class); public static void main(String args[]) throws IOException { FileAppender fileAppender=new FileAppender(new PatternLayout(),"service.txt"); logger.addAppender(fileAppender); BasicConfigurator.configure(); logger.debug("Debug Message."); logger.info("Info Message."); } This function will write logs messages in 'service.txt' file. for this purspose you need to have log4j.properties file configured as log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.File=/data/dev/newworkspace/log4jTry/src/com/anm/log/log.txt log4j.appender.rollingFile.MaxFileSize=2MB log4j.appender.rollingFile.MaxBackupIndex=2 log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n log4j.rootLogger = INFO, rollingFile
ans by 4p3
// program by Nishanth Thomas - InSolutions Global Bangalore
// example class file
package com.logs;
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession;
import org.apache.log4j.BasicConfigurator; import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator;
public class Logscollected {
static Logger log = Logger.getLogger(Logscollected.class);
public static void logscollection(Exception e, HttpServletRequest request) throws IOException{
HttpSession session = request.getSession(); String exceptiondetail = â??â?? ; //create a folder in your application config and place log4j.xml file there and // get path by the following; String strPath = request.getRealPath("config"); DOMConfigurator.configure(strPath+"/"+"log4jexp.xml"); //create log4j.xml file as give name like shown above log4jexp; try{ //// /* Get the Stack Trace messages ////// e = ((e==null)?((Exception)request.getAttribute("Exception")):e); StackTraceElement stem[]=e.getStackTrace(); for(int i=0;i<stem.length;i++){ if (!exceptiondetail.equals("")) exceptiondetail += "\n"; exceptiondetail += "\t"; exceptiondetail=exceptiondetail+stem[i].toString(); } log.debug(exceptiondetail); //log.info(e); // log.warn("Here is some WARN"); // log.info(object); log.error(e); // log.fatal("Here is some FATAL"); }catch(Exception e1){ e1.printStackTrace(); } }
}
In the catch of any servlet call that function like this
try{ // Some code���� } catch(Exception e) { // Logs collection log 4j------------------------ Logscollected.logscollection(e,request); //--------------------------- }
Now Finally
Lo4j.xml here we renamed to log4jexp.xml to avoid conflicts with server log4j.xml file palce log4jexp.xml in config folder ( here I used)
<--configure path according to your requrmts to save log file -->
<appender name="daoAppender" class="org.apache.log4j.RollingFileAppender"> <param name="maxFileSize" value="100KB" /> <param name="maxBackupIndex" value="2" /> <param name="File" value="${jboss.server.home.dir}/deploy/myexampleprj.war/serverlogs/serverlogs.txt" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd-MMM-yyyy HH:mm};%5p %c %m%n" /> </layout> </appender> <--configure path according for diff packge to your requrmts to save log file --> <!--appender name="appAppender" class="org.apache.log4j.RollingFileAppender"> <param name="maxFileSize" value="100KB" /> <param name="maxBackupIndex" value="2" /> <param name="File" value="${jboss.server.home.dir}/deploy/myexampleprj.war/serverlogs/serverlogs.txt" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}: %m%n " /> </layout> </appender--> <!â??package name given in category here com is the package give package name --> <!--category name="com.ex.actions"> <priority value="DEBUG"/> <appender-ref ref="appAppender"/> </category-->
<category name="com"> <priority value="DEBUG"/> <appender-ref ref="daoAppender"/> </category> <root> <!-- <appender-ref ref="appAppender" /> <appender-ref ref="daoAppender" /> --> </root>
Ads