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