Friday 17 March 2017

Stepwise how to implement spring logging with log4j

Step wise how to implement spring logging with log4j

Logging is very important for any application its give us insider information about application about its background and what happens in the application at debug and run time.

Logging is broken into three major pieces: the Logger, Formatter and the Handler (Appender). The Logger is responsible for capturing the message to be logged along with certain metadata and passing it to the logging framework. After receiving the message, the framework calls the Formatter with the message. The Formatter formats it for output. The framework then hands the formatted message to the appropriate Appender for disposition. This might include a console display, writing to disk, appending to a database, or email.


I will discuss on this post, how LOG4J property file to write output on log file and show on console as well.

Step 1: Download log4j.x.x.x.jar and copy it in lib folder of your project. If you are using Maven than add log4j dependency to your pom.xml like this.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>apache-log4j-extras</artifactId>
    <version>1.2.17</version>
</dependency>

If you are using Gradle than add log4j dependency to your build.gradle like this.

//LOG4J
    compile 'log4j:log4j:1.2.17'
    compile 'log4j:apache-log4j-extras:1.2.17'

Step 2: Add one of the configuration file either property file or xml file to your project and make sure the file is included in classpath.

Stepwise how to implement spring logging with log4j

# LOG4J configuration
log4j.rootLogger=INFO, CONSOLE, FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.logger.org.hibernate =WARN

log4j.appender.FILE=org.apache.log4j.rolling.RollingFileAppender 
log4j.appender.FILE.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy 
log4j.appender.FILE.rollingPolicy.maxIndex=2 
log4j.appender.FILE.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy 
#log4j.appender.FILE.triggeringPolicy.MaxFileSize=5000000
log4j.appender.FILE.triggeringPolicy.MaxFileSize=50000
log4j.appender.FILE.rollingPolicy.FileNamePattern=D:/Error-%i.log 
log4j.appender.FILE.rollingPolicy.ActiveFileName=D:/Error.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.ConversionPattern=%d %-5p - %c %x %m%n

Step 3: Add required logging code to your project classes where you want to enable logging and store activities.
private static final Logger logger = Logger.getLogger(Home.class); // register class with logger
logger.info("Message info");  // implement required logging method

1.    package com.engineeernitesh.controller;

2.    import org.apache.log4j.BasicConfigurator;
3.    import org.apache.log4j.Logger;
4.    import org.springframework.stereotype.Controller;
5.    import org.springframework.web.bind.annotation.RequestMapping;
6.    import org.springframework.web.servlet.ModelAndView;

7.    @Controller
8.    public class Test {
9.    User user = new User();

10.  private static final Logger logger = Logger.getLogger(Test.class);

11.  @RequestMapping("/hello")
12.  public ModelAndView hello(){
13.       BasicConfigurator.configure();
14.       logger.debug("Debug..");
15.       logger.info("Info.."+user);
16.       logger.error("Error.."+user);
17.       logger.fatal("Fatal"+user);
18.       logger.trace("trace");
19.       return new ModelAndView("hello","message","HelloWorld");
20.      }
21.  }



Related Post:

How to turning off hibernate logging console output

How can I disable application context info in log or console

No comments:

Post a Comment