Class XSLTLayout

  • All Implemented Interfaces:
    org.apache.log4j.spi.OptionHandler, org.apache.log4j.xml.UnrecognizedElementHandler

    public final class XSLTLayout
    extends org.apache.log4j.Layout
    implements org.apache.log4j.xml.UnrecognizedElementHandler
    XSLTLayout transforms each event as a document using a specified or default XSLT transform. The default XSLT transform produces a result similar to XMLLayout. When used with a FileAppender or similar, the transformation of an event will be appended to the results for previous transforms. If each transform results in an XML element, then resulting file will only be an XML entity since an XML document requires one and only one top-level element. To process the entity, reference it in a XML document like so:
      <!DOCTYPE log4j:eventSet [<!ENTITY data SYSTEM "data.xml">]>
    
      <log4j:eventSet xmlns:log4j="http://jakarta.apache.org/log4j/">
        &data
      </log4j:eventSet>
    
     
    The layout will detect the encoding and media-type specified in the transform. If no encoding is specified in the transform, an xsl:output element specifying the US-ASCII encoding will be inserted before processing the transform. If an encoding is specified in the transform, the same encoding should be explicitly specified for the appender. Extracting MDC values can be expensive when used with log4j releases prior to 1.2.15. Output of MDC values is enabled by default but be suppressed by setting properties to false. Extracting location info can be expensive regardless of log4j version. Output of location info is disabled by default but can be enabled by setting locationInfo to true. Embedded transforms in XML configuration should not depend on namespace prefixes defined earlier in the document as namespace aware parsing in not generally performed when using DOMConfigurator. The transform will serialize and reparse to get the namespace aware document needed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean activated
      Whether activateOptions has been called.
      private java.nio.charset.Charset encoding
      Encoding extracted from XSLT transform.
      private boolean ignoresThrowable
      Whether throwable information should be ignored.
      private boolean locationInfo
      Whether location information should be written.
      private static java.lang.String LOG4J_NS
      Namespace for log4j events.
      private java.lang.String mediaType
      media-type (mime type) extracted from XSLT transform.
      private java.io.ByteArrayOutputStream outputStream
      Output stream.
      private boolean properties
      Whether properties should be extracted.
      private javax.xml.transform.Templates templates
      XSLT templates.
      private javax.xml.transform.sax.SAXTransformerFactory transformerFactory
      Transformer factory.
      private org.apache.log4j.pattern.CachedDateFormat utcDateFormat
      DateFormat for UTC time.
      private static java.lang.String XSLT_NS
      Namespace for XSLT.
      • Fields inherited from class org.apache.log4j.Layout

        LINE_SEP, LINE_SEP_LEN
    • Constructor Summary

      Constructors 
      Constructor Description
      XSLTLayout()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activateOptions()
      java.lang.String format​(org.apache.log4j.spi.LoggingEvent event)
      java.lang.String getContentType()
      boolean getLocationInfo()
      Gets whether location info should be output.
      boolean getProperties()
      Gets whether MDC key-value pairs should be output.
      boolean ignoresThrowable()
      Gets whether throwables should not be output.
      boolean parseUnrecognizedElement​(org.w3c.dom.Element element, java.util.Properties props)
      void setIgnoresThrowable​(boolean ignoresThrowable)
      Sets whether throwables should not be output.
      void setLocationInfo​(boolean flag)
      The LocationInfo option takes a boolean value.
      void setProperties​(boolean flag)
      Sets whether MDC key-value pairs should be output, default false.
      void setTransform​(org.w3c.dom.Document xsltdoc)
      Sets XSLT transform.
      • Methods inherited from class org.apache.log4j.Layout

        getFooter, getHeader
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • XSLT_NS

        private static final java.lang.String XSLT_NS
        Namespace for XSLT.
        See Also:
        Constant Field Values
      • LOG4J_NS

        private static final java.lang.String LOG4J_NS
        Namespace for log4j events.
        See Also:
        Constant Field Values
      • locationInfo

        private boolean locationInfo
        Whether location information should be written.
      • mediaType

        private java.lang.String mediaType
        media-type (mime type) extracted from XSLT transform.
      • encoding

        private java.nio.charset.Charset encoding
        Encoding extracted from XSLT transform.
      • transformerFactory

        private javax.xml.transform.sax.SAXTransformerFactory transformerFactory
        Transformer factory.
      • templates

        private javax.xml.transform.Templates templates
        XSLT templates.
      • outputStream

        private final java.io.ByteArrayOutputStream outputStream
        Output stream.
      • ignoresThrowable

        private boolean ignoresThrowable
        Whether throwable information should be ignored.
      • properties

        private boolean properties
        Whether properties should be extracted.
      • activated

        private boolean activated
        Whether activateOptions has been called.
      • utcDateFormat

        private final org.apache.log4j.pattern.CachedDateFormat utcDateFormat
        DateFormat for UTC time.
    • Constructor Detail

      • XSLTLayout

        public XSLTLayout()
        Default constructor.
    • Method Detail

      • getContentType

        public java.lang.String getContentType()
        Overrides:
        getContentType in class org.apache.log4j.Layout
      • setLocationInfo

        public void setLocationInfo​(boolean flag)
        The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no location information output by this layout. If the the option is set to true, then the file name and line number of the statement at the origin of the log statement will be output.

        If you are embedding this layout within an SMTPAppender then make sure to set the LocationInfo option of that appender as well.

        Parameters:
        flag - new value.
      • getLocationInfo

        public boolean getLocationInfo()
        Gets whether location info should be output.
        Returns:
        if location is output.
      • setProperties

        public void setProperties​(boolean flag)
        Sets whether MDC key-value pairs should be output, default false.
        Parameters:
        flag - new value.
      • getProperties

        public boolean getProperties()
        Gets whether MDC key-value pairs should be output.
        Returns:
        true if MDC key-value pairs are output.
      • activateOptions

        public void activateOptions()
        Specified by:
        activateOptions in interface org.apache.log4j.spi.OptionHandler
      • ignoresThrowable

        public boolean ignoresThrowable()
        Gets whether throwables should not be output.
        Specified by:
        ignoresThrowable in class org.apache.log4j.Layout
        Returns:
        true if throwables should not be output.
      • setIgnoresThrowable

        public void setIgnoresThrowable​(boolean ignoresThrowable)
        Sets whether throwables should not be output.
        Parameters:
        ignoresThrowable - if true, throwables should not be output.
      • format

        public java.lang.String format​(org.apache.log4j.spi.LoggingEvent event)
        Specified by:
        format in class org.apache.log4j.Layout
      • setTransform

        public void setTransform​(org.w3c.dom.Document xsltdoc)
                          throws javax.xml.transform.TransformerConfigurationException
        Sets XSLT transform.
        Parameters:
        xsltdoc - DOM document containing XSLT transform source, may be modified.
        Throws:
        javax.xml.transform.TransformerConfigurationException - if transformer can not be created.
      • parseUnrecognizedElement

        public boolean parseUnrecognizedElement​(org.w3c.dom.Element element,
                                                java.util.Properties props)
                                         throws java.lang.Exception
        Specified by:
        parseUnrecognizedElement in interface org.apache.log4j.xml.UnrecognizedElementHandler
        Throws:
        java.lang.Exception