Uploaded image for project: 'CXF'
  1. CXF
  2. CXF-1327

Logging Interceptor with pretty formatting

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 2.0.10, 2.1.4
    • Tooling
    • None

    Description

      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); }

      }
      }
      }

      Attachments

        Activity

          People

            seanoc Sean O'Callaghan
            dgesino Davide Gesino
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: