HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / RESTEasy / RESTEasy + Tomcat 7 + SLF4J Logging Example

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

RESTEasy Tutorial

  • JAX-RS – Introduction
  • RESTEasy – JBoss
  • RESTEasy – Tomcat
  • RESTEasy – @Path
  • RESTEasy – HATEOAS
  • RESTEasy – SLF4J
  • RESTEasy – Log4j
  • RESTEasy – Download
  • RESTEasy – Upload (MultipartForm)
  • RESTEasy – Upload (HTTPClient)
  • RESTEasy – Custom Validation
  • RESTEasy – Hibernate Validator
  • RESTEasy – ContainerRequestFilter
  • RESTEasy – PreProcessorInterceptor
  • RESTEasy – JAXB XML
  • RESTEasy – Jettison JSON
  • RESTEasy – Jackson JSON
  • RESTEasy – ExceptionMapper

RESTEasy Client APIs

  • RESTEasy – java.net
  • RESTEasy – JAX-RS Client
  • RESTEasy – Apache HttpClient
  • RESTEasy – JavaScript API
  • RESTEasy – ResteasyClientBuilder

RESTEasy Best Practices

  • RESTEasy – Sharing Context Data
  • RESTEasy – Exception Handling
  • RESTEasy – ETag Cache control
  • RESTEasy – GZIP Compression
  • RESTful vs. SOAP

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)