簡體   English   中英

如何在EJB模塊中使用log4j?

[英]How to use log4j in EJB Module?

我有一個使用Netbeans和glassfish 3.1.2的EJB模塊。 我需要實現日志,但是我不知道如何在EJB中配置log4j。 部署時出現此問題:

SEVERE: log4j:ERROR Ignoring configuration file [file:/C:/Users/Luis Carlos/AppData/Roaming/NetBeans/7.2/config/GF3/domain1/config/log4j.properties].
SEVERE: log4j:WARN No appenders could be found for logger (beans.RutasBean).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

它在glassfish服務器控制台中顯示。

這是log4j.properties文件:

log4j.rootLogger=DEBUG, FILE
log4j.logger.myapp=DEBUG
log4j.appender.FILE=org.apache.log4j.RollingFileAppender 
log4j.appender.FILE.File=/space/gfv3/v3setup/GlassFish3/GlassFish/domains/domain1/logs/log4j.log
log4j.appender.FILE.MaxFileSize=100KB 
log4j.appender.FILE.MaxBackupIndex=1 
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.ConversionPattern=%d{DATE} %-5p %c{1} : %m%n 

我試過 ,它說我必須在glassfish的服務器管理員中配置這樣的屬性文件-Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/log4j.properties但是沒有不工作

我有一個Singleton Session Bean,並且在啟動方法中配置了日志,因此根據這個我在Bean中具有:

    private Logger log;
private static final String LOG4J_PROPERTIES = "/log4j.properties";

@PostConstruct
public void startup(){
    URL url = this.getClass().getResource(LOG4J_PROPERTIES);
    if(url == null || url.getFile().length() == 0) {
    throw new RuntimeException("Log4j config file "+ LOG4J_PROPERTIES +" not found");
}
    PropertyConfigurator.configure(url);
    log = Logger.getLogger(this.getClass());
    log.info("Starting application");
    em = Persistence.createEntityManagerFactory("BuscaBus-ejbPU");
    rutaController = new RutaJpaController(ut, em);
    actualizarCoordenadasRutas();
    ...
}

但仍然出現相同的錯誤

如何在EJB環境中配置log4j? 謝謝

請執行下列操作:

  1. 從服務器中刪除-Dlog4j.configuration=
  2. 刪除可能放置在C:/Users/Luis Carlos/AppData/Roaming/NetBeans/7.2/config/GF3/domain1/config所有log4j.properties
  3. 刪除以下代碼:

     URL url = this.getClass().getResource(LOG4J_PROPERTIES); if(url == null || url.getFile().length() == 0) { throw new RuntimeException("Log4j config file "+ LOG4J_PROPERTIES +" not found"); 
  4. 刪除此內容: PropertyConfigurator.configure(url);

完成上述步驟后,您的代碼就可以讓Log4J從classpath自動加載其log4j.properties文件。 根據我的經驗,這是配置Log4J的最靈活的方法。 剩下要做的就是確保log4j.properties確實存在於類路徑中的某個位置。

您有以下選擇:

  1. log4j.properties文件放置在EJB模塊的默認包中。 這樣, log4j.properties文件將與您的應用程序一起打包。 這樣做有優點也有缺點,但這是一個不同的故事(很可能屬於不同的問答環節)。
  2. log4j.properties放在文件系統上的某個目錄中,然后將該目錄添加到服務器的類路徑中。 請遵循GlassFish文檔中有關如何執行此操作的信息。

如果您堅持要使用URL引用log4j.properties文件,請恢復-Dlog4j.configuration=指令。 確保您提供的指定log4j.properties文件的URL 實際上存在 另外,指定-Dlog4j.debug=true 它將為您提供Log4J本身的調試信息,並准確告訴您它在做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM