From b61aef4ca2d8b1e01e95a7c55a1af8a682c348d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 27 Jan 2009 09:16:37 +0100 Subject: [PATCH] Add logger. --- src/net/pterodactylus/util/logging/Logging.java | 223 +++++++++++++++++++++ .../util/logging/LoggingListener.java | 40 ++++ 2 files changed, 263 insertions(+) create mode 100644 src/net/pterodactylus/util/logging/Logging.java create mode 100644 src/net/pterodactylus/util/logging/LoggingListener.java diff --git a/src/net/pterodactylus/util/logging/Logging.java b/src/net/pterodactylus/util/logging/Logging.java new file mode 100644 index 0000000..86a4a6f --- /dev/null +++ b/src/net/pterodactylus/util/logging/Logging.java @@ -0,0 +1,223 @@ +/* + * 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.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.logging.ConsoleHandler; +import java.util.logging.Formatter; +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> + */ +public class Logging { + + /** The log handler. */ + private static final LogHandler logHandler = new LogHandler(); + + static { + logHandler.setLevel(Level.ALL); + } + + /** The root name of the hierarchy. */ + private static String hierarchyRootName; + + /** + * 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. + * + * @param hierarchyName + * The name of the hierarchy root logger + */ + public static void setup(String hierarchyName) { + hierarchyRootName = hierarchyName; + Logger rootLogger = Logger.getLogger(hierarchyName); + rootLogger.addHandler(logHandler); + Handler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.ALL); + consoleHandler.setFormatter(new Formatter() { + + private StringBuffer recordBuffer = new StringBuffer(); + private DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z"); + + /** + * {@inheritDoc} + */ + @Override + public synchronized String format(LogRecord record) { + recordBuffer.setLength(0); + String linePrefix = dateFormatter.format(new Date(record.getMillis())) + " [" + record.getLevel() + "] [" + Thread.currentThread().getName() + "] [" + record.getSourceClassName() + "." + record.getSourceMethodName() + "] "; + recordBuffer.append(linePrefix).append(record.getMessage()).append('\n'); + if (record.getThrown() != null) { + Throwable throwable = record.getThrown(); + boolean causedBy = false; + while (throwable != null) { + recordBuffer.append(linePrefix); + if (causedBy) { + recordBuffer.append("caused by: "); + } + recordBuffer.append(throwable.getClass().getName()); + if (throwable.getMessage() != null) { + recordBuffer.append(": ").append(throwable.getMessage()); + } + recordBuffer.append("\n"); + StackTraceElement[] stackTraceElements = throwable.getStackTrace(); + for (StackTraceElement stackTraceElement: stackTraceElements) { + recordBuffer.append(linePrefix).append(" at ").append(stackTraceElement.getClassName()).append('.').append(stackTraceElement.getMethodName()).append("(").append(stackTraceElement.getFileName()).append(':').append(stackTraceElement.getLineNumber()).append(')').append("\n"); + } + throwable = throwable.getCause(); + causedBy = true; + } + } + return recordBuffer.toString(); + } + }); + rootLogger.addHandler(consoleHandler); + rootLogger.setUseParentHandlers(false); + rootLogger.setLevel(Level.ALL); + } + + /** + * Returns a named logger from the jSite hierarchy. + * + * @param name + * The name of the logger + * @return The logger + */ + public static Logger getLogger(String name) { + Logger logger = Logger.getLogger(hierarchyRootName + "." + name); + return logger; + } + + /** + * The log handler simply forwards every log message it receives to all + * registered listeners. + * + * @see LoggingListener + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + private static class LogHandler extends Handler { + + /** + * Package-private constructor. + */ + LogHandler() { + /* do nothing. */ + } + + /** 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 { + /* do nothing. */ + } + + /** + * {@inheritDoc} + */ + @Override + public void flush() { + /* do nothing. */ + } + + /** + * {@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..0025c67 --- /dev/null +++ b/src/net/pterodactylus/util/logging/LoggingListener.java @@ -0,0 +1,40 @@ +/* + * 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> + */ +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); + +} -- 2.7.4