2 Replies Latest reply on Nov 23, 2011 10:52 PM by tc7

    Using log4j on JBossAS7 - log category enumeration

    tc7 Newbie

      We've been happily using log4j on JBoss 4.2.3.GA for some time now. As part of the migration to JBoss-as-7.0.2.Final we've come across a few log4j related issues.

      See my other post regarding the use-parent-handlers attribute and logger.getLevel() returns null here.

       

      This post is regarding logging enumeration.

       

      Previously we were able to enumerate loggers using:

          Logger log = Logger.getLogger(LoggingComponent.class);

          log.getLoggerRepository()

       

      However this now returns null.

       

      I've also tried:

      Enumeration en = java.util.logging.LogManager.getLogManager().getLoggerNames();

      but this returns an empty set.

       

       

      We used this (log category enumeration) within one of our web application admin screens to provide control over log levels without having to revert to JMX or the web console.

       

      Does anyone know how this can be overcome?

        • 1. Re: Using log4j on JBossAS7 - log category enumeration
          James Perkins Master
          Just so you know we are working on better support for log4j emulation with JBoss AS 7.

           

          While that does you no good now, I do have a work-around for you. It's not quite as elegant as log.getLoggerRepostory(), but it should work.

          import java.util.List;
          import org.jboss.logmanager.LogContext;
          
          
          public class LoggerNames {
          
          
              public static List<String> get() {
                  return LogContext.getLogContext().getLoggingMXBean().getLoggerNames();
          
              }
          
          }
          
          

           

          1 of 1 people found this helpful
          • 2. Re: Using log4j on JBossAS7 - log category enumeration
            tc7 Newbie

            Thanks James, that's a workable workaround.

             

            I've used the above to create a method to return a list of log4j loggers (albeit JBoss specific code):

             

                import org.apache.log4j.Logger;

                import org.jboss.logmanager.LogContext;

             

                public List<Logger> getLog4jLoggers()

                {

                    LogContext cxt = LogContext.getLogContext();

                    List<String> names = cxt.getLoggingMXBean().getLoggerNames();

                    List<Logger> loggers = new ArrayList<Logger>(names.size());

                    for (String loggerName : names)

                    {

                        Logger logger = Logger.getLogger(loggerName);

                        if (logger != null)

                        {

                            loggers.add(logger);

                        }

                        else

                        {

                            log.warn("Could not find log4j logger with name ["+loggerName+"].");

                        }

                    }

                    return loggers;

                }