logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1172062 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src: main/java/org/apache/logging/log4j/spi/ test/java/org/apache/logging/log4j/
Date Sat, 17 Sep 2011 20:25:37 GMT
Author: rgoers
Date: Sat Sep 17 20:25:36 2011
New Revision: 1172062

URL: http://svn.apache.org/viewvc?rev=1172062&view=rev
Log:
Get Throwable from ParameterizedMessage and add to argument list on log method.

Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/LoggerTest.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java?rev=1172062&r1=1172061&r2=1172062&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
Sat Sep 17 20:25:36 2011
@@ -275,7 +275,8 @@ public abstract class AbstractLogger imp
      */
     public void trace(String message, Object... params) {
         if (isEnabled(Level.TRACE, null, message, params)) {
-            log(null, getFQCN(), Level.TRACE, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.TRACE, msg, msg.getThrowable());
         }
     }
 
@@ -288,7 +289,8 @@ public abstract class AbstractLogger imp
      */
     public void trace(Marker marker, String message, Object... params) {
         if (isEnabled(Level.TRACE, marker, message, params)) {
-            log(marker, getFQCN(), Level.TRACE, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.TRACE, msg, msg.getThrowable());
         }
     }
 
@@ -468,7 +470,8 @@ public abstract class AbstractLogger imp
      */
     public void debug(String message, Object... params) {
         if (isEnabled(Level.DEBUG, null, message, params)) {
-            log(null, getFQCN(), Level.DEBUG, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.DEBUG, msg, msg.getThrowable());
         }
     }
 
@@ -481,7 +484,8 @@ public abstract class AbstractLogger imp
      */
     public void debug(Marker marker, String message, Object... params) {
         if (isEnabled(Level.DEBUG, marker, message, params)) {
-            log(marker, getFQCN(), Level.DEBUG, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.DEBUG, msg, msg.getThrowable());
         }
     }
 
@@ -663,7 +667,8 @@ public abstract class AbstractLogger imp
      */
     public void info(String message, Object... params) {
         if (isEnabled(Level.INFO, null, message, params)) {
-            log(null, getFQCN(), Level.INFO, new ParameterizedMessage(message, params), null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.INFO, msg, msg.getThrowable());
         }
     }
 
@@ -676,7 +681,8 @@ public abstract class AbstractLogger imp
      */
     public void info(Marker marker, String message, Object... params) {
         if (isEnabled(Level.INFO, marker, message, params)) {
-            log(marker, getFQCN(), Level.INFO, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.INFO, msg, msg.getThrowable());
         }
     }
 
@@ -856,7 +862,8 @@ public abstract class AbstractLogger imp
      */
     public void warn(String message, Object... params) {
         if (isEnabled(Level.WARN, null, message, params)) {
-            log(null, getFQCN(), Level.WARN, new ParameterizedMessage(message, params), null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.WARN, msg, msg.getThrowable());
         }
     }
 
@@ -869,7 +876,8 @@ public abstract class AbstractLogger imp
      */
     public void warn(Marker marker, String message, Object... params) {
         if (isEnabled(Level.WARN, marker, message, params)) {
-            log(marker, getFQCN(), Level.WARN, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.WARN, msg, msg.getThrowable());
         }
     }
 
@@ -1050,7 +1058,8 @@ public abstract class AbstractLogger imp
      */
     public void error(String message, Object... params) {
         if (isEnabled(Level.ERROR, null, message, params)) {
-            log(null, getFQCN(), Level.ERROR, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.ERROR, msg, msg.getThrowable());
         }
     }
 
@@ -1063,7 +1072,8 @@ public abstract class AbstractLogger imp
      */
     public void error(Marker marker, String message, Object... params) {
         if (isEnabled(Level.ERROR, marker, message, params)) {
-            log(marker, getFQCN(), Level.ERROR, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.ERROR, msg, msg.getThrowable());
         }
     }
 
