While writing java programs to print the line developer uses System.out.println then application is effective. But if developer wants to add more printing lines, it takes more time, to remove this strategy Log4J will consists of control flags.Control flags contains OFF and ALL, by using this developer can add lines to the application depends on the requirement of clients. Elements of Spring Log4J as follows:
Loggers: In-charge of accessing information.
Appenders: In charge of distributing logging data to different chose destinations.
Layouts: In charge of erasing data.
Example for Log4J with explanation as follows
Features
Features of log4J are as follows:
Spring Log4J is thread safe.
It avoids the writing of most System.out.println in code.
Underpins internationalization.
Log4J handles Java Exceptions from the begin.
Log4J with Spring
Description
Spring Log4J is simple to implement it's functionality inside the Spring framework. Before proceeding with Log4J, developer should have Log4J installation in the system. The installation of Log4J as shown below.
Log4J installation link
Example
Example of Log4J with eclipse is as shown below. Before proceeding with Log4J application developer needs to have library files and property files to set up in IDE.
HelloWorld.java
[java]package splessons;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}[/java]
Here just created the variable message and performed SET and GET methods.
MainApp.java
[java]package splessons;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.log4j.Logger;
public class MainApp {
static Logger log = Logger.getLogger(MainApp.class.getName());
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
log.info("Going to create HelloWord Obj");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
log.info("Exiting the program");
}
}[/java]
The Application Context is spring's more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.
Beans.xml
[java]<?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="helloWorld" class="Splessons.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>[/java]
Here message has been passed that is Hello World.
log4j.properties
[java]# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout[/java]
After creation of source and bean configuration files, run the program.This will print the output in Eclipse console as follows.
[java]Your Message : Hello World![/java]
Now check C:\\ drive, log.out file is found with various log messages as follows:
[java];!-- initialization log messages --
Going to create HelloWord Obj
Returning cached instance of singleton bean 'helloWorld'
Exiting the program[/java]
Summary
Key Points
Spring Log4J can be used to write less code in JAVA applications.
Spring Log4J is a Java SW library that has practical experience in logging.