[Arakhnę-Dev] [15] First public release of arakhneLogger

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


Revision: 15
Author:   galland
Date:     2008-12-06 15:36:42 +0100 (Sat, 06 Dec 2008)

Log Message:
-----------
First public release of arakhneLogger

Added Paths:
-----------
    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: trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/ConsoleLogger.java	2008-12-06 14:36:42 UTC (rev 15)
@@ -0,0 +1,157 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2004-2008 Sté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.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ * This class provides the concrete implementation of the <code>Logger</code>
+ * interface. This impl is displaying on the standard output.
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see Logger
+ */
+public class ConsoleLogger extends AbstractLogger {
+
+	private final String name;
+	
+	private static String errorLabel = null;
+	private static String warningLabel = null;
+	private static String infoLabel = null;
+	private static String debugLabel = null;
+	
+	private static String getString(LogLevel level) {
+		switch (level) {
+		case NONE:
+			return null;
+		case DEBUG:
+			if (debugLabel!=null) return debugLabel;
+			break;
+		case WARNING:
+			if (warningLabel!=null) return warningLabel;
+			break;
+		case ERROR:
+			if (errorLabel!=null) return errorLabel;
+			break;
+		case INFO:
+			if (infoLabel!=null) return infoLabel;
+			break;
+		}
+
+		String name = level.name().toUpperCase();
+		
+        try {
+        	ResourceBundle resource = ResourceBundle.getBundle(ConsoleLogger.class.getCanonicalName());
+        	String str = resource.getString(name);
+        	if ((str!=null)&&(!"".equals(str))) //$NON-NLS-1$
+        		name = str;
+        }
+        catch (MissingResourceException exep) {
+            //
+        }
+		
+		switch (level) {
+		case NONE:
+			return null;
+		case DEBUG:
+			debugLabel = name;
+			break;
+		case WARNING:
+			warningLabel = name;
+			break;
+		case ERROR:
+			errorLabel = name;
+			break;
+		case INFO:
+			infoLabel = name;
+			break;
+		}
+		
+		return name;
+	}
+
+	/**
+	 * Unamed Logger on system console.
+	 */
+	public ConsoleLogger() {
+		this.name = null;
+	}
+
+	/**
+	 * Named Logger on system console.
+	 * 
+	 * @param name is the name of the logger.
+	 */
+	public ConsoleLogger(String name) {
+		this.name = name;
+	}
+
+//	**************************************************************************************//
+	
+	/** {@InheritDoc}
+	 * 
+	 * @param level {@InheritDoc}
+	 * @param msg {@InheritDoc}
+	 */
+	public void log(LogLevel level, String msg) {
+		if (isLoggableFor(level)) {
+			
+			StringBuffer buffer = new StringBuffer();
+			
+			if (this.name!=null) {
+				buffer.append("["); //$NON-NLS-1$
+				buffer.append(this.name);
+				buffer.append("] - "); //$NON-NLS-1$
+			}
+			buffer.append(getString(level));
+			buffer.append(": "); //$NON-NLS-1$
+			buffer.append(msg);
+			
+			System.out.println(buffer.toString());
+		}
+	}
+
+	/** {@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();
+	}
+
+	
+//	**************************************************************************************//	
+
+	/** {@InheritDoc}
+	 * 
+	 * @return {@InheritDoc}
+	 */
+	public String getName() {
+		return this.name;
+	}
+
+}

