From: David ‘Bombe’ Roden Date: Wed, 16 Apr 2008 06:31:03 +0000 (+0000) Subject: add logging handler X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ee455fc59ab4cf41a7777c1ee8185e2ec14cb045;p=jSite2.git add logging handler git-svn-id: http://trooper/svn/projects/jSite/trunk@751 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/util/logging/Logging.java b/src/net/pterodactylus/util/logging/Logging.java new file mode 100644 index 0000000..443c05d --- /dev/null +++ b/src/net/pterodactylus/util/logging/Logging.java @@ -0,0 +1,154 @@ +/* + * jFCPlib-high-level-client - Logging.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.util.logging; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * Sets up logging. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class Logging { + + /** The log handler. */ + private static final LogHandler logHandler = new LogHandler(); + + /** + * Adds a listener to the log handler. + * + * @param loggingListener + * The listener to add + */ + public static void addLoggingListener(LoggingListener loggingListener) { + logHandler.addLoggingListener(loggingListener); + } + + /** + * Removes a listener from the log handler. + * + * @param loggingListener + * The listener to remove + */ + public static void removeLoggingListener(LoggingListener loggingListener) { + logHandler.removeLoggingListener(loggingListener); + } + + /** + * Sets up logging and installs the log handler. + */ + public static void setup() { + Logger rootLogger = Logger.getAnonymousLogger().getParent(); + rootLogger.addHandler(logHandler); + rootLogger.setLevel(Level.ALL); + logHandler.setLevel(Level.INFO); + } + + /** + * The log handler simply forwards every log message it receives to all + * registered listeners. + * + * @see LoggingListener + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ + private static class LogHandler extends Handler { + + /** + * Package-private constructor. + */ + LogHandler() { + } + + /** The list of the listeners. */ + private final List loggingListeners = Collections.synchronizedList(new ArrayList()); + + // + // EVENT MANAGEMENT + // + + /** + * Adds a listener to the log handler. + * + * @param loggingListener + * The listener to add + */ + public void addLoggingListener(LoggingListener loggingListener) { + loggingListeners.add(loggingListener); + } + + /** + * Removes a listener from the log handler. + * + * @param loggingListener + * The listener to remove + */ + public void removeLoggingListener(LoggingListener loggingListener) { + loggingListeners.remove(loggingListener); + } + + /** + * Notifies all listeners that a log record was received. + * + * @param logRecord + * The received log record + */ + private void fireLogged(LogRecord logRecord) { + for (LoggingListener loggingListener: loggingListeners) { + loggingListener.logged(logRecord); + } + } + + // + // INTERFACE Handler + // + + /** + * {@inheritDoc} + */ + @Override + public void close() throws SecurityException { + } + + /** + * {@inheritDoc} + */ + @Override + public void flush() { + } + + /** + * {@inheritDoc} + */ + @Override + public void publish(LogRecord logRecord) { + fireLogged(logRecord); + } + + } + +} diff --git a/src/net/pterodactylus/util/logging/LoggingListener.java b/src/net/pterodactylus/util/logging/LoggingListener.java new file mode 100644 index 0000000..788d848 --- /dev/null +++ b/src/net/pterodactylus/util/logging/LoggingListener.java @@ -0,0 +1,41 @@ +/* + * jFCPlib-high-level-client - LoggingListener.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.util.logging; + +import java.util.EventListener; +import java.util.logging.LogRecord; + +/** + * Interface for components that want to received logged messages. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public interface LoggingListener extends EventListener { + + /** + * Notifies a listener that a new log record was received. + * + * @param logRecord + * The received log record + */ + public void logged(LogRecord logRecord); + +}