Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

09 August 2017

Log4J

Log4J is the API with which we can add Log information for a application.
Though in development environments,we can have the tools to debug the code directly,
In production environment,Logs are the only source of information to analyse any issue/bug.
Logs can be used to debug,provided the information is logged in a proper way.

Simple Hello World Spring application with Logger.

There are Different log levels provided by API,and they can be configured in log4j.xml.
1.DEBUG
2.INFO
3.WARN
4.ERROR
5.FATAL
6.OFF

log4j_log_levels


Suppose a log level INFO is enabled , all the levels below INFO will be enabled. i.e. levels
WARN,ERROR,FATAL,OFF.



Log4j Logger implementation with example


Log4j.xml

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    
    <appender name="apndr" class="org.apache.log4j.FileAppender">
        <param name="file" value="logerTesterABC.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
        </layout>
    </appender>
    
    <appender name="apndr1" class="org.apache.log4j.FileAppender">
        <param name="file" value="logerTesterABD.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
        </layout>
    </appender>
    
    <logger name="become.java9pro.logger" additivity="true">
        <level value="debug"/>
        <appender-ref ref="apndr1"/>
    </logger>

    <root>
        <priority value="trace"></priority>
        <appender-ref ref="apndr"/>
    </root>

</log4j:configuration>

Context.xml

<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
xmlns:context="http://www.springframework.org/schema/context"       
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd      
 http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd ">


    <context:component-scan base-package="become.java9pro.logger"/>


    <bean id="java9ProLoggerTest" class="become.java9pro.logger.Java9ProLoggerTest">
           <property name="myName" value="Java9Pro"/>
    </bean>

 </beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>prj-group</groupId>
    <artifactId>prj-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>


web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"        
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


</web-app>

Java Class Using Logger


package become.java9pro.logger;


import org.apache.log4j.Logger;

public class Java9ProLoggerTest {
    Logger logger = Logger.getLogger(Java9ProLoggerTest.class);
    String myName;
    public void setMyName(String myName) {
        logger.info("myName is "+ myName);
        this.myName = myName;
    }
}



Output will be put in logerTesterABD.log file with  "myName is Java9Pro"


Project Structure