Added: trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/Logger.java	2008-12-06 14:36:42 UTC (rev 15)
@@ -0,0 +1,210 @@
+/* 
+ * $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;
+
+/**
+ * The main user interface to logging.
+ * This interface provides classic method use to log 
+ * string or status at various level.
+ * These level are described in the <code>LogLevel</code> Enum;
+ * 
+ * @author Nicolas GAUD &lt;gaud@xxxxxxxxxxx&gt;
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see Logger.LogLevel
+ */
+public interface Logger {
+
+	/**
+	 * The enum describing the various available log levels.
+	 * 
+	 * @author Nicolas GAUD &lt;nicolas.gaud@xxxxxxx&gt;
+	 * @version $Name$ $Revision$ $Date$
+	 */	
+	public static enum LogLevel {
+		/**
+		 * No logging.
+		 */
+		NONE,
+		/**
+		 * Error message Level.
+		 */
+		ERROR,
+		/**
+		 * Warning message Level.
+		 */
+		WARNING,
+		/**
+		 * Information message Level.
+		 */
+		INFO,
+		/**
+		 * Debug message Level.
+		 */
+		DEBUG;
+
+		/** Check if logging is allowed.
+		 * This function check if the current log level has a priority
+		 * greater or equals to the given log level.
+		 * 
+		 * @param referenceLogLevel is the level to check.
+		 * @return <code>true</code> if logging is allowed, <code>false</code> otherwise
+		 */
+		public boolean hasHigherPriority(LogLevel referenceLogLevel) {
+			return ordinal()>0 && ordinal()<=referenceLogLevel.ordinal();
+		}	
+
+	}
+
+	/**
+	 * Return the name of this <code>Logger</code> instance.
+	 * 
+	 * @return the name of this <code>Logger</code> instance.
+	 */
+	public String getName();
+
+	/**
+	 * Is the logger instance enabled for the DEBUG level?
+	 * 
+	 * @return <code>true</code> if this Logger is enabled for the DEBUG level,
+	 * <code>false</code> otherwise.
+	 */
+	public boolean isDebugEnabled();
+
+	/**
+	 * Log the specified message at the specified level
+	 * 
+	 * @param level - the log level
+	 * @param msg - the message to log
+	 */
+	public void log(LogLevel level, String msg);
+
+	/**
+	 * Log the specified message at the specified level
+	 * 
+	 * @param level - the log level
+	 * @param msg - the message to log
+	 * @param exception - the exception cause of the log message
+	 */
+	public void log(LogLevel level, String msg, Throwable exception);
+
+	/**
+	 * Log a message at the DEBUG level.
+	 *
+	 * @param msg the message string to be logged
+	 */
+	public void debug(String msg);
+
+	/**
+	 * Log an exception (throwable) at the DEBUG level with an
+	 * accompanying message. 
+	 * 
+	 * @param msg the message accompanying the exception
+	 * @param t the exception (throwable) to log
+	 */ 
+	public void debug(String msg, Throwable t);
+
+	/**
+	 * Is the logger instance enabled for the INFO level?
+	 * 
+	 * @return <code>true</code> if this Logger is enabled for the INFO level,
+	 * <code>false</code> otherwise.
+	 */
+	public boolean isInfoEnabled();
+
+
+	/**
+	 * Log a message at the INFO level.
+	 *
+	 * @param msg the message string to be logged
+	 */
+	public void info(String msg);
+
+	/**
+	 * Log an exception (throwable) at the INFO level with an
+	 * accompanying message. 
+	 * 
+	 * @param msg the message accompanying the exception
+	 * @param t the exception (throwable) to log 
+	 */
+	public void info(String msg, Throwable t);
+
+	/**
+	 * Is the logger instance enabled for the WARN level?
+	 * 
+	 * @return <code>true</code> if this Logger is enabled for the WARN level,
+	 * <code>false</code> otherwise.
+	 */
+	public boolean isWarnEnabled();
+
+	/**
+	 * Log a message at the WARN level.
+	 *
+	 * @param msg the message string to be logged
+	 */
+	public void warn(String msg);
+
+	/**
+	 * Log an exception (throwable) at the WARN level with an
+	 * accompanying message. 
+	 * 
+	 * @param msg the message accompanying the exception
+	 * @param t the exception (throwable) to log 
+	 */
+	public void warn(String msg, Throwable t);
+
+	/**
+	 * Is the logger instance enabled for the ERROR level?
+	 * 
+	 * @return <code>true</code> if this Logger is enabled for the ERROR level,
+	 * <code>false</code> otherwise.
+	 */
+	public boolean isErrorEnabled();
+
+	/**
+	 * Log a message at the ERROR level.
+	 *
+	 * @param msg the message string to be logged
+	 */
+	public void error(String msg);
+
+	/**
+	 * Log an exception (throwable) at the ERROR level with an
+	 * accompanying message. 
+	 * 
+	 * @param msg the message accompanying the exception
+	 * @param t the exception (throwable) to log
+	 */
+	public void error(String msg, Throwable t);
+	
+	/** Set the minimum log level.
+	 * 
+	 * @param level
+	 */
+	public void setLogLevel(LogLevel level);
+
+	/** Replies the minimum log level.
+	 * 
+	 * @return the minimal log level
+	 */
+	public LogLevel getLogLevel();
+
+}

