[exo-jcr-commits] exo-jcr SVN: r4813 - in kernel/trunk: exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Aug 30 09:45:20 EDT 2011


Author: dkuleshov
Date: 2011-08-30 09:45:20 -0400 (Tue, 30 Aug 2011)
New Revision: 4813

Added:
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/DynamicLocationAwareLogger.java
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java
   kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java
Modified:
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/Log.java
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LocationAwareSLF4JExoLog.java
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SLF4JExoLog.java
   kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SimpleExoLog.java
   kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/log/TestLoggers.java
Log:
EXOJCR-1479: added support for both slfj 1.5.x and 1.6.x LocationAwareLog.log methods via reflection, also added some new methods to org.exoplatform.services.log.Log interface and their implementations correspondingly

Modified: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/Log.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/Log.java	2011-08-30 13:21:51 UTC (rev 4812)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/Log.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -227,4 +227,88 @@
     */
    void fatal(Object message, Throwable t);
 
+   /**
+    * <p>
+    * Log an error with <i>trace</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void trace(String format, Object... argArray);
+
+   /**
+    * <p>
+    * Log an error with <i>debug</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void debug(String format, Object... argArray);
+
+   /**
+    * <p>
+    * Log an error with <i>info</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void info(String format, Object... argArray);
+
+   /**
+    * <p>
+    * Log a message with <i>warn</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void warn(String format, Object... argArray);
+
+   /**
+    * <p>
+    * Log a message with <i>error</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void error(String format, Object... argArray);
+
+   /**
+    * <p>
+    * Log a message with <i>fatal</i> log level.
+    * </p>
+    * 
+    * @param format <br> {@link String} format to represent log message.
+    * Sequence of '{}' is used to represent an anchor which will be replaced by
+    * string representation of objects in argArray
+    * @param argArray <br> array of arguments to add to log message
+    * including {@link Throwable}, {@link String} 
+    * or any other {@link Object} for which string representation will be used
+    */
+   void fatal(String format, Object... argArray);
+
 }

Added: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/DynamicLocationAwareLogger.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/DynamicLocationAwareLogger.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/DynamicLocationAwareLogger.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.log.impl;
+
+import org.slf4j.Marker;
+import org.slf4j.spi.LocationAwareLogger;
+
+import java.lang.reflect.Method;
+
+/**
+ * This is an utility class allowing to use in runtime either 1.5.x or
+ * 1.6.x slf4j libraries. As their log method signature differs, 
+ * we use reflection library to invoke it.
+ * 
+ * @author <a href="mailto:dkuleshov at exoplatform.com">Dmitry Kuleshov</a>
+ */
+public class DynamicLocationAwareLogger
+{
+   /**
+    * To keep log method instance
+    */
+   private Method log;
+
+   /**
+    * Message to show if log method invocation has failed for some reasons.
+    */
+   private final static String LOG_METHOD_INVOKE_ERROR_MSG =
+      "LocationAwareLogger had some issues on method 'log' invocation. Using location unaware methods.\n";
+
+   /**
+    * To trace if new parameter is supported
+    * 1.5.x slf4j lib does not support
+    * 1.6.x slf4j lib supports
+    */
+   private boolean parameterSupported = false;
+
+   /**
+    * Logger
+    */
+   private LocationAwareLogger logger;
+
+   /**
+    * Simple constructor with one parameter is used to pull out log {@link Method}
+    * an determine which version of slf4j library is currently used.
+    * 
+    * @param logger location aware logger to be wrapped
+    */
+   public DynamicLocationAwareLogger(LocationAwareLogger logger)
+   {
+      this.logger = logger;
+
+      // here we're going to retrieve 'log' method instance from logger's class
+      // using java reflection library 
+      // also we're determining number of parameters of 'log' method to know
+      // what slf4j library version we're dealing with
+      for (Method m : logger.getClass().getDeclaredMethods())
+      {
+         if ("log".equals(m.getName()))
+         {
+            log = m;
+            if (log.getParameterTypes().length == 6)
+            {
+               parameterSupported = true;
+            }
+            break;
+         }
+      }
+
+      // if no method named 'log' is determined for currently used logger class
+      // we throw an exception to warn that something is definitely going wrong
+      if (log == null)
+      {
+         throw new UnsupportedOperationException("Currently used logger does not have log method.");
+      }
+   }
+
+   /**
+    * Printing method with support for location information. 
+    * Encapsulates slf4j lib log {@link Method} invocation and passing it the correct parameters.
+    */
+   public void log(Marker marker, String fqcn, int level, String message, Throwable t)
+   {
+      try
+      {
+         if (!parameterSupported)
+         {
+            log.invoke(logger, marker, fqcn, level, message, t);
+         }
+         else
+         {
+            log.invoke(logger, marker, fqcn, level, message, null, t);
+         }
+      }
+      catch (Exception e)
+      {
+         switch( level)
+         {
+            case LocationAwareLogger.TRACE_INT :
+               logger.trace(LOG_METHOD_INVOKE_ERROR_MSG + message, t);
+               break;
+            case LocationAwareLogger.DEBUG_INT :
+               logger.debug(LOG_METHOD_INVOKE_ERROR_MSG + message, t);
+               break;
+            case LocationAwareLogger.INFO_INT :
+               logger.info(LOG_METHOD_INVOKE_ERROR_MSG + message, t);
+               break;
+            case LocationAwareLogger.WARN_INT :
+               logger.warn(LOG_METHOD_INVOKE_ERROR_MSG + message, t);
+               break;
+            case LocationAwareLogger.ERROR_INT :
+               logger.error(LOG_METHOD_INVOKE_ERROR_MSG + message, t);
+               break;
+         }
+      }
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isDebugEnabled()
+   {
+      return logger.isDebugEnabled();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isErrorEnabled()
+   {
+      return logger.isErrorEnabled();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isFatalEnabled()
+   {
+      return logger.isErrorEnabled();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isInfoEnabled()
+   {
+      return logger.isInfoEnabled();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isTraceEnabled()
+   {
+      return logger.isTraceEnabled();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public boolean isWarnEnabled()
+   {
+      return logger.isWarnEnabled();
+   }
+}
\ No newline at end of file

Modified: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LocationAwareSLF4JExoLog.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LocationAwareSLF4JExoLog.java	2011-08-30 13:21:51 UTC (rev 4812)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LocationAwareSLF4JExoLog.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -30,12 +30,11 @@
  */
 public class LocationAwareSLF4JExoLog implements Log
 {
-
    /** . */
    private static final String FQCN = LocationAwareSLF4JExoLog.class.getName();
 
    /** . */
-   private final LocationAwareLogger logger;
+   private final DynamicLocationAwareLogger logger;
 
    /**
     * Create a new instance.
@@ -49,96 +48,204 @@
       {
          throw new NullPointerException();
       }
-      this.logger = logger;
+      this.logger = new DynamicLocationAwareLogger(logger);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isDebugEnabled()
    {
       return logger.isDebugEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isErrorEnabled()
    {
       return logger.isErrorEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isFatalEnabled()
    {
       return logger.isErrorEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isInfoEnabled()
    {
       return logger.isInfoEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isTraceEnabled()
    {
       return logger.isTraceEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public boolean isWarnEnabled()
    {
       return logger.isWarnEnabled();
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void trace(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void trace(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(o), throwable);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void debug(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void debug(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(o), throwable);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void info(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void info(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(o), throwable);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void warn(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void warn(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(o), throwable);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void error(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void error(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(o), throwable);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void fatal(Object o)
    {
       logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(o), null);
    }
 
+   /**
+    * {@inheritDoc}
+    */
    public void fatal(Object o, Throwable throwable)
    {
       logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(o), throwable);
    }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void trace(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void debug(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void info(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.INFO_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void warn(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.WARN_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void error(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void fatal(String format, Object... argsArray)
+   {
+      logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
 }

Added: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.log.impl;
+
+import java.util.regex.Pattern;
+
+/**
+ * Simple class to provide format parsing of log messages similar to what slf4j library does.
+ * @author <a href="mailto:dkuleshov at exoplatform.com">Dmitry Kuleshov</a>
+ */
+public class LogMessageFormatter
+{
+   /**
+    * '{}' - anchor which will be replaced by corresponding 
+    * object's string representation
+    */
+   private static final Pattern REPLACE_PATTERN = Pattern.compile("\\{\\}");
+
+   public static String getMessage(String str, Object... argsArray)
+   {
+
+      if (argsArray != null && argsArray.length > 0)
+      {
+         for (int i = 0; i < argsArray.length; i++)
+         {
+            if (i != argsArray.length - 1 || !(argsArray[i] instanceof Throwable))
+            {
+               str = REPLACE_PATTERN.matcher(str).replaceFirst(String.valueOf(argsArray[i]));
+            }
+         }
+      }
+      return str;
+
+
+   }
+
+   public static Throwable getThrowable(Object... argsArray)
+   {
+      if (argsArray != null && argsArray.length > 0)
+      {
+         // we assume that last element in argsArray may be a Throwable
+         if (argsArray[argsArray.length - 1] instanceof Throwable)
+         {
+            return (Throwable)argsArray[argsArray.length - 1];
+         }
+      }
+      return null;
+   }
+}

Modified: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SLF4JExoLog.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SLF4JExoLog.java	2011-08-30 13:21:51 UTC (rev 4812)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SLF4JExoLog.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -187,4 +187,52 @@
    {
       logger.error(String.valueOf(o), throwable);
    }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void trace(String format, Object... argsArray)
+   {
+      logger.trace(format, argsArray);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void debug(String format, Object... argsArray)
+   {
+      logger.debug(format, argsArray);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void info(String format, Object... argsArray)
+   {
+      logger.info(format, argsArray);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void warn(String format, Object... argsArray)
+   {
+      logger.warn(format, argsArray);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void error(String format, Object... argsArray)
+   {
+      logger.error(format, argsArray);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public void fatal(String format, Object... argsArray)
+   {
+      logger.error(format, argsArray);
+   }
 }

Modified: kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SimpleExoLog.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SimpleExoLog.java	2011-08-30 13:21:51 UTC (rev 4812)
+++ kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/SimpleExoLog.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -139,4 +139,40 @@
    {
       this.delegate = new SimpleLog(name);
    }
+
+   public void trace(String format, Object... argsArray)
+   {
+      this.delegate.trace(LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   public void debug(String format, Object... argsArray)
+   {
+      this.delegate.debug(LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   public void info(String format, Object... argsArray)
+   {
+      this.delegate
+         .info(LogMessageFormatter.getMessage(format, argsArray), LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   public void warn(String format, Object... argsArray)
+   {
+      this.delegate
+         .warn(LogMessageFormatter.getMessage(format, argsArray), LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   public void error(String format, Object... argsArray)
+   {
+      this.delegate.error(LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
+
+   public void fatal(String format, Object... argsArray)
+   {
+      this.delegate.fatal(LogMessageFormatter.getMessage(format, argsArray),
+         LogMessageFormatter.getThrowable(argsArray));
+   }
 }

Added: kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java
===================================================================
--- kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.commons.utils;
+
+import junit.framework.TestCase;
+
+import org.exoplatform.services.log.impl.LogMessageFormatter;
+
+/**
+ * @author <a href="mailto:dkuleshov at exoplatform.com">Dmitry Kuleshov</a>
+ * @version $Revision$
+ */
+public class TestLogMessageFormatter extends TestCase
+{
+   public void testMessageParsing() throws Exception
+   {
+      assertEquals("Sometimes even the wisest of man or machine can make an error.",
+         LogMessageFormatter.getMessage("Sometimes even the wisest of man or machine can make an {}.", "error"));
+
+      assertEquals("Time makes all things possible. I can wait.",
+         LogMessageFormatter.getMessage("Time makes all {} possible. I can wait{}", "things", "."));
+
+      assertEquals("Give it up, Megatron!", LogMessageFormatter.getMessage("Give it up, Megatron!", new Throwable()));
+
+      assertEquals("Just remember, there's a thin line between being a hero and being a memory.",
+         LogMessageFormatter.getMessage("Just remember, there's a thin line between being a hero and being a {}.",
+            "memory", new Throwable()));
+
+      assertEquals("We can't stand by and watch the destruction of this beautiful planet.",
+         LogMessageFormatter.getMessage("{}", "We can't stand by and watch the destruction of this beautiful planet.",
+            new Throwable()));
+   }
+   
+   public void testGetThrowable() throws Exception
+   {
+      Throwable t = new Throwable();
+
+      assertNull(LogMessageFormatter.getThrowable());
+      assertNull(LogMessageFormatter.getThrowable(new Object()));
+      assertNull(LogMessageFormatter.getThrowable(new Object(), t, new String()));
+      assertEquals(t, LogMessageFormatter.getThrowable(new Object(), t));
+      assertEquals(t, LogMessageFormatter.getThrowable("Just testing{}", new Object(), t));
+   }
+}

Modified: kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/log/TestLoggers.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/log/TestLoggers.java	2011-08-30 13:21:51 UTC (rev 4812)
+++ kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/log/TestLoggers.java	2011-08-30 13:45:20 UTC (rev 4813)
@@ -120,6 +120,9 @@
       Log log = ExoLogger.getLogger(TestLoggers.class);
 
       log.info("Log4j Container Tests");
+      log.info("Log4j Container {}", "Tests");
+      log.info("Log4j Conta{} Te{}", "iner", "sts");
+      log.info("Log4j Container Tests", 1, 2, 3);
       logOut(log);
 
    }



More information about the exo-jcr-commits mailing list