RESTEasy + Tomcat 7 + SLF4J Logging Example

RESTEasy is very flexible when comes to logging support. It can work with log4j, slf4j and java.util.logging also. Algorithm used to decide that which logging framework needs to be used is:

  1. If log4j is in the application’s classpath, log4j will be used
  2. If slf4j is in the application’s classpath, slf4j will be used
  3. java.util.logging is the default if neither log4j or slf4j is in the classpath
  4. If the servlet context param resteasy.logger.type is set to JUL, LOG4J, or SLF4J will override this default behavior

In this post, we will learn to slf4j with RESTEasy when developing application in tomcat server. This post is in continuation to my last post about log4j integration with RESTEasy. Here, I am adding slf4j dependency (JCL binding) and removing log4j dependency. I have not touched the application code to show the flexibility “org.jboss.resteasy.logging.Logger” provides.

Environment used:

  • Tomcat 7
  • SLF4J 1.7.5
  • RESTEasy JAX-RS 2.3.1.GA

Steps to configure SLF4j

1) Include dependency in project

I am adding maven dependencies. You can choose to include jar file if required.

	<!-- SLF4j -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.5</version>
	</dependency> 
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-jcl</artifactId>
		<version>1.7.5</version>
	</dependency>

Also make sure, Commons Logging jar file is present in classpath. If you are using maven for dependency management, you will likely get it included when you will add jax-rs dependency. So, no need to include it separately.

2) Use log statements in API methods

Always use “org.jboss.resteasy.logging.Logger” as it is configured using above given algorithm and thus decouple the logging framework dependency from application code completely. It means that if you later decide to use slf4j instead of log4j, you simply need to put slf4j in runtime classpath and remove log4j from classpath. That’s it !!

import org.jboss.resteasy.logging.Logger;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "user-management")
@Path("/user-management")
public class UserService 
{
	Logger log = Logger.getLogger(UserService.class); 
	
	@GET
    @Path("/users/{id : \d+}")
    public User getUserById(@PathParam("id") Integer id) {
    	log.info("GET API called for id : " + id);
        User user = new User();
        user.setId(id);
        user.setFirstName("demo");
        user.setLastName("user");
        return user;
    }
}

3) Test the logging

Making call to above RESTFul API will log the events like this:

May 13, 2013 11:39:10 AM org.slf4j.impl.JCLLoggerAdapter info
INFO: Adding scanned resource: com.howtodoinjava.service.UserService
May 13, 2013 11:39:10 AM org.slf4j.impl.JCLLoggerAdapter info
INFO: GET API called for id : 10

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.