3 Replies Latest reply on Oct 23, 2014 7:20 PM by jamezp

    jboss eap 6.1 - Log4j's configureAndWatch method not working

    rajajboss

      Dear All,

       

      My requirement is I need to change the LOG level at RUNTIME WITHOUT restarting jboss eap 6.1 server.

       

      Below code is working when I run as a standalone application.

      But the same code is NOT working in JBOSS.

      When I changed the debug level at run time, it is not reflecting.

       

       

      Client code:

       

      view plainprint?
      1. import java.io.IOException;  
      2. public class Log4jAppTest {
      3.     static LoggerManager logger = LoggerManager.getLogger(Log4jAppTest.class);
      4.     public static void main(String[] args) throws IOException {
      5.         while(true){
      6.         
      7.             logger.debug(logger.getBundle().getString("debug"));
      8.             logger.info(logger.getBundle().getString("info"));
      9.             logger.warn(logger.getBundle().getString("warn"));
      10.             logger.error(logger.getBundle().getString("error"));
      11.             logger.fatal(logger.getBundle().getString("fatal"));
      12.             try {
      13.                 Thread.sleep(5000);
      14.             } catch (InterruptedException e) {
      15.                 // TODO Auto-generated catch block
      16.                 e.printStackTrace();
      17.             }
      18.             }
      19.     }
      20.     
      21.     }

       

       

      LoggerManager.java

      --------------------------------

      1. import java.io.File;  
      2. import java.io.FileInputStream; 
      3. import java.io.IOException; 
      4. import java.net.URL; 
      5. import java.net.URLClassLoader; 
      6. import java.nio.file.Files; 
      7. import java.nio.file.LinkOption; 
      8. import java.nio.file.Path; 
      9. import java.nio.file.Paths; 
      10. import java.util.Locale; 
      11. import java.util.Properties; 
      12. import java.util.ResourceBundle; 
      13. import org.apache.log4j.Logger; 
      14. import org.apache.log4j.PropertyConfigurator; 
      15. public class LoggerManager { 
      16.     private ResourceBundle bundle = null
      17.     private static LoggerManager instance; 
      18.     private Logger logger = null
      19.     private LoggerManager(String log4jPath, String bundlePath, 
      20.             String bundleFileName, Locale locale, Logger logger) 
      21.             throws IOException { 
      22.         PropertyConfigurator.configureAndWatch(log4jPath, 5000); 
      23.         setLogger(logger); 
      24.         File file = new File(bundlePath); 
      25.         URL[] urls = { file.toURI().toURL() }; 
      26.         ClassLoader loader = new URLClassLoader(urls); 
      27.         bundle = ResourceBundle.getBundle(bundleFileName, locale, loader); 
      28.     } 
      29.     public static LoggerManager getLogger(Class<?> class1) { 
      30.         try
      31.             Properties props = LoggerManager 
      32.                     .loadMDFProperties("C:\\testcode\\MavenWeb\\src\\main\\resources\\XXX.properties"); 
      33.             if (instance == null) { 
      34.                 instance = new LoggerManager( 
      35.                         props.getProperty("log4jFileLocation"), 
      36.                         props.getProperty("ResourceBundlePath"), "MDFBundle"
      37.                         new Locale(props.getProperty("i18n")), 
      38.                         Logger.getLogger(class1)); 
      39.             } 
      40.         } catch (IOException ex) { 
      41.             ex.printStackTrace(); 
      42.         } 
      43.         return instance; 
      44.     } 
      45.     /**
      46.      * Load properties. 
      47.      *  
      48.      * @param propertiesFile 
      49.      *            path to properties file 
      50.      * @return filled in properties object 
      51.      * @throws IOException 
      52.      *             if something goes wrong 
      53.      */ 
      54.     @SuppressWarnings("unused"
      55.     public static Properties loadMDFProperties(final String propertiesFile) 
      56.             throws IOException { 
      57.         // Properties object we are going to fill up.
      58.         Properties properties = new Properties(); 
      59.         // If file exists as an absolute path, load as input stream.
      60.         final Path path = Paths.get(propertiesFile); 
      61.         if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { 
      62.             properties.load(new FileInputStream(propertiesFile)); 
      63.         } else
      64.             // Otherwise, use resource as stream.
      65.             properties.load(LoggerManager.class.getClassLoader() 
      66.                     .getResourceAsStream(propertiesFile)); 
      67.         } 
      68.         return properties; 
      69.     } 
      70.     public ResourceBundle getBundle() { 
      71.         return bundle; 
      72.     } 
      73.     public void setBundle(ResourceBundle bundle) { 
      74.         this.bundle = bundle; 
      75.     } 
      76.     public Logger getLogger() { 
      77.         return logger; 
      78.     } 
      79.     public void setLogger(Logger logger) { 
      80.         this.logger = logger; 
      81.     } 
      82.     public void trace(Object o) { 
      83.         logger.trace(o); 
      84.     } 
      85.     public void trace(Object o, Throwable e) { 
      86.         logger.trace(o, e); 
      87.     } 
      88.     public void debug(Object o) { 
      89.         logger.debug(o); 
      90.     } 
      91.     public void debug(Object o, Throwable e) { 
      92.         logger.debug(o, e); 
      93.     } 
      94.     public void info(Object o) { 
      95.         logger.info(o); 
      96.     } 
      97.     public void info(Object o, Throwable e) { 
      98.         logger.info(o, e); 
      99.     } 
      100.     public void warn(Object o) { 
      101.         logger.warn(o); 
      102.     } 
      103.     public void warn(Object o, Throwable e) { 
      104.         logger.warn(o, e); 
      105.     } 
      106.     public void error(Object o) { 
      107.         logger.error(o); 
      108.     } 
      109.     public void error(Object o, Throwable e) { 
      110.         logger.error(o, e); 
      111.     } 
      112.     public void fatal(Object o) { 
      113.         logger.fatal(o); 
      114.     } 
      115.     public void fatal(Object o, Throwable e) { 
      116.         logger.fatal(o, e); 
      117.     } 
      118. }

      Please help me on this.