Home Discussion Discuss: A simple example of log4j for Servlet

  • HlpTrjimCwChubby November 1, 2011 at 5:13 AM

    Mighty useful. Make no misakte, I appreciate it.

  • Log4j using servlet log4j.xmlNishanth Thomas February 29, 2012 at 12:09 PM

    // 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); //--------------------------- } 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) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" > <--configure path according to your requrmts to save log file --> <log4j:configuration> <!--code between <log4j:configuration> </log4j:configuration> --> <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--> <!--package name given in category here com is the package --> <category name="com"> <priority value="DEBUG"/> <appender-ref ref="daoAppender"/> </category> <root> <!-- <appender-ref ref="appAppender" /> <appender-ref ref="daoAppender" /> --> </root> </log4j:configuration>

  • Log4j - servlet web applicationsNishanth Thomas February 29, 2012 at 12:10 PM

    // 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); //--------------------------- } 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) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" > <--configure path according to your requrmts to save log file --> <log4j:configuration> <!--code between <log4j:configuration> </log4j:configuration> --> <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--> <!--package name given in category here com is the package --> <category name="com"> <priority value="DEBUG"/> <appender-ref ref="daoAppender"/> </category> <root> <!-- <appender-ref ref="appAppender" /> <appender-ref ref="daoAppender" /> --> </root> </log4j:configuration>

  • log file locationI'm what I'm May 7, 2012 at 3:19 PM

    Where can i find those log files...in wich path it will be stored??? anyone help me out...

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Log4j

A simple example of log4j for Servlet
A simple example of log4j for Servlet       This Example shows you how to create a log in a Servlet. Description of the code: Logger.getLogger(): Logger class is used 

Tutorials

log4j
; A simple example of log4j for Servlet This Example shows you how....   A simple example of log4j This Example shows...;     log4j example 

Log4J Questions

log4j
log4j  explain about log4j with example ?   Please visit the following link: log4j Tutorials 

JSP Servlet Questions

simple web applications using log4j
simple web applications using log4j  i am getting java.lang.NoClassDefFoundError: org/apache/log4j/Logger and javax.servlet.ServletException: Error instantiating servlet class Sample exceptions when trying to execute. this is my 

Log4j

A simple example of log4j
A simple example of log4j       This Example shows you how to create a log in a program. Description of the code: Logger.getLogger 

Log4j

log4j example
log4j example       This Example shows you how to create a log in a Servlet. Description of the code: Logger.getLogger(): Logger class is used for handling the majority 

Log4j

Log4J Tutorials: Using Log4J in Easy Steps
Architecture A simple example of log4j Use of BasicConfigurator in Log4j logging log4j.xml Example Console Appender in Log4j...Log4J Tutorials: Using Log4J in Easy Steps Log4j is a logging library for Java 

Log4J Questions

hi - Log4J
hi  Please give me the clear definition of Log4J and give me the example 

Log4J Questions

java - Log4J
java  a small servlet application with procedure that how to get logg statements in that servlet application.  Hi friend, Logging Filter Servlet application related u r Problem. Read for more information 

Log4J Questions

jsp - Log4J
path to set servlet programs   What is the path to set servlet programs ?  Use the following path to set servlet programs while using weblogic serverprojectfolder -> WEB-INF -> classes -> MyServlet.java 

Java Interview Questions

log4j - Java Interview Questions
log4j  plz send me example program, using log4j and tel me how to execute that program.(what are the requirements to execute that program 

Log4j

WriterAppender in Log4j
WriterAppender in Log4j     ... in simple file (plain/text) file and to the console also. If we want to write... to a Writer or an OutputStream depending on the user's choice. In our example we have 

Log4J Questions

log4j in web application
log4j in web application  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 

Log4j

Log4J Architecture - Introduction to the Log4J architecture
Log4J Architecture - Introduction to the Log4J architecture... will learn about the architectural component of Log4J. Understanding the architecture of Log4J is important. Three main component of Log4J The architecture 

Java Interview Questions

Log4j
Log4j  what is the use of log4j in realtime?   Please visit the following link: Log4j Tutorials 

Log4J Questions

LOG4J
LOG4J  HOW AND WHERE TO FIND org.prot.appserver.config.Configuration; PACKAGE TO IMPORTIN LOG4J PREOGRAM 

EJB Questions

Simple EJB3.0 - EJB
Simple EJB3.0  Hi friends... I am new user... bean on servlet/jsp. thanQ  Hi Friend, Please visit...://www.roseindia.net/ejb/example-of-statelfulbean.shtml http://www.roseindia.net 

Log4j

Use of BasicConfigurator in Log4j logging
Use of BasicConfigurator in Log4j logging       log4j warning log4j: WARN No appenders could be found for logger (org.activemq.transport.tcp.TcpTransportChannel). log4j: WARN 

Log4J Questions

log4j - Log4J
log4j  could u pls help me out regarding the log4j coding   http://www.javaworld.com/javaworld/jw-12-2004/jw-1220-toolbox.html Thanks 

Hibernate Questions

Simple Example of Hibernate 4
Simple Example of Hibernate 4  Hi, As a new comer in the Hibernate programming, I want simple program of Hibernate to learn the basic concepts... Simple Example More Tutorials for a beginner: Hibernate Tutorials Home page 

Servlets-Tutorials

Java Servlet : Hello World Example
Java Servlet : Hello World Example In this tutorial, we are discussing about Servlet with simple Hello World Example. Java Servlet : A servlet is web...; for writing simple hello world example of servlet. Step1 : Write servlet, a POJO 

Log4J Questions

Log4J - Log4J
Log4J  What is the use of Log4j? where does it use? Log4j is using for debuging ur code. Its does the same thing as System.out/System.err do. i.e to trace out ur program   Log4j is using for debuging ur code.Its does 

Servlets

Simple Counter In Servlet
Simple Counter in Servlet       In this example we are going to know how we can make a program on counter which will keep track how many times the servlet has been 

Log4J Questions

L4j in WebApplication with common logging - Log4J
but it is not working.i am writting follwing code to cinfigure my log4j in my servlet... log4j for Web Application in which common logging api used i tried to configure... the following error :log4j:ERROR Could not instantiate class 

Log4J Questions

log4j - Log4J
log4j  give me explanation about log4j,junit? how i hvae to use them   Hi mamatha, In Log4J, if you log a message at DEBUG level, and the current Appender is set to only log messages of INFO level and above 

Log4j

TTCCLayout in Log4j
TTCCLayout in Log4j       In this part of Log4j tutorial we are going to discuss about... (output message) For example in our output as "16 [main] INFO 

Srping 2.5

Simple Form Controller Example
Example of using Simple Form Controller Page 2 In this page we will see the next steps necessary to run the example code.  Here... it at Example of using Simple Form Controller Step 7: Now we 

JSP Servlet Questions

Simple clarification - JSP-Servlet
Simple clarification  Respected Sir/Madam, Thanks for your immediate response..Sorry for troubling you again and again. So,you told that there is no possibility for getting my alert box in home page. Is there any other 

Enterprise Java

JSF 2.0 Simple Example
This section contain simple Hello World Example using JSF 2.0. 

Programming Tutorials

Simple Ajax Example
 

DMCA.com