Saturday, September 10, 2011

Get Log4j File names dynamically for all registered loggers

Use following code to get the filenames registered for logging in log4j.xml file and use them to do whatever you want. Usually the processes in organization will take longer to get the log files, can you do something useful from this? Think and do it on your own and don't document it ever :)

    Enumeration currentLoggers = Logger.getRootLogger().getLoggerRepository().getCurrentLoggers();
    while (currentLoggers.hasMoreElements()) {
      Logger logger = (Logger)currentLoggers.nextElement();
      System.out.println("Logger : " + logger.getName());
      Enumeration loggerApp = logger.getAllAppenders();
      if (loggerApp.hasMoreElements()) {
        Appender ap = (Appender) loggerApp.nextElement();
        if (ap instanceof FileAppender) {
          FileAppender fileAppender = (FileAppender)ap;
          System.out.println("   FileName : " + fileAppender.getFile());
          System.out.println("Not a file Appender, appender type=" + ap.getClass().getName());

No comments:

Post a Comment