Code

Spring Java Config for Log4J

Applications write interesting information to its log. For this purpose, there is a well known library named, Log4J. It is used in many iterations, including the ability to write custom logs that are size restricted, so that once the size limit is reached, a new log is created. In addition, there are other tools that make perusing a log nice, Splunk is one of them.

The following code will provide you with a basic starting point for a Restful Web Service using the Spring Framework.

The artifacts are as follows:

  • configuration.LoggingConfig
  • controllers.LoggingController
  • controllers.LoggingServiceInterface
  • controllers.LoggingService
  • resources/log4j.properties
  • webapp/META-INF/log4j.properties
  • webapp/WEB-INF/web.xml
  • webapp/index.jsp

BUILDING THE PROJECT

Create a webapp maven project with the name LoggingConfig.


// LoggingConfig

package configuration;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
* Created by david.king
*/
@Configuration
@EnableWebMvc
@ComponentScan("controllers")
public class LoggingConfig {

public LoggingConfig() {}

@Bean
public ConsoleAppender consoleAppender() {
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setThreshold(Level.ALL);
PatternLayout patternLayout = new PatternLayout();
patternLayout.setConversionPattern("%d %-5p [%c{1}] %m %n");
consoleAppender.setLayout(patternLayout);
return consoleAppender;
}

}

// LoggingController

package controllers;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by david.king
*/
@RestController
public class LoggingController {
@Autowired
LoggingServiceInterface loggingService;

@RequestMapping(value = "/logging", produces = MediaType.APPLICATION_JSON_VALUE)
public String produceLogging() {
for (int i = 0; i < 5; i++) {
loggingService.performLogging();
}

String success = "Success";
return new Gson().toJson(success);
}

}

// LoggingServiceInterface

package controllers;

/**
* Created by david.king
*/
public interface LoggingServiceInterface {
public void performLogging();
}

// LoggingService

package controllers;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
* Created by david.king
*/
@Service
public class LoggingService implements LoggingServiceInterface {
private static final Logger logger = Logger.getLogger(LoggingService.class);

public LoggingService() {}

@Override
public void performLogging() {
logger.info("I am performing some logging, you should see this message in the logs.");
}
}

LOG4J PROPERTIES

#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n

log4j.rootLogger = INFO, consoleAppender

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s