2012-12-14 174 views
0

java代碼log4j配置

public static synchronized void init(String log4jXMLPath) //throws ServletException 
    { 
     try 
     { 
     if (!initialized) // set the global RepositorySelector 
     { 
      defaultRepository = LogManager.getLoggerRepository(); 
      RepositorySelector theSelector = new AppRepositorySelector(); 
      **LogManager.setRepositorySelector(theSelector, guard);** 
      initialized = true; 
     } 
     Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.DEBUG)); 
     //loadLog4JConfig(servletContext, hierarchy); 
     loadLog4JConfig(hierarchy, log4jXMLPath); 
     ClassLoader loader = Thread.currentThread().getContextClassLoader(); 
     repositories.put(loader, hierarchy); 
     } 
     catch (Exception e) 
     { 

     e.printStackTrace(); 
     } 
    } 

LogManager.setRepositorySelector(theSelector,後衛);這條線在Java代碼中拋出一個錯誤

堆棧跟蹤

18:17:11,189 ERROR [stderr] (MSC service thread 1-2) java.lang.IllegalArgumentException: Attempted to reset the LoggerFactory without possessing the guard. 
18:17:11,190 ERROR [stderr] (MSC service thread 1-2) at org.apache.log4j.LogManager.setRepositorySelector(LogManager.java:164) 
18:17:11,191 ERROR [stderr] (MSC service thread 1-2) at com.mportal.logger.api.AppRepositorySelector.init(AppRepositorySelector.java:73) 
18:17:11,192 ERROR [stderr] (MSC service thread 1-2) at com.mportal.logger.api.MPLoggerImpl.loadLogInstance(MPLoggerImpl.java:64) 
18:17:11,192 ERROR [stderr] (MSC service thread 1-2) at com.mportal.logger.api.MPLoggerImpl.<init>(MPLoggerImpl.java:38) 
18:17:11,192 ERROR [stderr] (MSC service thread 1-2) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
18:17:11,193 ERROR [stderr] (MSC service thread 1-2) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
18:17:11,194 ERROR [stderr] (MSC service thread 1-2) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
18:17:11,194 ERROR [stderr] (MSC service thread 1-2) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 

如何解決這個問題呢?請給解答。

+1

1)正確設置代碼的格式2)解釋發生這種錯誤的上下文3)只是傾銷堆棧跟蹤而無需解釋將無法爲任何人提供幫助 –

+0

請參閱http://stackoverflow.com/ questions/9584787/using-log4j-with-jboss-7-1您可能必須從JBoss中排除log4j依賴 –

回答

1
LogManager.setRepositorySelector(theSelector, guard); 

您只能使用一個後衛,用不同的保護的對象將導致錯誤observerd:

java.lang.IllegalArgumentException: Attempted to reset the LoggerFactory without possessing the guard. 

應該可以通過null作爲參數傳遞給函數。或者乾脆參照最初的集合後衛。

的文檔狀態:

Initally保護爲空。如果守衛爲空,則調用此方法設置記錄器工廠和守衛。以下調用將拋出IllegalArgumentException,除非先前設置的警戒作爲第二個參數傳遞。