Tuesday, March 22, 2016

Spring + Maven + Log4j for HelloWorld

Log4j is very useful for developer to identify code flow logging information.

In this project I have used below tools and technologies.

1. Eclipse Indigo
2. Spring
3. Maven
4. Log4j

In previous section I have simply developed helloworld program with Spring and Maven. Here I am going to add just log4j configuration and its usage.

Since our application created through maven, we should add log4j dependency in pom.xml. I have mentioned group id , artifact id, scope and version for log4j. Here runtime scope indicates dependency is required at execution time only, not on compilation time.



  4.0.0
  com.test
  helloworld
  0.0.1-SNAPSHOT
  
  
  
   org.springframework
   spring-core
   ${spring.version}
  
  
   org.springframework
   spring-context
   ${spring.version}
  
  
   log4j
   log4j
   runtime
   ${log4j.version}
  
 
 
 
  3.2.3.RELEASE
  1.2.17
 
 


Create a log4j.properties file under src/main/resources. The below is log4j property file.

# LOG4J configuration

#You can set here different level like ALL,INFO,WARN etc.,
log4j.rootLogger=INFO, TestConsoleAppender, TestFileAppeneder
 
log4j.appender.TestConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.TestConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.TestConsoleAppender.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
 
log4j.appender.TestFileAppeneder=org.apache.log4j.FileAppender
log4j.appender.TestFileAppeneder.File=C:/logs/test.log
log4j.appender.TestFileAppeneder.layout=org.apache.log4j.PatternLayout
log4j.appender.TestFileAppeneder.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n

In above property file, I have configured root logger in INFO level with ConsoleAppender and FileAppender.

1. Added TestConsoleAppender for the purpose of console output to display in console tab.

2. Added TestFileAppeneder for the purpose of create a log file in your local drive. Suppose if you have web application, there is good way to add your log file with your web server. Just you need to change the path location to web server directory.

Finally, in our java code I added sayHello method that have all logging level possibilities.(TRACE,DEBUG,INFO,WARN,ERROR,FATAL).

Based on your log4j configuration only, log will be printed. For example, if you set rootlogger as ALL all log levels will be printed. But in our code I have set to INFO, so INFO,WARN, ERROR and FATAL will be displayed based on its hierarchy level. TRACE and DEBUG will not be printed.

Log4j Hierarchy Order

  • OFF
  • FATAL
  • ERROR
  • WARN
  • INFO
  • DEBUG
  • TRACE
  • ALL


For more info please visit Log4J levels details

Java code is below. 

package com.test.helloworld;

import org.apache.log4j.Logger;
 
//@Service("helloWorld")
public class HelloService {
 
 private static final Logger LOGGER = Logger.getLogger(HelloService.class);
 private String name;
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String sayHello() {
  
  LOGGER.trace("Trace Message!");
  LOGGER.debug("Debug Message!");
  LOGGER.info("Info Message!");
  LOGGER.warn("Warn Message!");
  LOGGER.error("Error Message!");
  LOGGER.fatal("Fatal Message!");
  return "Hello ! Spring + Maven + Log4J Tested by " + name;
 }
}

Now we have to run HelloTest java code, the below out put you able to see in console tab, due to adding ConsoleAppender.

Note:  In my previous post, you can find HelloTest and application context xml code.

Spring_maven_log4j


At the same time, log file also generated at C:\logs location, since you provided in FileAppender configuration in log4j property file.

log4j

Thats All. Happy Learning!!!

1 comment:

  1. very nice information......thank you.......

    ReplyDelete