cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r982491 - in /cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor: AbstractLoggingInterceptor.java LoggingInInterceptor.java LoggingOutInterceptor.java
Date Thu, 05 Aug 2010 07:42:58 GMT
Author: ningjiang
Date: Thu Aug  5 07:42:57 2010
New Revision: 982491

URL: http://svn.apache.org/viewvc?rev=982491&view=rev
Log:
CXF-2931 Add configurable pretty printing to the CXF logging in/out interceptors

Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java?rev=982491&r1=982490&r2=982491&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java
Thu Aug  5 07:42:57 2010
@@ -21,11 +21,20 @@ package org.apache.cxf.interceptor;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.net.URI;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 
@@ -39,6 +48,7 @@ public abstract class AbstractLoggingInt
 
     protected int limit = 100 * 1024;
     protected PrintWriter writer;
+    protected boolean prettyLogging;
     
     
     public AbstractLoggingInterceptor(String phase) {
@@ -77,7 +87,51 @@ public abstract class AbstractLoggingInt
     
     public int getLimit() {
         return limit;
-    }    
+    }
+    
+    public void setPrettyLogging(boolean flag) {
+        prettyLogging = flag;
+    }
+    
+    public boolean isPrettyLogging() {
+        return prettyLogging;
+    }
+    
+    
+    protected void writePayload(StringBuilder builder, CachedOutputStream cos, String encoding)

+        throws Exception {
+        if (isPrettyLogging()) {
+
+            TransformerFactory tfactory = TransformerFactory.newInstance();
+            try {
+                tfactory.setAttribute("indent-number", "2");
+            } catch (Exception ex) {
+                // ignore
+            }
+            Transformer serializer;
+            serializer = tfactory.newTransformer();
+            // Setup indenting to "pretty print"
+            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
+            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
+
+            StringWriter swriter = new StringWriter();
+            serializer.transform(new StreamSource(cos.getInputStream()), new StreamResult(swriter));
+            String result = swriter.toString();
+            if (result.length() < limit || limit == -1) {
+                builder.append(swriter.toString());
+            } else {
+                builder.append(swriter.toString().substring(0, limit));
+            }
+
+        } else {
+            if (StringUtils.isEmpty(encoding)) {
+                cos.writeCacheTo(builder, limit);
+            } else {
+                cos.writeCacheTo(builder, encoding, limit);
+            }
+
+        }
+    }
 
 
     /**

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java?rev=982491&r1=982490&r2=982491&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java Thu
Aug  5 07:42:57 2010
@@ -18,13 +18,11 @@
  */
 package org.apache.cxf.interceptor;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.util.logging.Level;
 
 import org.apache.cxf.common.injection.NoJSR250Annotations;
-import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.message.Message;
@@ -117,14 +115,10 @@ public class LoggingInInterceptor extend
                 if (bos.size() > limit) {
                     buffer.getMessage().append("(message truncated to " + limit + " bytes)\n");
                 }
-                if (StringUtils.isEmpty(encoding)) {
-                    bos.writeCacheTo(buffer.getPayload(), limit);
-                } else {
-                    bos.writeCacheTo(buffer.getPayload(), encoding, limit);
-                }
+                writePayload(buffer.getPayload(), bos, encoding); 
                     
                 bos.close();
-            } catch (IOException e) {
+            } catch (Exception e) {
                 throw new Fault(e);
             }
         }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java?rev=982491&r1=982490&r2=982491&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
Thu Aug  5 07:42:57 2010
@@ -24,7 +24,6 @@ import java.io.PrintWriter;
 import java.util.logging.Level;
 
 import org.apache.cxf.common.injection.NoJSR250Annotations;
-import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.io.CacheAndWriteOutputStream;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.io.CachedOutputStreamCallback;
@@ -134,11 +133,7 @@ public class LoggingOutInterceptor exten
                 }
             }
             try {
-                if (StringUtils.isEmpty(encoding)) {
-                    cos.writeCacheTo(buffer.getPayload(), limit);
-                } else {                    
-                    cos.writeCacheTo(buffer.getPayload(), encoding, limit);
-                }
+                writePayload(buffer.getPayload(), cos, encoding); 
             } catch (Exception ex) {
                 //ignore
             }



Mime
View raw message