2 * jFCPlib-high-level-client - Logging.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.util.logging;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.logging.Handler;
26 import java.util.logging.Level;
27 import java.util.logging.LogRecord;
28 import java.util.logging.Logger;
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class Logging {
38 /** The log handler. */
39 private static final LogHandler logHandler = new LogHandler();
42 * Adds a listener to the log handler.
44 * @param loggingListener
47 public static void addLoggingListener(LoggingListener loggingListener) {
48 logHandler.addLoggingListener(loggingListener);
52 * Removes a listener from the log handler.
54 * @param loggingListener
55 * The listener to remove
57 public static void removeLoggingListener(LoggingListener loggingListener) {
58 logHandler.removeLoggingListener(loggingListener);
62 * Sets up logging and installs the log handler.
64 public static void setup() {
65 Logger rootLogger = Logger.getAnonymousLogger().getParent();
66 rootLogger.addHandler(logHandler);
67 rootLogger.setLevel(Level.ALL);
68 logHandler.setLevel(Level.INFO);
72 * The log handler simply forwards every log message it receives to all
73 * registered listeners.
75 * @see LoggingListener
76 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
79 private static class LogHandler extends Handler {
82 * Package-private constructor.
87 /** The list of the listeners. */
88 private final List<LoggingListener> loggingListeners = Collections.synchronizedList(new ArrayList<LoggingListener>());
95 * Adds a listener to the log handler.
97 * @param loggingListener
100 public void addLoggingListener(LoggingListener loggingListener) {
101 loggingListeners.add(loggingListener);
105 * Removes a listener from the log handler.
107 * @param loggingListener
108 * The listener to remove
110 public void removeLoggingListener(LoggingListener loggingListener) {
111 loggingListeners.remove(loggingListener);
115 * Notifies all listeners that a log record was received.
118 * The received log record
120 private void fireLogged(LogRecord logRecord) {
121 for (LoggingListener loggingListener: loggingListeners) {
122 loggingListener.logged(logRecord);
134 public void close() throws SecurityException {
141 public void flush() {
148 public void publish(LogRecord logRecord) {
149 fireLogged(logRecord);