Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging best approach with CDI

Creating a new version of my RESTful service architecture using JEE 7, deploying to a Wildfly 9 instance, I was wondering if there is a clever way to create a log system, can you suggest some patterns? Thanks a lot.

like image 288
Marcos J.C Kichel Avatar asked Oct 29 '25 11:10

Marcos J.C Kichel


1 Answers

I am using a simple producer with SLF4J interface for a JavaEE 7 JAX-RS project.

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggerProducer
{

    @Produces
    public Logger getLogger(final InjectionPoint ip)
    {
        return LoggerFactory.getLogger(ip.getMember().getDeclaringClass());
    }
}

The usage is quite convenient by using injection.

@Inject
private Logger logger;

And the implementation can be changed easily without affecting any business classes. As the implementation slf4j-simple is sufficient for me, but if you need more advanced stuff, I would have a look at logback as already suggested or log4j 2.

like image 89
simdevmon Avatar answered Oct 31 '25 12:10

simdevmon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!