[Arakhnę-Dev] [59] Add logger event listening API.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 59
Author:   galland
Date:     2009-05-18 13:38:18 +0200 (Mon, 18 May 2009)

Log Message:
-----------
Add logger event listening API.

Modified Paths:
--------------
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/AbstractLogger.java
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java

Added Paths:
-----------
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEvent.java
    trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEventListener.java


Modified: trunk/arakhneLogger/src/main/java/org/arakhne/logging/AbstractLogger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/AbstractLogger.java	2009-05-05 02:49:33 UTC (rev 58)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/AbstractLogger.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -20,6 +20,9 @@
  */
 package org.arakhne.logging;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * This class provides the concrete implementation of the <code>Logger</code>
  * interface. This impl is based on the Sun's Logging Facade for Java
@@ -31,6 +34,7 @@
 public abstract class AbstractLogger implements Logger {
 
 	private LogLevel minLevel;
+	private transient List<LoggerEventListener> listeners = null;
 	
 	/**
 	 */
@@ -152,4 +156,61 @@
 		return isLoggableFor(LogLevel.WARNING);
 	}
 
+	/** Add listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public final void addLoggerEventListener(LoggerEventListener listener) {
+		if (this.listeners==null) {
+			this.listeners = new ArrayList<LoggerEventListener>();
+		}
+		this.listeners.add(listener);
+	}
+
+	/** Remove listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public void removeLoggerEventListener(LoggerEventListener listener) {
+		if (this.listeners!=null) {
+			this.listeners.remove(listener);
+			if (this.listeners.isEmpty())
+				this.listeners = null;
+		}
+	}
+	
+	/** Notify the logger event listeners.
+	 * 
+	 * @param level is the level of the message
+	 * @param message is the text of the message.
+	 */
+	protected final void fireLoggerEvent(LogLevel level, String message) {
+		fireLoggerEvent(level, message, null);
+	}
+	
+	/** Notify the logger event listeners.
+	 * 
+	 * @param level is the level of the message
+	 * @param message is the text of the message.
+	 * @param exception is the exception associated to the message.
+	 */
+	protected final void fireLoggerEvent(LogLevel level, String message, Throwable exception) {
+		if (this.listeners!=null) {
+			LoggerEvent event = new LoggerEvent(this, level, message, exception);
+			for(LoggerEventListener listener : this.listeners) {
+				listener.onLoggedEvent(event);
+			}
+		}
+	}
+
+	/** {@InheritDoc}
+	 * 
+	 * @param level {@InheritDoc}
+	 * @param msg {@InheritDoc}
+	 */
+	public void log(LogLevel level, String msg) {
+		log(level, msg, null);
+	}
+
+
 }

Modified: trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java	2009-05-05 02:49:33 UTC (rev 58)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -112,8 +112,9 @@
 	 * 
 	 * @param level {@InheritDoc}
 	 * @param msg {@InheritDoc}
+	 * @param exception {@InheritDoc}
 	 */
