Logging Interceptor with pretty formatting
------------------------------------------

                 Key: CXF-1327
                 URL: https://issues.apache.org/jira/browse/CXF-1327
             Project: CXF
          Issue Type: Wish
          Components: Tooling
            Reporter: Davide Gesino
            Priority: Trivial


LoggingInIterceptor and LoggingOutInterceptor are really useful, but sometimes 
it is difficult to read the SOAP messages without having a "pretty" identation, 
I would a LOG that  outputs a nice formatted string with XML identation.
Modifying the LoggingOutInterceptor something similar to the example I paste in 
the following, obtained modifying the LoggingOutInterceptor.
I hav not included the length limit and used the jdom api instead of stax.
 :

public class PrettyLoggingOutInterceptor extends AbstractPhaseInterceptor {

        private final Log LOG = LogFactory
                        .getLog(PrettyLoggingOutInterceptor.class);

        private SAXBuilder saxBuilder = new SAXBuilder();
        private XMLOutputter xmlOutputter = new XMLOutputter();

        public PrettyLoggingOutInterceptor() {
                super(Phase.PRE_STREAM);
                addBefore(StaxOutInterceptor.class.getName());
        }

        public void handleMessage(Message message) throws Fault {
                final OutputStream os = message.getContent(OutputStream.class);
                if (os == null) {
                        return;
                }
                if (!LOG.isInfoEnabled()) {
                        return;
                }

                // Write the output while caching it for the log message
                final CacheAndWriteOutputStream newOut = new 
CacheAndWriteOutputStream( os);
                message.setContent(OutputStream.class, newOut);
                newOut.registerCallback(new LoggingCallback());
        }

        class LoggingCallback implements CachedOutputStreamCallback {

                public void onFlush(CachedOutputStream cos) {

                }

                public void onClose(CachedOutputStream cos) {

                        try {
                                Document jdoCument = 
saxBuilder.build(cos.getInputStream());
                                
xmlOutputter.setFormat(Format.getPrettyFormat());
                                StringWriter writer = new StringWriter();
                                xmlOutputter.output(jdoCument, writer);
                                LOG.info(writer.getBuffer().toString());
                        } catch (Exception e) {
                                LOG.error("fatal parsing the SOAP message");
                                LOG.error(e);
                        }
                }
        }
}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to