Added: trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/LoggingSystem.java	2008-12-06 14:36:42 UTC (rev 15)
@@ -0,0 +1,237 @@
+/* 
+ * $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.lang.reflect.Constructor;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.arakhne.logging.Logger.LogLevel;
+
+
+
+/** This interface describes the body of an situated agent.
+ * The body is the only available interaction mean between
+ * an agent and the environment.
+ * 
+ * @author Nicolas GAUD &lt;gaud@xxxxxxxxxxx&gt;
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ */
+public class LoggingSystem {
+
+	private static final String[] PREFERED_LOGGERS = new String[] {
+		"fr.utbm.set.janus.kernel.logging.ApacheLogger", //$NON-NLS-1$
+		"fr.utbm.set.logging.SunLogger", //$NON-NLS-1$
+		"fr.utbm.set.logging.ConsoleLogger" //$NON-NLS-1$
+	};
+	
+	private static final LoggingSystem singleton = new LoggingSystem();
+	
+	/**
+	 * Return an anonymous logger.
+	 * 
+	 * @return logger
+	 */
+	public static Logger getLogger() {
+		return singleton.getAnonymousLogger();
+	}
+
+	/**
+	 * Return a logger named according to the name parameter.
+	 * 
+	 * @param name - The name of the logger.
+	 * @return logger
+	 */
+	public static Logger getLogger(String name) {
+		return singleton.getPreferedLogger(name);
+	}
+
+	/**
+	 * Return a logger named corresponding to the class passed as parameter.
+	 * 
+	 * @param clazz - the returned logger will be named after clazz
+	 * @return logger
+	 */
+	public static Logger getLogger(Class<?> clazz) {
+		return singleton.getPreferedLogger(clazz.getCanonicalName());
+	}
+
+	/**
+	 * Returns the instance of <code>LoggingSystem</code> in use
+	 * @return the instance of <code>LoggingSystem</code> in use
+	 */
+	public static LoggingSystem getLoggingSystem() {
+		return singleton;
+	}	
+	
+	private final Map<String,Logger> preferedLoggers = new TreeMap<String,Logger>();
+	private Logger anonymousLogger = null;
+	
+	private Class<? extends Logger> preferedLoggerType = null;
+	
+	private LogLevel defaultLevel = LogLevel.WARNING;
+	
+	/**
+	 */
+	protected LoggingSystem() {
+		//
+	}
+	
+	/** Set the default log level for new loggers.
+	 * 
+	 * @param logLevel
+	 */
+	public void setDefaultLogLevel(LogLevel logLevel) {
+		assert(logLevel!=null);
+		this.defaultLevel = logLevel;
+	}
+	
+	/** Replies the default log level for new loggers.
+	 * 
+	 * @return the default log level, never <code>null</code>
+	 */
+	public LogLevel getDefaultLogLevel() {
+		return this.defaultLevel;
+	}
+
+	/** Set the prefered type for the loggers.
+	 * 
+	 * @param type is the prefered type or <code>null</code> to use the system default.
+	 */
+	public void setPreferedLoggerType(Class<? extends Logger> type) {
+		this.preferedLoggerType = type;
+	}
+	
+	/** Replies the prefered type for the loggers.
+	 * 
+	 * @return the type of <code>null</code> to use the system default.
+	 */
+	public Class<? extends Logger> getPreferedLoggerType() {
+		return this.preferedLoggerType;
+	}
+	/** Set the prefered logger.
+	 * 
+	 * @param scopeName is the name of the scope for the new prefered logger.
+	 * @param logger msut be a reference to the new prefered logger
+	 * or <code>null</code> to use the default logger.
+	 */
+	public void setPreferedLogger(String scopeName, Logger logger) {
+		if (logger==null)
+			this.preferedLoggers.remove(scopeName);
+		else
+			this.preferedLoggers.put(scopeName, logger);
+	}
+	
+	/** Replies the prefered logger for the specified scope.
+	 * 
+	 * @param scopeName is the name of the logger scope.
+	 * @return the logger.
+	 */
+	public Logger getPreferedLogger(String scopeName) {
+		Logger refLogger = this.preferedLoggers.get(scopeName);
+		if (refLogger!=null) return refLogger;
+		// Create default logger
+		Logger logger = createLoggerInstance(scopeName, this.defaultLevel);
+		this.preferedLoggers.put(scopeName, logger);
+		return logger;
+	}	
+	
+	/** Replies the logger to use when the scope is unknown.
+	 * 
+	 * @return the anonymous logger.
+	 */
+	public Logger getAnonymousLogger() {
+		Logger anonLogger = this.anonymousLogger;
+		if (anonLogger==null) {
+			anonLogger = createLoggerInstance(null, this.defaultLevel);
+			this.anonymousLogger = anonLogger;
+		}
+		return anonLogger;
+	}
+	
+	/** 
+	 * @param name is the scope of the logger to create.
+	 * @param minLogLevel is the minimal log level of the new logger.
+	 * @return the created logged.
+	 */
+	protected Logger createLoggerInstance(String name, LogLevel minLogLevel) {
+		Class<? extends Logger> type = findLoggerClass();
+		Logger logger = createLoggerInstance(type, name);
+		if (logger!=null) {
+			logger.setLogLevel(this.defaultLevel);
+			return logger;
+		}
+		type = findDefaultLoggerClass();
+		logger = createLoggerInstance(type, name);
+		if (logger!=null) {
+			logger.setLogLevel(this.defaultLevel);
+			return logger;
+		}
+		throw new Error("unable to create an instance of Logger"); //$NON-NLS-1$
+	}
+	
+	private Logger createLoggerInstance(Class<? extends Logger> type, String name) {
+		if (type==null) return null;
+		if ((name!=null)&&(!"".equals(name))) { //$NON-NLS-1$
+			try {
+				Constructor<? extends Logger> cons = type.getConstructor(String.class);
+				return cons.newInstance(name);
+			}
+			catch(Exception _) {
+				//
+			}
+		}
+		try {
+			return type.newInstance();
+		}
+		catch(Exception _) {
+			//
+		}
+		return null;
+	}
+
+	/**
+	 * @return the type of the the preferred logging system.
+	 */
+	protected final Class<? extends Logger> findLoggerClass() {
+		if (this.preferedLoggerType!=null) return this.preferedLoggerType;
+		return findDefaultLoggerClass();
+	}
+
+	/**
+	 * @return the type of the the preferred logging system.
+	 */
+	@SuppressWarnings("unchecked")
+	protected Class<? extends Logger> findDefaultLoggerClass() {
+		for(String className : PREFERED_LOGGERS) {
+			try {
+				Class<?> type = Class.forName(className);
+				if (type!=null && Logger.class.isAssignableFrom(type)) return (Class<? extends Logger>)type;
+			}
+			catch(Exception _) {
+				//
+			}
+		}
+		return null;
+	}
+
+}

