Class HighlightConverter

  • All Implemented Interfaces:
    AnsiConverter, PatternConverter

    @Plugin(name="highlight",
            category="Converter")
    public final class HighlightConverter
    extends LogEventPatternConverter
    implements AnsiConverter
    Highlight pattern converter. Formats the result of a pattern using a color appropriate for the Level in the LogEvent.

    For example:

     %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}
     

    You can define custom colors for each Level:

     %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan,
     TRACE=black}
     

    You can use a predefined style:

     %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{STYLE=DEFAULT}
     

    The available predefined styles are:

    • Default
    • Log4j - The same as Default
    • Logback

    You can use whitespace around the comma and equal sign. The names in values MUST come from the AnsiEscape enum, case is normalized to upper-case internally.

    To disable ANSI output unconditionally, specify an additional option disableAnsi=true, or to disable ANSI output if no console is detected, specify option noConsoleNoAnsi=true e.g..

     %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{STYLE=DEFAULT, noConsoleNoAnsi=true}
     
    • Field Detail

      • DEFAULT_STYLES

        private static final java.util.Map<java.lang.String,​java.lang.String> DEFAULT_STYLES
      • LOGBACK_STYLES

        private static final java.util.Map<java.lang.String,​java.lang.String> LOGBACK_STYLES
      • DISABLE_ANSI_KEY

        private static final java.lang.String DISABLE_ANSI_KEY
        See Also:
        Constant Field Values
      • NO_CONSOLE_NO_ANSI_KEY

        private static final java.lang.String NO_CONSOLE_NO_ANSI_KEY
        See Also:
        Constant Field Values
      • STYLE_KEY_DEFAULT

        private static final java.lang.String STYLE_KEY_DEFAULT
        See Also:
        Constant Field Values
      • STYLE_KEY_LOGBACK

        private static final java.lang.String STYLE_KEY_LOGBACK
        See Also:
        Constant Field Values
      • STYLES

        private static final java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.String>> STYLES
      • levelStyles

        private final java.util.Map<java.lang.String,​java.lang.String> levelStyles
      • patternFormatters

        private final java.util.List<PatternFormatter> patternFormatters
      • noAnsi

        private final boolean noAnsi
      • defaultStyle

        private final java.lang.String defaultStyle
    • Constructor Detail

      • HighlightConverter

        private HighlightConverter​(java.util.List<PatternFormatter> patternFormatters,
                                   java.util.Map<java.lang.String,​java.lang.String> levelStyles,
                                   boolean noAnsi)
        Construct the converter.
        Parameters:
        patternFormatters - The PatternFormatters to generate the text to manipulate.
        noAnsi - If true, do not output ANSI escape codes.
    • Method Detail

      • createLevelStyleMap

        private static java.util.Map<java.lang.String,​java.lang.String> createLevelStyleMap​(java.lang.String[] options)
        Creates a level style map where values are ANSI escape sequences given configuration options in option[1] .

        The format of the option string in option[1] is:

         Level1=Value, Level2=Value, ...
         

        For example:

         ERROR=red bold, WARN=yellow bold, INFO=green, ...
         

        You can use whitespace around the comma and equal sign. The names in values MUST come from the AnsiEscape enum, case is normalized to upper-case internally.

        Parameters:
        options - The second slot can optionally contain the style map.
        Returns:
        a new map
      • newInstance

        public static HighlightConverter newInstance​(Configuration config,
                                                     java.lang.String[] options)
        Gets an instance of the class.
        Parameters:
        config - The current Configuration.
        options - pattern options, may be null. If first element is "short", only the first line of the throwable will be formatted.
        Returns:
        instance of class.
      • format

        public void format​(LogEvent event,
                           java.lang.StringBuilder toAppendTo)
        Formats an event into a string buffer.
        Specified by:
        format in class LogEventPatternConverter
        Parameters:
        event - event to format, may not be null.
        toAppendTo - string buffer to which the formatted event will be appended. May not be null.
      • getLevelStyle

        java.lang.String getLevelStyle​(Level level)
      • handlesThrowable

        public boolean handlesThrowable()
        Description copied from class: LogEventPatternConverter
        Tests whether this pattern converter is renders a Throwable.

        The PatternParser checks this flag when processing the alwaysWriteExceptions option: if no converter in the pattern handles throwables, the parser automatically appends a converter to ensure exceptions are still written.

        Overrides:
        handlesThrowable in class LogEventPatternConverter
        Returns:
        true if this converter consumes and renders a Throwable, false otherwise