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/
  • webapp/META-INF/
  • webapp/WEB-INF/web.xml
  • webapp/index.jsp


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
public class LoggingConfig {

public LoggingConfig() {}

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


// LoggingController

package controllers;

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
public class LoggingController {
LoggingServiceInterface loggingService;

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

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
public class LoggingService implements LoggingServiceInterface {
private static final Logger logger = Logger.getLogger(LoggingService.class);

public LoggingService() {}

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


#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


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s