Saturday, February 8, 2014

How To Enable Logging Feature in Our Web Application?

One of the ways That we can Enable Logging Functionality in Our Web Applications is as Follows.....
                                                                             
Firstly

we need a file that has all properties related to Logging functionality
Ex:


# Root logger option  
      
   # log4j.rootLogger=INFO, file, stdout  
    log4j.rootLogger=ALL, file, stdout 
      
    # Direct log messages to a log file  
    log4j.appender.file=org.apache.log4j.RollingFileAppender  
    log4j.appender.file.File=/home/imadas/simplrdapilogs/logingFile.log

     #The Maximum size of a backup file.
    log4j.appender.file.MaxFileSize=1MB  

    #To maintain only one backup file.
    log4j.appender.file.MaxBackupIndex=1  

    log4j.appender.file.layout=org.apache.log4j.PatternLayout  
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n  
    
    #=======
    # Set the immediate flush to true (default)
    log4j.appender.FILE.ImmediateFlush=true

    # Set the threshold to debug mode
    log4j.appender.FILE.Threshold=debug
    #======
      
    # Direct log messages to stdout  
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    log4j.appender.stdout.Target=System.out  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n  



Now Configure the above file in application "web.xml"  and Logger Listener  like:

<!-- Log4j Configuration -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- End -->


Now we can use the Logging Feature in Our Application

Logger logger=new Logger.getLogger(The class); 


 Now we can use as we like

logger.info("...");
logger.debug,
logger.error.....etc.


0 comments:

Post a Comment