Class PatternLayout

  • All Implemented Interfaces:
    LocationAware, Layout<String>, Encoder<LogEvent>, StringLayout

    @Plugin(name="PatternLayout",
            category="Core",
            elementType="layout",
            printObject=true)
    public final class PatternLayout
    extends AbstractStringLayout
    A flexible layout configurable with pattern string.

    The goal of this class is to format a LogEvent and return the results. The format of the result depends on the conversion pattern.

    The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.

    See the Log4j Manual for details on the supported pattern converters.

    • Field Detail

      • DEFAULT_CONVERSION_PATTERN

        public static final String DEFAULT_CONVERSION_PATTERN
        Default pattern string for log output. Currently set to the string "%m%n" which just prints the application supplied message.
        See Also:
        Constant Field Values
      • TTCC_CONVERSION_PATTERN

        public static final String TTCC_CONVERSION_PATTERN
        A conversion pattern equivalent to the TTCCLayout. Current value is %r [%t] %p %c %notEmpty{%x }- %m%n.
        See Also:
        Constant Field Values
      • SIMPLE_CONVERSION_PATTERN

        public static final String SIMPLE_CONVERSION_PATTERN
        A simple pattern. Current value is %d [%t] %p %c - %m%n.
        See Also:
        Constant Field Values
    • Method Detail

      • createSerializer

        @Deprecated
        public static AbstractStringLayout.Serializer createSerializer​(Configuration configuration,
                                                                       RegexReplacement replace,
                                                                       String pattern,
                                                                       String defaultPattern,
                                                                       PatternSelector patternSelector,
                                                                       boolean alwaysWriteExceptions,
                                                                       boolean noConsoleNoAnsi)
        Deprecated.
        Deprecated, use newSerializerBuilder() instead.
        Parameters:
        configuration - the current configuration
        replace - Allows portions of the resulting String to be replaced.
        pattern - the current pattern
        defaultPattern - the default pattern
        patternSelector - Allows different patterns to be used with the PatternLayout based on some selection criteria.
        alwaysWriteExceptions - To always write exceptions even if the pattern contains no exception conversions.
        noConsoleNoAnsi - Do not output ANSI escape codes if System.console() is null.
        Returns:
        a new Serializer
      • getConversionPattern

        public String getConversionPattern()
        Gets the conversion pattern.
        Returns:
        the conversion pattern.
      • getContentFormat

        public Map<String,​String> getContentFormat()
        Gets this PatternLayout's content format. Specified by:
        • Key: "structured" Value: "false"
        • Key: "formatType" Value: "conversion" (format uses the keywords supported by OptionConverter)
        • Key: "format" Value: provided "conversionPattern" param
        Specified by:
        getContentFormat in interface Layout<String>
        Overrides:
        getContentFormat in class AbstractLayout<String>
        Returns:
        Map of content format keys supporting PatternLayout
      • toSerializable

        public String toSerializable​(LogEvent event)
        Formats a logging event to a writer.
        Parameters:
        event - logging event to be formatted.
        Returns:
        The event formatted as a String.
      • encode

        public void encode​(LogEvent event,
                           ByteBufferDestination destination)
        Description copied from class: AbstractLayout
        Encodes the specified source LogEvent to some binary representation and writes the result to the specified destination.

        The default implementation of this method delegates to the Layout.toByteArray(LogEvent) method which allocates temporary objects.

        Subclasses can override this method to provide a garbage-free implementation. For text-based layouts, AbstractStringLayout provides various convenience methods to help with this:

         @Plugin(name = "MyLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
         public final class MyLayout extends AbstractStringLayout {
             @Override
             public void encode(LogEvent event, ByteBufferDestination destination) {
                 StringBuilder text = getStringBuilder();
                 convertLogEventToText(event, text);
                 getStringBuilderEncoder().encode(text, destination);
             }
        
             private void convertLogEventToText(LogEvent event, StringBuilder destination) {
                 ... // append a text representation of the log event to the StringBuilder
             }
         }
         
        Specified by:
        encode in interface Encoder<LogEvent>
        Overrides:
        encode in class AbstractLayout<String>
        Parameters:
        event - the LogEvent to encode.
        destination - holds the ByteBuffer to write into.
        See Also:
        AbstractStringLayout.getStringBuilder(), AbstractStringLayout.getStringBuilderEncoder()
      • createPatternParser

        public static PatternParser createPatternParser​(Configuration config)
        Creates a PatternParser.
        Parameters:
        config - The Configuration or null.
        Returns:
        The PatternParser.
      • createDefaultLayout

        public static PatternLayout createDefaultLayout()
        Creates a PatternLayout using the default options. These options include using UTF-8, the default conversion pattern, exceptions being written, and with ANSI escape codes.
        Returns:
        the PatternLayout.
        See Also:
        Default conversion pattern
      • createDefaultLayout

        public static PatternLayout createDefaultLayout​(Configuration configuration)
        Creates a PatternLayout using the default options and the given configuration. These options include using UTF-8, the default conversion pattern, exceptions being written, and with ANSI escape codes.
        Parameters:
        configuration - The Configuration.
        Returns:
        the PatternLayout.
        See Also:
        Default conversion pattern