log4j

log4j

explain about log4j with example ?

View Answers

October 28, 2011 at 10:47 AM

Please visit the following link:

log4j Tutorials


January 9, 2012 at 5:08 PM

In large web applications, idea to write on console using System.out.println("xx"); is not good as it's become heavy to write on console. Insteda a new concept known as 'log4j' has been introduced by java in which instead of writing on console, we can write it in any file which can be readable.

For this puspose, you need to have log4j.1.2.15jar.


February 29, 2012 at 11:52 AM

program for servlet log4j configured in jboss application

// Crerated By NISHANTH THOMAS - In-Solutions 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 toplace log4j.xml file there  and       
               // get path by the following;

      String strPath =  request.getRealPath("config");

     //change log4j.xml file as give name like  shown log4jexp and configure path ;

      DOMConfigurator.configure(strPath+"/"+"log4jexp.xml");


  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 now configuration of log4j.xml

Lo4j.xml here we renamed to log4jexp.xml to avoid conflicts with server log4j.xml file ,place log4jexp.xml in config folder ( here I used)


<--configure path according to your requrmts to save log file -->

<!--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-->
<category name="com">     
    <priority value="DEBUG"/>     
    <appender-ref ref="daoAppender"/>       
</category>    

<root>   
    <!--     
    <appender-ref ref="appAppender" />   
    <appender-ref ref="daoAppender" />   
    -->    
</root>









Related Tutorials/Questions & Answers:
log4j
log4j  explain about log4j with example ?   Please visit the following link: log4j Tutorials
Log4j
Log4j  what is the use of log4j in realtime?   Please visit the following link: Log4j Tutorials
Advertisements
LOG4J
LOG4J  HOW AND WHERE TO FIND org.prot.appserver.config.Configuration; PACKAGE TO IMPORTIN LOG4J PREOGRAM
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
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
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
Version of log4j>log4j dependency
List of Version of log4j>log4j dependency
Configuring Log4J
Configuring Log4J  Is it possible to configure xml file in such a way that it has different logging levels for diferent packages in java through Log4J
Log4j error - Log4J
Log4j error  Error while running tomcat server. SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener java.lang.NoClassDefFoundError: org
hi - Log4J
hi  Please give me the clear definition of Log4J and give me the example
java - Log4J
java  Q What is the real use for log4J?  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorials/log4j/index.shtml Thanks
Artifacts of log4j
List of Artifacts of log4j maven depenency
Hibernate log4j
Hibernate log4j  Hi, when i run the FirstExample.java given in the tutorial, i got the right output, what my doubt is from where i am getting the log4j messages, like log4j:WARN no appenders could be found for logger
java - Log4J
regarding "LOG4J" and where these are exactly used in realtime application... as posible.   Hi Friend, Log4j is a logging package to help..., an OutputStream, a java.io.Writer etc. Using Log4j it is possible to enable logging
Maven Repository/Dependency: log4j | log4j
Maven Repository/Dependency of Group ID log4j and Artifact ID log4j. Latest version of log4j:log4j dependencies. # Version Release Date 1 1.2.15 30 Aug 2007
Steps to log4j configuration in Struts2
Steps to log4j configuration in Struts2  Hi... any tel me each step for configure log4j in struts2 application
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
Maven Dependency log4j >> 1.2.12
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.12 in your project
Maven Dependency log4j >> 1.2.15
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.15 in your project
Maven Dependency log4j >> 1.2.4
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.4 in your project
Maven Dependency log4j >> 1.2.7
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.7 in your project
Maven Dependency log4j >> 1.1.3
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.1.3 in your project
Maven Dependency log4j >> 1.2.11
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.11 in your project
Maven Dependency log4j >> 1.2.13
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.13 in your project
Maven Dependency log4j >> 1.2.14
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.14 in your project
Maven Dependency log4j >> 1.2.16
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.16 in your project
Maven Dependency log4j >> 1.2.17
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.17 in your project
Maven Dependency log4j >> 1.2.5
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.5 in your project
Maven Dependency log4j >> 1.2.6
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.6 in your project
Maven Dependency log4j >> 1.2.8
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.8 in your project
Maven Dependency log4j >> 1.2.9
You should include the dependency code given in this page to add Maven Dependency of log4j >> log4j version1.2.9 in your project
Version of log4j>apache-log4j-extras dependency
List of Version of log4j>apache-log4j-extras dependency
System Error messge - Log4J
System Error messge  Hi I am implementing log4j to my application I am getting this error message ?log4j: WARN No appenders could... Please initialize the log4j system properly.? I don?t have any Message
Difficulty in plugging log4J with Hibernate.
Difficulty in plugging log4J with Hibernate.  I am using Hibernate 3.6. Followed your all instruction of plugging log4j with my hibernate code... HandlingWorker, it uses SLF4j and does not detect log4J. What could
Updating log4j programatically
Updating log4j programatically  Is there a ready made utility which I can use to edit the log4j.xml programmatically
java log4j - Java Beginners
java log4j  which is the best site to prepare log4j in java since i am new to it  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorials/log4j/index.shtml Hope that it will be helpful
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 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 Log4JADS_TO_REPLACE_1
remove logging - Log4J
remove logging  How to remove the logging from crystal reports only? log4j hss been used for logging purpose.Thanks and regards vishal  hi friend,Crystal Reports XI is the definitive ?out of the box&#65533
Log4J Tutorials: Using Log4J in Easy Steps
Log4J Tutorials: Using Log4J in Easy Steps Log4j is a logging library for Java... Software Foundation. Log4j is one of best Java logging frameworks used... statements into the code. Log4j has been designed with three objectives to provide
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
Maven dependency for log4j - log4j version 1.2.15 is released. Learn to use log4j version 1.2.15 in Maven based Java projects
Maven dependency for  log4j  - Version 1.2.15 of log4j released The developers of   log4j - log4j project have released the latest version of this library on 30 Aug 2007, the released version of  log4j - log4j
L4j in WebApplication with common logging - Log4J
configure log4j for WebApplication  configure log4j for WebApplication  Hello,I seems that log4j.properties file is missing in your project.So create log4j.properties file with following content:# Default Logging
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
JSP cannot log to FileAppender - Log4J
Log4j. At the moment, it logs fine to console when run from class main method... on log4j visit to : http://www.roseindia.net/tutorials/log4j/ http://www.roseindia.net/tutorialsearch/?t=log4j Thanks
Compression of rolled-over files using log4j
Compression of rolled-over files using log4j  How can I use log4j to rollover the log files on hourly basis and compress the rolled over files
L4j in WebApplication with common logging - Log4J
log4j for Web Application in which common logging api used i tried to configure... but it is not working.i am writting follwing code to cinfigure my log4j in my servlet... the following error :log4j:ERROR Could not instantiate class
Hibernate Logging using Log4j
This section contains detail description of how you can use Log4j for logging configuration
Log4j 2.15.0 maven dependency - Fixing log4j zero day vulnerability
What is Log4j 2.15.0 Maven Dependency and how to fix log4j zero day vulnerability? Log4j is one of the most popular logging library in Java project and it is being used by millions of developer in the project. Recently Log4j zero day
TTCCLayout in Log4j
TTCCLayout in Log4j       In this part of Log4j tutorial we are going to discuss about the TTCCLayout which is extending DateLayout. It is another layout format for representing

Ads