"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/HighlightConverter.java" between
apache-log4j-2.11.1-src.tar.gz and apache-log4j-2.11.2-src.tar.gz

About: Apache Log4j 2 is a logging library for Java. Source distribution (Java).

HighlightConverter.java  (apache-log4j-2.11.1-src):HighlightConverter.java  (apache-log4j-2.11.2-src)
skipping to change at line 83 skipping to change at line 83
* </p> * </p>
* <pre> * <pre>
* %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{STYLE=DEFAULT, noC onsoleNoAnsi=true} * %highlight{%d{ ISO8601 } [%t] %-5level: %msg%n%throwable}{STYLE=DEFAULT, noC onsoleNoAnsi=true}
* </pre> * </pre>
*/ */
@Plugin(name = "highlight", category = PatternConverter.CATEGORY) @Plugin(name = "highlight", category = PatternConverter.CATEGORY)
@ConverterKeys({ "highlight" }) @ConverterKeys({ "highlight" })
@PerformanceSensitive("allocation") @PerformanceSensitive("allocation")
public final class HighlightConverter extends LogEventPatternConverter implement s AnsiConverter { public final class HighlightConverter extends LogEventPatternConverter implement s AnsiConverter {
private static final Map<Level, String> DEFAULT_STYLES = new HashMap<>(); private static final Map<String, String> DEFAULT_STYLES = new HashMap<>();
private static final Map<Level, String> LOGBACK_STYLES = new HashMap<>(); private static final Map<String, String> LOGBACK_STYLES = new HashMap<>();
private static final String STYLE_KEY = "STYLE"; private static final String STYLE_KEY = "STYLE";
private static final String STYLE_KEY_DEFAULT = "DEFAULT"; private static final String STYLE_KEY_DEFAULT = "DEFAULT";
private static final String STYLE_KEY_LOGBACK = "LOGBACK"; private static final String STYLE_KEY_LOGBACK = "LOGBACK";
private static final Map<String, Map<Level, String>> STYLES = new HashMap<>( ); private static final Map<String, Map<String, String>> STYLES = new HashMap<> ();
static { static {
// Default styles: // Default styles:
DEFAULT_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BRIGHT", "RED DEFAULT_STYLES.put(Level.FATAL.name(), AnsiEscape.createSequence("BRIGHT
")); ", "RED"));
DEFAULT_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED DEFAULT_STYLES.put(Level.ERROR.name(), AnsiEscape.createSequence("BRIGHT
")); ", "RED"));
DEFAULT_STYLES.put(Level.WARN, AnsiEscape.createSequence("YELLOW")); DEFAULT_STYLES.put(Level.WARN.name(), AnsiEscape.createSequence("YELLOW"
DEFAULT_STYLES.put(Level.INFO, AnsiEscape.createSequence("GREEN")); ));
DEFAULT_STYLES.put(Level.DEBUG, AnsiEscape.createSequence("CYAN")); DEFAULT_STYLES.put(Level.INFO.name(), AnsiEscape.createSequence("GREEN")
DEFAULT_STYLES.put(Level.TRACE, AnsiEscape.createSequence("BLACK")); );
DEFAULT_STYLES.put(Level.DEBUG.name(), AnsiEscape.createSequence("CYAN")
);
DEFAULT_STYLES.put(Level.TRACE.name(), AnsiEscape.createSequence("BLACK"
));
// Logback styles: // Logback styles:
LOGBACK_STYLES.put(Level.FATAL, AnsiEscape.createSequence("BLINK", "BRIG LOGBACK_STYLES.put(Level.FATAL.name(), AnsiEscape.createSequence("BLINK"
HT", "RED")); , "BRIGHT", "RED"));
LOGBACK_STYLES.put(Level.ERROR, AnsiEscape.createSequence("BRIGHT", "RED LOGBACK_STYLES.put(Level.ERROR.name(), AnsiEscape.createSequence("BRIGHT
")); ", "RED"));
LOGBACK_STYLES.put(Level.WARN, AnsiEscape.createSequence("RED")); LOGBACK_STYLES.put(Level.WARN.name(), AnsiEscape.createSequence("RED"));
LOGBACK_STYLES.put(Level.INFO, AnsiEscape.createSequence("BLUE")); LOGBACK_STYLES.put(Level.INFO.name(), AnsiEscape.createSequence("BLUE"))
LOGBACK_STYLES.put(Level.DEBUG, AnsiEscape.createSequence((String[]) nul ;
l)); LOGBACK_STYLES.put(Level.DEBUG.name(), AnsiEscape.createSequence((String
LOGBACK_STYLES.put(Level.TRACE, AnsiEscape.createSequence((String[]) nul []) null));
l)); LOGBACK_STYLES.put(Level.TRACE.name(), AnsiEscape.createSequence((String
[]) null));
// Style map: // Style map:
STYLES.put(STYLE_KEY_DEFAULT, DEFAULT_STYLES); STYLES.put(STYLE_KEY_DEFAULT, DEFAULT_STYLES);
STYLES.put(STYLE_KEY_LOGBACK, LOGBACK_STYLES); STYLES.put(STYLE_KEY_LOGBACK, LOGBACK_STYLES);
} }
/** /**
* Creates a level style map where values are ANSI escape sequences given co nfiguration options in {@code option[1]} * Creates a level style map where values are ANSI escape sequences given co nfiguration options in {@code option[1]}
* . * .
* <p> * <p>
* The format of the option string in {@code option[1]} is: * The format of the option string in {@code option[1]} is:
skipping to change at line 143 skipping to change at line 143
* *
* <p> * <p>
* You can use whitespace around the comma and equal sign. The names in valu es MUST come from the * You can use whitespace around the comma and equal sign. The names in valu es MUST come from the
* {@linkplain AnsiEscape} enum, case is normalized to upper-case internally . * {@linkplain AnsiEscape} enum, case is normalized to upper-case internally .
* </p> * </p>
* *
* @param options * @param options
* The second slot can optionally contain the style map. * The second slot can optionally contain the style map.
* @return a new map * @return a new map
*/ */
private static Map<Level, String> createLevelStyleMap(final String[] options ) { private static Map<String, String> createLevelStyleMap(final String[] option s) {
if (options.length < 2) { if (options.length < 2) {
return DEFAULT_STYLES; return DEFAULT_STYLES;
} }
// Feels like a hack. Should String[] options change to a Map<String,Str ing>? // Feels like a hack. Should String[] options change to a Map<String,Str ing>?
final String string = options[1] final String string = options[1]
.replaceAll(PatternParser.DISABLE_ANSI + "=(true|false)", String s.EMPTY) .replaceAll(PatternParser.DISABLE_ANSI + "=(true|false)", String s.EMPTY)
.replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY); .replaceAll(PatternParser.NO_CONSOLE_NO_ANSI + "=(true|false)", Strings.EMPTY);
// //
final Map<String, String> styles = AnsiEscape.createMap(string, new Stri ng[] {STYLE_KEY}); final Map<String, String> styles = AnsiEscape.createMap(string, new Stri ng[] {STYLE_KEY});
final Map<Level, String> levelStyles = new HashMap<>(DEFAULT_STYLES); final Map<String, String> levelStyles = new HashMap<>(DEFAULT_STYLES);
for (final Map.Entry<String, String> entry : styles.entrySet()) { for (final Map.Entry<String, String> entry : styles.entrySet()) {
final String key = entry.getKey().toUpperCase(Locale.ENGLISH); final String key = entry.getKey().toUpperCase(Locale.ENGLISH);
final String value = entry.getValue(); final String value = entry.getValue();
if (STYLE_KEY.equalsIgnoreCase(key)) { if (STYLE_KEY.equalsIgnoreCase(key)) {
final Map<Level, String> enumMap = STYLES.get(value.toUpperCase( Locale.ENGLISH)); final Map<String, String> enumMap = STYLES.get(value.toUpperCase (Locale.ENGLISH));
if (enumMap == null) { if (enumMap == null) {
LOGGER.error("Unknown level style: " + value + ". Use one of " + LOGGER.error("Unknown level style: " + value + ". Use one of " +
Arrays.toString(STYLES.keySet().toArray())); Arrays.toString(STYLES.keySet().toArray()));
} else { } else {
levelStyles.putAll(enumMap); levelStyles.putAll(enumMap);
} }
} else { } else {
final Level level = Level.toLevel(key, null); final Level level = Level.toLevel(key, null);
if (level == null) { if (level == null) {
LOGGER.error("Unknown level name: {}; use one of {}", key, A LOGGER.warn("Setting style for yet unknown level name {}", k
rrays.toString(Level.values())); ey);
levelStyles.put(key, value);
} else { } else {
levelStyles.put(level, value); levelStyles.put(level.name(), value);
} }
} }
} }
return levelStyles; return levelStyles;
} }
/** /**
* Gets an instance of the class. * Gets an instance of the class.
* *
* @param config The current Configuration. * @param config The current Configuration.
skipping to change at line 202 skipping to change at line 203
return null; return null;
} }
final PatternParser parser = PatternLayout.createPatternParser(config); final PatternParser parser = PatternLayout.createPatternParser(config);
final List<PatternFormatter> formatters = parser.parse(options[0]); final List<PatternFormatter> formatters = parser.parse(options[0]);
final boolean disableAnsi = Arrays.toString(options).contains(PatternPar ser.DISABLE_ANSI + "=true"); final boolean disableAnsi = Arrays.toString(options).contains(PatternPar ser.DISABLE_ANSI + "=true");
final boolean noConsoleNoAnsi = Arrays.toString(options).contains(Patter nParser.NO_CONSOLE_NO_ANSI + "=true"); final boolean noConsoleNoAnsi = Arrays.toString(options).contains(Patter nParser.NO_CONSOLE_NO_ANSI + "=true");
final boolean hideAnsi = disableAnsi || (noConsoleNoAnsi && System.conso le() == null); final boolean hideAnsi = disableAnsi || (noConsoleNoAnsi && System.conso le() == null);
return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi); return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi);
} }
private final Map<Level, String> levelStyles; private final Map<String, String> levelStyles;
private final List<PatternFormatter> patternFormatters; private final List<PatternFormatter> patternFormatters;
private final boolean noAnsi; private final boolean noAnsi;
private final String defaultStyle; private final String defaultStyle;
/** /**
* Construct the converter. * Construct the converter.
* *
* @param patternFormatters * @param patternFormatters
* The PatternFormatters to generate the text to manipulate. * The PatternFormatters to generate the text to manipulate.
* @param noAnsi * @param noAnsi
* If true, do not output ANSI escape codes. * If true, do not output ANSI escape codes.
*/ */
private HighlightConverter(final List<PatternFormatter> patternFormatters, f inal Map<Level, String> levelStyles, final boolean noAnsi) { private HighlightConverter(final List<PatternFormatter> patternFormatters, f inal Map<String, String> levelStyles, final boolean noAnsi) {
super("style", "style"); super("style", "style");
this.patternFormatters = patternFormatters; this.patternFormatters = patternFormatters;
this.levelStyles = levelStyles; this.levelStyles = levelStyles;
this.defaultStyle = AnsiEscape.getDefaultStyle(); this.defaultStyle = AnsiEscape.getDefaultStyle();
this.noAnsi = noAnsi; this.noAnsi = noAnsi;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void format(final LogEvent event, final StringBuilder toAppendTo) { public void format(final LogEvent event, final StringBuilder toAppendTo) {
int start = 0; int start = 0;
int end = 0; int end = 0;
final String levelStyle = levelStyles.get(event.getLevel().name());
if (!noAnsi) { // use ANSI: set prefix if (!noAnsi) { // use ANSI: set prefix
start = toAppendTo.length(); start = toAppendTo.length();
toAppendTo.append(levelStyles.get(event.getLevel())); if (levelStyle != null) {
toAppendTo.append(levelStyle);
}
end = toAppendTo.length(); end = toAppendTo.length();
} }
//noinspection ForLoopReplaceableByForEach // noinspection ForLoopReplaceableByForEach
for (int i = 0, size = patternFormatters.size(); i < size; i++) { for (int i = 0, size = patternFormatters.size(); i < size; i++) {
patternFormatters.get(i).format(event, toAppendTo); patternFormatters.get(i).format(event, toAppendTo);
} }
// if we use ANSI we need to add the postfix or erase the unnecessary pr efix // if we use ANSI we need to add the postfix or erase the unnecessary pr efix
final boolean empty = toAppendTo.length() == end; final boolean empty = toAppendTo.length() == end;
if (!noAnsi) { if (!noAnsi) {
if (empty) { if (empty) {
toAppendTo.setLength(start); // erase prefix toAppendTo.setLength(start); // erase prefix
} else { } else if (levelStyle != null) {
toAppendTo.append(defaultStyle); // add postfix toAppendTo.append(defaultStyle); // add postfix
} }
} }
} }
String getLevelStyle(Level level) { String getLevelStyle(final Level level) {
return levelStyles.get(level); return levelStyles.get(level.name());
} }
@Override @Override
public boolean handlesThrowable() { public boolean handlesThrowable() {
for (final PatternFormatter formatter : patternFormatters) { for (final PatternFormatter formatter : patternFormatters) {
if (formatter .handlesThrowable()) { if (formatter .handlesThrowable()) {
return true; return true;
} }
} }
return false; return false;
 End of changes. 17 change blocks. 
35 lines changed or deleted 44 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)