| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.application; |
| 16 | |
| 17 | import java.util.Map; |
| 18 | import java.util.concurrent.ConcurrentHashMap; |
| 19 | import java.util.logging.Handler; |
| 20 | import java.util.logging.Level; |
| 21 | import java.util.logging.LogManager; |
| 22 | import java.util.logging.LogRecord; |
| 23 | import java.util.logging.Logger; |
| 24 | |
| 25 | import org.apache.commons.logging.Log; |
| 26 | import org.apache.commons.logging.LogFactory; |
| 27 | |
| 28 | /** |
| 29 | * Utility class to redirect JUL logs (from Swing App Framework) to |
| 30 | * commons-logging (used by hivemind, hivemind-utilities). |
| 31 | * @author MarkusDoering |
| 32 | */ |
| 33 | public final class JavaLoggingToCommonLoggingRedirector |
| 34 | { |
| 35 | private JavaLoggingToCommonLoggingRedirector() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Activates this feature. |
| 41 | */ |
| 42 | public static void activate() |
| 43 | { |
| 44 | // CSOFF: IllegalCatchCheck |
| 45 | try |
| 46 | { |
| 47 | Logger rootLogger = LogManager.getLogManager().getLogger(""); |
| 48 | // remove old handlers |
| 49 | for (Handler handler : rootLogger.getHandlers()) |
| 50 | { |
| 51 | rootLogger.removeHandler(handler); |
| 52 | } |
| 53 | // add our own |
| 54 | _activeHandler = new JDKLogHandler(); |
| 55 | _activeHandler.setLevel(Level.ALL); |
| 56 | rootLogger.addHandler(_activeHandler); |
| 57 | rootLogger.setLevel(Level.ALL); |
| 58 | // done, let's check it right away!!! |
| 59 | Logger.getLogger(JavaLoggingToCommonLoggingRedirector.class.getName()) |
| 60 | .info("activated: sending JDK log messages to Commons Logging"); |
| 61 | } |
| 62 | catch (Exception exc) |
| 63 | { |
| 64 | LogFactory.getLog(JavaLoggingToCommonLoggingRedirector.class) |
| 65 | .error("activation failed", exc); |
| 66 | } |
| 67 | // CSON: IllegalCatchCheck |
| 68 | } |
| 69 | |
| 70 | public static void deactivate() |
| 71 | { |
| 72 | Logger rootLogger = LogManager.getLogManager().getLogger(""); |
| 73 | rootLogger.removeHandler(_activeHandler); |
| 74 | Logger.getLogger(JavaLoggingToCommonLoggingRedirector.class.getName()) |
| 75 | .info("dactivated"); |
| 76 | } |
| 77 | |
| 78 | static private class JDKLogHandler extends Handler |
| 79 | { |
| 80 | private Map<String, Log> _cachedLogs = new ConcurrentHashMap<String, Log>(); |
| 81 | |
| 82 | private Log getLog(String logName) |
| 83 | { |
| 84 | Log log = _cachedLogs.get(logName); |
| 85 | if (log == null) |
| 86 | { |
| 87 | log = LogFactory.getLog(logName); |
| 88 | _cachedLogs.put(logName, log); |
| 89 | } |
| 90 | return log; |
| 91 | } |
| 92 | |
| 93 | @Override public void publish(LogRecord record) |
| 94 | { |
| 95 | Log log = getLog(record.getLoggerName()); |
| 96 | String message = record.getMessage(); |
| 97 | Throwable exception = record.getThrown(); |
| 98 | Level level = record.getLevel(); |
| 99 | if (level == Level.SEVERE) |
| 100 | { |
| 101 | log.error(message, exception); |
| 102 | } |
| 103 | else if (level == Level.WARNING) |
| 104 | { |
| 105 | log.warn(message, exception); |
| 106 | } |
| 107 | else if (level == Level.INFO) |
| 108 | { |
| 109 | log.info(message, exception); |
| 110 | } |
| 111 | else if (level == Level.CONFIG) |
| 112 | { |
| 113 | log.debug(message, exception); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | log.trace(message, exception); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override public void flush() |
| 122 | { |
| 123 | // nothing to do |
| 124 | } |
| 125 | |
| 126 | @Override public void close() |
| 127 | { |
| 128 | // nothing to do |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static private JDKLogHandler _activeHandler; |
| 133 | } |