0 Replies Latest reply: May 7, 2007 9:06 PM by 807597 RSS

    Java Logger---can't have more than one?

    807597
      Hi,

      The output from the following code is
      javaLog/$ java LoggerTest
      INFO    info4
      javaLog/$ 
      Can anyone explain why there are only messages from the last logger?

      Thanks,
      Glenn
      www.mines.edu/~gmurray
      import java.util.logging.*;
      
      public class LoggerTest {
      
          public static Logger getLogger(String loggerName, Level level) {
              Logger logger = Logger.getLogger(loggerName);
              logger.setLevel(level);
              logger.setUseParentHandlers(false);
              logger.addHandler(getDefaultHandler(level));
              return logger;
          }
      
          /**
           * Get a default Handler for a certain Level.
           */
          public static Handler getDefaultHandler(Level level) {
              // Get a Formatter for the Handler for this Logger.
              Formatter formatter = new Formatter() {
                      public String format(LogRecord rec) {
                          StringBuilder sb = new StringBuilder();
                          java.util.Formatter fmt = new java.util.Formatter(sb);
                          fmt.format("%-7s", rec.getLevel());
                          sb.append(" ");  
                          sb.append(rec.getMessage());
                          return sb.append("\n").toString();
                      }
                  };
      
              Handler handler = new StreamHandler(System.out, formatter);
              handler.setLevel(level);
      
              return handler;
          }
      
          /**
           * A main() to try out logging.
           */
          public static void main(String args[]) {
              Logger log1 = getLogger("log1", Level.ALL);
              Logger log2 = getLogger("log2", Level.ALL);
              Logger log3 = getLogger("log3", Level.ALL);
              Logger log4 = getLogger("log4", Level.ALL);
      
              log1.info("info1");
              log2.info("info2");
              log3.info("info3");
              log4.info("info4");
          }
      }