Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot Easy Logging

DZone's Guide to

Spring Boot Easy Logging

What if you enjoy Spring Boot, but hate its packaged logging system? This project should make logging easier, and maybe even enjoyable.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

I've liked the Spring framework since I started using Spring Boot. It is undoubtedly easier to configure and run a Spring project with Spring Boot when you use  spring-boot-starter-logging  without any configuration, Logging is provided out of the box but who is not fed up with seeing this clumsy code?

private static final Log log = LogFactory.getLog(CardService.class);

public Card createCard(User user, String cardNo) {

    if ( log.isDebugEnabled() ) {

        log.debug("Creating a new card for user: " + user.getUsername()
            + " cardNo: " + cardNo);

    }

    return new Card(user, cardNo);

}


It is verbose and some developers even argue that in a web application in which there is a lot of threading, a final static logger shared by all threads may result in bottleneck (I don't know how, by the way).

I decided to create a Spring logger project to alleviates this pain a little.

Instead of the above code, what if I show you this:

@Logger
static org.slf4j.Logger LOGGER;

@Trace(level=LogLevel.DEBUG)
public Card createCard(User user, String cardNo) 
{
   LOGGER.info("Trace annotation will do the job but any other informant log goes here ");
}

It doesn't matter if you declare the log variable static or not, it will work either way although I suggest you remain with static logger, injecting logger per request is somehow slow.

It prints: 

15:09:16.870 [main] INFO org.logeasy.test.TraceTestService - Entering : [TraceTestService] :  testWithParams("test", 1)
15:09:16.870 [main] INFO org.logeasy.test.TraceTestService - TEST WITH PARAMS
15:09:16.870 [main] INFO org.logeasy.test.TraceTestService - Exiting : [TraceTestService] :  testWithParams("test", 1)

If you don't see Exiting : [Class] : methodName(parameterValues) , it means something bad happened or exception is thrown.

Logeasy gest even more exciting after I add Spring Expression Language (SpEL). What if you could code like this:

LOGGER.debug("Creating a new card for user: #{user.username}");


How to Set Up Logeasy

To enable Logeasy, just add a dependency to your Maven project and set up following beans:

@Bean
public TracePostProcessor trace()
{
     return new TracePostProcessor();
}

@Bean
public LoggerPostProcessor log()
{
    return new LoggerPostProcessor();
}

Everything is ready now. Enjoy logging!

To download the source code, please visit the project github.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
spring boot ,java ,logging ,performance ,easy logging

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}