View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd;
    5
    6   import java.util.logging.Handler;
    7   import java.util.logging.Level;
    8   import java.util.logging.Logger;
    9
    10  /**
    11   *	@author Romain PELISSE, belaran@gmail.com
    12   *  @author Roberto Ferranti - rferranti@users.sourceforge.net
    13   *  Thanks to Jesse Glick for the bug report.
    14   *
    15   */
    16  public class ScopedLogHandlersManager {
    17
    18  	private static String PACKAGE_NAME = "net.sourceforge.pmd";
    19
    20      private Logger logger;
    21      private Level oldLogLevel;
    22      private Handler[] oldHandlers;
    23      private Handler[] newHandlers;
    24
    25      public ScopedLogHandlersManager(Level level, Handler... handlers) {
    26          newHandlers = handlers;
    27          logger = Logger.getLogger(PACKAGE_NAME);
    28          oldHandlers = logger.getHandlers();
    29          oldLogLevel = logger.getLevel();
    30          logger.setLevel(level);
    31          //The Ant logger filters itself
    32          for (Handler handler : oldHandlers) {
    33              logger.removeHandler(handler);
    34          }
    35          for (Handler handler : newHandlers) {
    36              logger.addHandler(handler);
    37          }
    38      }
    39
    40      public void close() {
    41          for (Handler handler : newHandlers) {
    42              logger.removeHandler(handler);
    43          }
    44          for (Handler handler : oldHandlers) {
    45              logger.addHandler(handler);
    46          }
    47          logger.setLevel(oldLogLevel);
    48      }
    49  }