@Test public void testLevelNamesGood() { String colorName = "red"; final String[] options = { "%-5level: %msg", PatternParser.NO_CONSOLE_NO_ANSI + "=false, " + PatternParser.DISABLE_ANSI + "=false, " + "DEBUG=" + colorName + ", TRACE=" + colorName }; final HighlightConverter converter = HighlightConverter.newInstance(null, options); Assert.assertNotNull(converter); Assert.assertEquals(AnsiEscape.createSequence(colorName), converter.getLevelStyle(Level.TRACE)); Assert.assertEquals(AnsiEscape.createSequence(colorName), converter.getLevelStyle(Level.DEBUG)); }
/** * Creates an ANSI escape sequence from the given {@linkplain AnsiEscape} names. * * @param names * {@linkplain AnsiEscape} names. * @return An ANSI escape sequence. */ public static String createSequence(final String... names) { if (names == null) { return getDefaultStyle(); } final StringBuilder sb = new StringBuilder(AnsiEscape.CSI.getCode()); boolean first = true; for (final String name : names) { try { final AnsiEscape escape = EnglishEnums.valueOf(AnsiEscape.class, name.trim()); if (!first) { sb.append(AnsiEscape.SEPARATOR.getCode()); } first = false; sb.append(escape.getCode()); } catch (final Exception ex) { // Ignore the error. } } sb.append(AnsiEscape.SUFFIX.getCode()); return sb.toString(); }
/** * Constructs the converter. * * @param patternFormatters * The PatternFormatters to generate the text to manipulate. * @param style * The style that should encapsulate the pattern. * @param noAnsi * If true, do not output ANSI escape codes. */ private StyleConverter(final List<PatternFormatter> patternFormatters, final String style, final boolean noAnsi) { super("style", "style"); this.patternFormatters = patternFormatters; this.style = style; this.defaultStyle = AnsiEscape.getDefaultStyle(); this.noAnsi = noAnsi; }
/** * Creates a Map from a source array where values are ANSI escape sequences. The format is: * * <pre> * Key1=Value, Key2=Value, ... * </pre> * * For example: * * <pre> * ERROR=red bold, WARN=yellow bold, INFO=green, ... * </pre> * * You can use whitespace around the comma and equal sign. The names in values MUST come from the * {@linkplain AnsiEscape} enum, case is normalized to upper-case internally. * * @param values the source string to parse. * @param dontEscapeKeys do not escape these keys, leave the values as is in the map * @return a new map */ public static Map<String, String> createMap(final String values, final String[] dontEscapeKeys) { return createMap(values.split(Patterns.COMMA_SEPARATOR), dontEscapeKeys); }
/** * Construct the converter. * * @param patternFormatters * The PatternFormatters to generate the text to manipulate. * @param noAnsi * If true, do not output ANSI escape codes. */ private HighlightConverter(final List<PatternFormatter> patternFormatters, final Map<Level, String> levelStyles, final boolean noAnsi) { super("style", "style"); this.patternFormatters = patternFormatters; this.levelStyles = levelStyles; this.defaultStyle = AnsiEscape.getDefaultStyle(); this.noAnsi = noAnsi; }
.replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY); final Map<String, String> styles = AnsiEscape.createMap(string, new String[] {STYLE_KEY}); final Map<Level, String> levelStyles = new HashMap<>(DEFAULT_STYLES); for (final Map.Entry<String, String> entry : styles.entrySet()) {
final String value = keyValue[1]; final boolean escape = Arrays.binarySearch(sortedIgnoreKeys, key) < 0; map.put(key, escape ? createSequence(value.split("\\s")) : value);
/** * {@inheritDoc} */ @Override @PerformanceSensitive("allocation") public void format(final LogEvent event, final StringBuilder toAppendTo) { final int start = toAppendTo.length(); for (int i = 0; i < formatters.size(); i++) { final PatternFormatter formatter = formatters.get(i); formatter.format(event, toAppendTo); } if (toAppendTo.length() > start) { toAppendTo.insert(start, style); toAppendTo.append(AnsiEscape.getDefaultStyle()); } } }
final String style = AnsiEscape.createSequence(options[1].split(Patterns.COMMA_SEPARATOR)); final boolean disableAnsi = Arrays.toString(options).contains(PatternParser.DISABLE_ANSI + "=true"); final boolean noConsoleNoAnsi = Arrays.toString(options).contains(PatternParser.NO_CONSOLE_NO_ANSI + "=true");
return constructor.newInstance(formatters, AnsiEscape.createSequence(name)); } catch (final SecurityException e) { LOGGER.error(e.toString(), e);