-	public void log(LogLevel level, String msg) {
+	public void log(LogLevel level, String msg, Throwable exception) {
 		if (isLoggableFor(level)) {
 			
 			StringBuffer buffer = new StringBuffer();
@@ -128,21 +129,14 @@
 			buffer.append(msg);
 			
 			System.out.println(buffer.toString());
+			
+			if (exception!=null)
+				exception.printStackTrace();
+
+			fireLoggerEvent(level, msg);
 		}
 	}
 
-	/** {@InheritDoc}
-	 * 
-	 * @param level {@InheritDoc}
-	 * @param msg {@InheritDoc}
-	 * @param exception {@InheritDoc}
-	 */
-	public void log(LogLevel level, String msg, Throwable exception) {
-		log(level,msg);
-		if ((exception!=null)&&(isLoggableFor(level)))
-			exception.printStackTrace();
-	}
-
 	
 //	**************************************************************************************//	
 

Modified: trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java	2009-05-05 02:49:33 UTC (rev 58)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -207,4 +207,16 @@
 	 */
 	public LogLevel getLogLevel();
 
+	/** Add listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public void addLoggerEventListener(LoggerEventListener listener);
+
+	/** Remove listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public void removeLoggerEventListener(LoggerEventListener listener);
+
 }

Added: trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEvent.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEvent.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEvent.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -0,0 +1,91 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2004-2008 St&eacute;phane GALLAND and Nicolas GAUD
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library 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
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.logging;
+
+import java.util.EventObject;
+
+import org.arakhne.logging.Logger.LogLevel;
+
+/**
+ * This class provides a description of a logging event.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see Logger
+ */
+public class LoggerEvent extends EventObject {
+
+	private static final long serialVersionUID = 6781328201390477344L;
+	
+	private final LogLevel level;
+	private final String message;
+	private final Throwable exception;
+	
+	/**
+	 * @param logger is the object which had fired the event.
+	 * @param level is the level of the message
+	 * @param message is the text of the message.
+	 */
+	public LoggerEvent(Logger logger, LogLevel level, String message) {
+		super(logger);
+		this.level = level;
+		this.message = message;
+		this.exception = null;
+	}
+	
+	/**
+	 * @param logger is the object which had fired the event.
+	 * @param level is the level of the message
+	 * @param message is the text of the message.
+	 * @param exception is the exception associated to the message.
+	 */
+	public LoggerEvent(Logger logger, LogLevel level, String message, Throwable exception) {
+		super(logger);
+		this.level = level;
+		this.message = message;
+		this.exception = exception;
+	}
+	
+	/** Replies the log level of the event.
+	 * 
+	 * @return the log level of the event.
+	 */
+	public LogLevel getLogLevel() {
+		return this.level;
+	}
+	
+	/** Replies the message in the event.
+	 * 
+	 * @return the message in the event.
+	 */
+	public String getMessage() {
+		return this.message;
+	}
+	
+	/** Replies the exception associated to this event.
+	 * 
+	 * @return the associated exception or <code>null</code>
+	 */
+	public Throwable getException() {
+		return this.exception;
+	}
+
+}

Added: trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEventListener.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEventListener.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggerEventListener.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -0,0 +1,40 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2004-2008 St&eacute;phane GALLAND and Nicolas GAUD
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library 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
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.logging;
+
+import java.util.EventListener;
+
+/**
+ * Listener on logger events.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see Logger
+ */
+public interface LoggerEventListener extends EventListener {
+
+	/** Invoked when an event was logged in.
+	 * 
+	 * @param event
+	 */
+	public void onLoggedEvent(LoggerEvent event);
+
+}

Modified: trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java	2009-05-05 02:49:33 UTC (rev 58)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -21,6 +21,8 @@
 package org.arakhne.logging;
 
 import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -90,6 +92,8 @@
 	
 	private LogLevel defaultLevel = LogLevel.WARNING;
 	
+	private List<LoggerEventListener> listeners = null;
+	
 	/**
 	 */
 	protected LoggingSystem() {
@@ -234,4 +238,39 @@
 		return null;
 	}
 
+	/** Add listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public final void addLoggerEventListener(LoggerEventListener listener) {
+		if (this.listeners==null) {
+			this.listeners = new ArrayList<LoggerEventListener>();
+		}
+		this.listeners.add(listener);
+	}
+
+	/** Remove listener on logger events.
+	 * 
+	 * @param listener
+	 */
+	public void removeLoggerEventListener(LoggerEventListener listener) {
+		if (this.listeners!=null) {
+			this.listeners.remove(listener);
+			if (this.listeners.isEmpty())
+				this.listeners = null;
+		}
+	}
+
+	/** Notify the logger event listeners.
+	 * 
+	 * @param event
+	 */
+	final void fireLoggerEvent(LoggerEvent event) {
+		if (this.listeners!=null) {
+			for(LoggerEventListener listener : this.listeners) {
+				listener.onLoggedEvent(event);
+			}
+		}
+	}
+
 }

Modified: trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java	2009-05-05 02:49:33 UTC (rev 58)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java	2009-05-18 11:38:18 UTC (rev 59)
@@ -80,8 +80,10 @@
 	 * @param level {@InheritDoc}
 	 * @param msg {@InheritDoc}
 	 */
+	@Override
 	public void log(LogLevel level, String msg) {
 		this.bindedLogger.log(toSunLevel(level),msg);
+		fireLoggerEvent(level, msg);
 	}
 
 	/** {@InheritDoc}
@@ -92,6 +94,7 @@
 	 */
 	public void log(LogLevel level, String msg, Throwable exception) {
 		this.bindedLogger.log(toSunLevel(level),msg,exception);
+		fireLoggerEvent(level, msg);
 	}
 
 	


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/