【问题标题】:what's is the best way to initialize log4j with EJB 3.0?用 EJB 3.0 初始化 log4j 的最佳方法是什么?
【发布时间】:2012-02-08 17:08:39
【问题描述】:

我有一个只有 EJB (EJB3.0) 并且没有 WAR 模块的 EAR,服务器是基于 linux 的 JBOSS 4.3。

我想用服务器外部的 log4j.properties 文件初始化 LOG4J,并使用 slf4j 作为外观。

初始化我的 log4j 的最佳方法是什么?

提前致谢

【问题讨论】:

    标签: java log4j ejb-3.0 slf4j java-ee-5


    【解决方案1】:

    我已经在 Startup 中初始化并将 log4j 放在 META-INF 文件夹中。并且只使用没有 Slf4j 的 log4j。 (EJB 3.1)

    我希望下面的代码有所帮助;

    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();
            }
        }
    }
    

    【讨论】:

    • 这个想法对 EJB 3.0 没有用处。
    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2019-07-24
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多