Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Initializing Log4J with Spring

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 520
    Comment on it

    In below Code you have to define the following log4j properties into your log.properties file:-

    log4j.properties

        # Global logging configuration
        log4j.rootLogger = DEBUG, fileout
    
        # Define the file appender
        log4j.appender.fileout=org.apache.log4j.FileAppender
        # Set the name of the file
        log4j.appender.fileout.File=D:\\logInfo.out
    
        # Set the immediate flush to true (default)
        log4j.appender.fileout.ImmediateFlush=true
    
        # Set the threshold to debug mode
        log4j.appender.fileout.Threshold=debug
    
        # Set the append to true, overwrite
        log4j.appender.fileout.Append=true    
    
        #Set the Max File Size
       log4j.appender.fileout.MaxFileSize=100MB
    
        log4j.appender.fileout.MaxBackupIndex=10
        log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
        log4j.appender.fileout.layout.conversionPattern=%d{MM-dd HH:mm:ss.SSS} %5p (%c{1}-%t)(%X{USER}) %m%n
    

    After configuring the log4j.properties file you can run your Spring program .

    Main.java

    package com.evon;
    import com.evon.employee;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.apache.log4j.Logger;
    
    public class Main {
    
       static Logger log = Logger.getLogger(Main.class);
    
       public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
    
          log.info("Start Operation to execute");
    
          Employee obj = (Employee) context.getBean("emp");
    
          emp.getName();
    
          log.info("END the program");
       }
    }
    

    spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="emp" class="com.evon.Employee">
           <property name="name" value="Manish"/>
       </bean>
    
    </beans>
    

    The log file is created name logInfo.out in the location of your local drive D:\logInfo.out

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: