[jbosscache-commits] JBoss Cache SVN: r7399 - in core/trunk/src/test: java/org/jboss/cache/util/log4j and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Thu Jan 8 06:44:20 EST 2009


Author: mircea.markus
Date: 2009-01-08 06:44:20 -0500 (Thu, 08 Jan 2009)
New Revision: 7399

Added:
   core/trunk/src/test/java/org/jboss/cache/util/log4j/
   core/trunk/src/test/java/org/jboss/cache/util/log4j/MultipleFilesQuiteWriter.java
   core/trunk/src/test/java/org/jboss/cache/util/log4j/PerTestFileAppender.java
Modified:
   core/trunk/src/test/resources/log4j.xml
Log:
added utility class for logging

Added: core/trunk/src/test/java/org/jboss/cache/util/log4j/MultipleFilesQuiteWriter.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/log4j/MultipleFilesQuiteWriter.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/util/log4j/MultipleFilesQuiteWriter.java	2009-01-08 11:44:20 UTC (rev 7399)
@@ -0,0 +1,153 @@
+package org.jboss.cache.util.log4j;
+
+import org.apache.log4j.helpers.QuietWriter;
+import org.apache.log4j.spi.ErrorHandler;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Mircea.Markus at jboss.com
+ */
+public class MultipleFilesQuiteWriter extends QuietWriter
+{
+
+   public static final String DEFAULT_LOG_FILE = "logs/default.log";
+   private static Map<String, QuietWriter> testName2Qw = new HashMap<String, QuietWriter>();
+
+   public MultipleFilesQuiteWriter(ErrorHandler errorHandler)
+   {
+      super(new Writer()
+      {
+         public void write(char cbuf[], int off, int len) throws IOException
+         {
+            throw new UnsupportedOperationException("Not implemented");//sholdn't be called
+         }
+
+         public void flush() throws IOException
+         {
+            throw new UnsupportedOperationException("Not implemented");//sholdn't be called
+         }
+
+         public void close() throws IOException
+         {
+            throw new UnsupportedOperationException("Not implemented");//sholdn't be called
+         }
+      }, errorHandler);
+      File file = new File("logs");
+      if (!file.isDirectory()) file.mkdir();
+   }
+
+   public void write(String string)
+   {
+      try
+      {
+         QuietWriter qw = getQuietWriter();
+         qw.write(string);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   private QuietWriter getQuietWriter()
+         throws IOException
+   {
+      String logFile = getTestClass();
+      if (logFile == null)
+      {
+         logFile = DEFAULT_LOG_FILE;
+      } else
+      {
+         logFile = "logs/" + logFile;
+      }
+      QuietWriter qw = testName2Qw.get(logFile);
+      if (qw == null)
+      {
+         File file = new File(logFile);
+         if (file.exists())
+         {
+            file.delete();
+            file.createNewFile();
+         }
+         FileOutputStream ostream = new FileOutputStream(file);
+         Writer writer = new OutputStreamWriter(ostream);
+         qw = new QuietWriter(writer, errorHandler);
+         testName2Qw.put(logFile, qw);
+      }
+      return qw;
+   }
+
+   public void flush()
+   {
+      for (QuietWriter qw : testName2Qw.values())
+      {
+         qw.flush();
+      }
+   }
+
+   public void close() throws IOException
+   {
+      for (QuietWriter qw : testName2Qw.values())
+      {
+         qw.close();
+      }
+   }
+
+
+   public void write(int c) throws IOException
+   {
+      getQuietWriter().write(c);
+   }
+
+   public void write(char cbuf[], int off, int len) throws IOException
+   {
+      getQuietWriter().write(cbuf, off, len);
+   }
+
+   public void write(String str, int off, int len) throws IOException
+   {
+      getQuietWriter().write(str, off, len);
+   }
+
+   public void write(char cbuf[]) throws IOException
+   {
+      getQuietWriter().write(cbuf);
+   }
+
+   public Writer append(CharSequence csq) throws IOException
+   {
+      return getQuietWriter().append(csq);
+   }
+
+   public Writer append(CharSequence csq, int start, int end) throws IOException
+   {
+      return getQuietWriter().append(csq, start, end);
+   }
+
+   public Writer append(char c) throws IOException
+   {
+      return getQuietWriter().append(c);
+   }
+
+   public String getTestClass()
+   {
+      StackTraceElement[] stack = Thread.currentThread().getStackTrace();
+      if (stack.length == 0) return null;
+      for (int i = stack.length - 1; i > 0; i--)
+      {
+         StackTraceElement e = stack[i];
+         String className = e.getClassName();
+         if (className.indexOf("org.jboss.cache") != -1) return getFileName(className); //+ "." + e.getMethodName();
+      }
+      return null;
+   }
+
+   private String getFileName(String className)
+   {
+      String noPackageStr = className.substring("org.jboss.cache.".length());
+      return noPackageStr.replace('.', '_') + ".log";
+   }
+}

Added: core/trunk/src/test/java/org/jboss/cache/util/log4j/PerTestFileAppender.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/log4j/PerTestFileAppender.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/util/log4j/PerTestFileAppender.java	2009-01-08 11:44:20 UTC (rev 7399)
@@ -0,0 +1,21 @@
+package org.jboss.cache.util.log4j;
+
+import org.apache.log4j.WriterAppender;
+import org.apache.log4j.helpers.QuietWriter;
+
+/**
+ * @author Mircea.Markus at jboss.com
+ */
+public class PerTestFileAppender extends WriterAppender
+{
+
+   public PerTestFileAppender()
+   {
+   }
+
+   public void activateOptions()
+   {
+      super.activateOptions();
+      this.qw = new MultipleFilesQuiteWriter(getErrorHandler());
+   }
+}

Modified: core/trunk/src/test/resources/log4j.xml
===================================================================
--- core/trunk/src/test/resources/log4j.xml	2009-01-08 09:40:36 UTC (rev 7398)
+++ core/trunk/src/test/resources/log4j.xml	2009-01-08 11:44:20 UTC (rev 7399)
@@ -30,6 +30,19 @@
         </layout>
     </appender>
 
+    <!--<appender name="PER_TEST" class="org.jboss.cache.util.log4j.PerTestFileAppender">-->
+        <!--<param name="Threshold" value="TRACE"/>-->
+
+        <!--<layout class="org.apache.log4j.PatternLayout">-->
+            <!-- The default pattern: Date Priority [Category] Message\n -->
+            <!--<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>-->
+
+            <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n-->
+           <!--<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>-->
+            <!---->-->
+        <!--</layout>-->
+    <!--</appender>-->
+
     <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
         <param name="Threshold" value="TRACE"/>
         <param name="Target" value="System.out"/>




More information about the jbosscache-commits mailing list