Added: trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java
===================================================================
--- trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java	                        (rev 0)
+++ trunk/arakhneLogger/src/main/java/org/arakhne/logging/SunLogger.java	2008-12-06 14:36:42 UTC (rev 15)
@@ -0,0 +1,118 @@
+/* 
+ * $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.logging.Level;
+
+/**
+ * This class provides the concrete implementation of the <code>Logger</code>
+ * interface. This impl is based on the Sun's Logging Facade for Java
+ * 
+ * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+ * @version $Name$ $Revision$ $Date$
+ * @see Logger
+ */
+public class SunLogger extends AbstractLogger {
+
+	private final java.util.logging.Logger bindedLogger;
+
+	/**
+	 * Create an unamed logger based of Sun's logging system.
+	 */
+	public SunLogger() {
+		this.bindedLogger = java.util.logging.Logger.getAnonymousLogger();
+		this.bindedLogger.setLevel(Level.ALL);
+	}
+
+	/**
+	 * Create a named logger based of Sun's logging system.
+	 * 
+	 * @param name is the name of the logger.
+	 */
+	public SunLogger(String name) {
+		this.bindedLogger = java.util.logging.Logger.getLogger(name);
+		this.bindedLogger.setLevel(toSunLevel(getLogLevel()));
+	}
+
+//	**************************************************************************************//
+
+	/** Translate a logging level into the same thing for Sun's logging system.
+	 * 
+	 * @param level is the level to translate
+	 * @return the Sun's level
+	 */
+	protected Level toSunLevel(LogLevel level) {
+		switch(level) {
+		case NONE:
+			return Level.OFF;
+		case ERROR:
+			return Level.SEVERE;
+		case WARNING:
+			return Level.WARNING;
+		case INFO:
+			return Level.INFO;
+		case DEBUG:
+			return Level.CONFIG;
+		}
+		return Level.OFF;
+	}
+	
+	/** {@InheritDoc}
+	 * 
+	 * @param level {@InheritDoc}
+	 * @param msg {@InheritDoc}
+	 */
+	public void log(LogLevel level, String msg) {
+		this.bindedLogger.log(toSunLevel(level),msg);
+	}
+
+	/** {@InheritDoc}
+	 * 
+	 * @param level {@InheritDoc}
+	 * @param msg {@InheritDoc}
+	 * @param exception {@InheritDoc}
+	 */
+	public void log(LogLevel level, String msg, Throwable exception) {
+		this.bindedLogger.log(toSunLevel(level),msg,exception);
+	}
+
+	
+//	**************************************************************************************//	
+
+	/** {@InheritDoc}
+	 * 
+	 * @return {@InheritDoc}
+	 */
+	public String getName() {
+		return this.bindedLogger.getName();
+	}
+
+	/** Set the minimum log level.
+	 * 
+	 * @param level
+	 */
+	@Override
+	public void setLogLevel(LogLevel level) {
+		super.setLogLevel(level);
+		this.bindedLogger.setLevel(toSunLevel(level));
+	}
+
+}


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