01: package com.jidesoft.utils;
02:
03: import java.util.logging.Handler;
04: import java.util.logging.Level;
05: import java.util.logging.LogRecord;
06: import java.util.logging.Logger;
07:
08: /**
09: * LoggerUtils contains two methods to allow logger to print certain level of message to console.
10: * This is mainly used for debugging purpose.
11: */
12: public class LoggerUtils {
13: public static Handler enableLogger(String loggerName, Level level) {
14: Logger log = Logger.getLogger(loggerName);
15: log.setLevel(level);
16: Handler handler = new Handler() {
17: @Override
18: public void publish(LogRecord record) {
19: System.err.println(record.getMessage());
20: }
21:
22: @Override
23: public void flush() {
24: }
25:
26: @Override
27: public void close() throws SecurityException {
28: }
29: };
30: log.addHandler(handler);
31: return handler;
32: }
33:
34: public static void disableLogger(String loggerName, Handler handler) {
35: Logger log = Logger.getLogger(loggerName);
36: log.setLevel(null);
37: log.removeHandler(handler);
38: }
39: }
|