@@ -1246,7 +1256,8 @@ public abstract class AbstractLogger imp
      */
     public void fatal(String message, Object... params) {
         if (isEnabled(Level.FATAL, null, message, params)) {
-            log(null, getFQCN(), Level.FATAL, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(null, getFQCN(), Level.FATAL, msg, msg.getThrowable());
         }
     }
 
@@ -1259,7 +1270,8 @@ public abstract class AbstractLogger imp
      */
     public void fatal(Marker marker, String message, Object... params) {
         if (isEnabled(Level.FATAL, marker, message, params)) {
-            log(marker, getFQCN(), Level.FATAL, new ParameterizedMessage(message, params),
null);
+            ParameterizedMessage msg = new ParameterizedMessage(message, params);
+            log(marker, getFQCN(), Level.FATAL, msg, msg.getThrowable());
         }
     }
 

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/LoggerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/LoggerTest.java?rev=1172062&r1=1172061&r2=1172062&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/LoggerTest.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/LoggerTest.java
Sat Sep 17 20:25:36 2011
@@ -17,26 +17,45 @@
 package org.apache.logging.log4j;
 
 import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.Date;
+import java.util.List;
 import java.util.Locale;
 
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+
 /**
  *
  */
 public class LoggerTest {
 
-    Logger logger = LogManager.getLogger("LoggerTest");
+    SimpleLogger logger = (SimpleLogger) LogManager.getLogger("LoggerTest");
+    List<String> results = logger.getEntries();
+
+    @Before
+    public void setup() {
+        results.clear();
+    }
+
     @Test
     public void basicFlow() {
         logger.entry();
         logger.exit();
+        assertEquals(2, results.size());
+        assertTrue("Incorrect Entry", results.get(0).startsWith(" TRACE  entry"));
+        assertTrue("incorrect Exit", results.get(1).startsWith(" TRACE  exit"));
+
     }
 
     @Test
     public void throwing() {
         logger.throwing(new IllegalArgumentException("Test Exception"));
+        assertEquals(1, results.size());
+        assertTrue("Incorrect Throwing",
+            results.get(0).startsWith(" ERROR throwing java.lang.IllegalArgumentException:
Test Exception"));
     }
 
     @Test
@@ -45,22 +64,39 @@ public class LoggerTest {
             throw new NullPointerException();
         } catch (Exception e) {
             logger.catching(e);
+            assertEquals(1, results.size());
+            assertTrue("Incorrect Catching",
+                results.get(0).startsWith(" DEBUG catching java.lang.NullPointerException"));
         }
     }
 
     @Test
     public void debug() {
         logger.debug("Debug message");
+        assertEquals(1, results.size());
+        assertTrue("Incorrect message", results.get(0).startsWith(" DEBUG Debug message"));
     }
 
     @Test
     public void debugObject() {
         logger.debug(new Date());
+        assertEquals(1, results.size());
+        assertTrue("Invalid length", results.get(0).length() > 7);
     }
 
     @Test
     public void debugWithParms() {
         logger.debug("Hello, {}", "World");
+        assertEquals(1, results.size());
+        assertTrue("Incorrect substitution", results.get(0).startsWith(" DEBUG Hello, World"));
+    }
+
+    @Test
+    public void debugWithParmsAndThrowable() {
+        logger.debug("Hello, {}", "World", new RuntimeException("Test Exception"));
+        assertEquals(1, results.size());
+        assertTrue("Unexpected results: " + results.get(0),
+            results.get(0).startsWith(" DEBUG Hello, World java.lang.RuntimeException: Test
Exception"));
     }
 
     @Test
@@ -70,6 +106,11 @@ public class LoggerTest {
         logger.debug("Debug message");
         ThreadContext.clear();
         logger.debug("Debug message");
+        assertEquals(2, results.size());
+        assertTrue("Incorrect MDC: " + results.get(0),
+            results.get(0).startsWith(" DEBUG Debug message {TestYear=2010}"));
+        assertTrue("MDC not cleared?: " + results.get(1),
+            results.get(1).startsWith(" DEBUG Debug message"));
     }
 
     @Test
@@ -83,5 +124,8 @@ public class LoggerTest {
         msg.put("Amount", "200.00");
         logger.info(MarkerManager.getMarker("EVENT"), msg);
         ThreadContext.clear();
+        assertEquals(1, results.size());
+        assertTrue("Incorrect structured data: " + results.get(0),results.get(0).startsWith(
+            " INFO Transfer [Audit@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"]
Transfer Complete"));
     }
 }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java?rev=1172062&r1=1172061&r2=1172062&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
Sat Sep 17 20:25:36 2011
@@ -21,17 +21,25 @@ import org.apache.logging.log4j.spi.Abst
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 /**
  *
  */
 public class SimpleLogger extends AbstractLogger {
+    private List<String> array = new ArrayList<String>();
+
     @Override
     protected String getFQCN() {
         return SimpleLogger.class.getName();
     }
 
+    public List<String> getEntries() {
+        return array;
+    }
+
     @Override
     public void log(Marker marker, String fqcn, Level level, Message msg, Throwable throwable)
{
         StringBuilder sb = new StringBuilder();
@@ -58,7 +66,8 @@ public class SimpleLogger extends Abstra
             t.printStackTrace(new PrintStream(baos));
             sb.append(baos.toString());
         }
-        System.out.println(sb.toString());
+        array.add(sb.toString());
+        //System.out.println(sb.toString());
     }
 
     @Override



Mime
View raw message