what's is the best way to initialize log4j with EJB 3.0?

1.6k views Asked by At

I have an EAR who has only EJBs (EJB3.0) and with out a WAR module, the server is JBOSS 4.3 over linux.

I want to initialize LOG4J with a log4j.properties file outside the server and use slf4j as facade.

What is the best way to initialize my log4j?

Thanks in advance

1

There are 1 answers

1
Yusuf K. On

I have initialized in Startup and put log4j in META-INF folder. And just use log4j without Slf4j. (EJB 3.1)

I hope below code helps;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

@Singleton
@Startup
public class InitClass { 

@PostConstruct
private void log4jIlkle() {
        String log4jProp = yourlog4jPath;//My path definition maybe put more flexible path: "../applications/DeployName/META-INF/log4j.properties";
        File logFile = new File(log4jProp);
        if (logFile.exists()) {
            System.out.println("Log4j init: " + log4jProp);
            PropertyConfigurator.configure(log4jProp);
        }
        else {
            System.err.println("*** " + log4jProp + " file not found, initialize with default settings");
            BasicConfigurator.configure();
        }
    }
}