Java Code Examples for org.apache.logging.log4j.core.layout.PatternLayout#createPatternParser()

The following examples show how to use org.apache.logging.log4j.core.layout.PatternLayout#createPatternParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinecraftFormattingConverter.java    From TerminalConsoleAppender with MIT License 6 votes vote down vote up
/**
 * Gets a new instance of the {@link MinecraftFormattingConverter} with the
 * specified options.
 *
 * @param config The current configuration
 * @param options The pattern options
 * @return The new instance
 *
 * @see MinecraftFormattingConverter
 */
public static @Nullable MinecraftFormattingConverter newInstance(Configuration config, String[] options) {
    if (options.length < 1 || options.length > 2) {
        LOGGER.error("Incorrect number of options on minecraftFormatting. Expected at least 1, max 2 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on minecraftFormatting");
        return null;
    }

    PatternParser parser = PatternLayout.createPatternParser(config);
    List<PatternFormatter> formatters = parser.parse(options[0]);
    boolean strip = options.length > 1 && "strip".equals(options[1]);
    return new MinecraftFormattingConverter(formatters, strip);
}
 
Example 2
Source File: EncodingPatternConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an EncodingPatternConverter using a pattern string and an optional escape format.
 *
 * @param config  the current Configuration
 * @param options first option is the nested pattern format; second option is the escape format (optional)
 * @return instance of pattern converter.
 */
public static EncodingPatternConverter newInstance(final Configuration config, final String[] options) {
    if (options.length > 2 || options.length == 0) {
        LOGGER.error("Incorrect number of options on escape. Expected 1 or 2, but received {}",
            options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on escape");
        return null;
    }
    final EscapeFormat escapeFormat = options.length < 2 ? EscapeFormat.HTML
        : EnglishEnums.valueOf(EscapeFormat.class, options[1], EscapeFormat.HTML);
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new EncodingPatternConverter(formatters, escapeFormat);
}
 
Example 3
Source File: StyleConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config
 *            The current Configuration.
 * @param options
 *            pattern options, may be null. If first element is "short", only the first line of the throwable will
 *            be formatted.
 * @return instance of class.
 */
public static StyleConverter newInstance(final Configuration config, final String[] options) {
    if (options == null) {
        return null;
    }
    if (options.length < 2) {
        LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied for style converter");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No style attributes supplied for style converter");
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    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");
    final boolean hideAnsi = disableAnsi || (noConsoleNoAnsi && System.console() == null);
    return new StyleConverter(formatters, style, hideAnsi);
}
 
Example 4
Source File: MaxLengthConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config  The current Configuration.
 * @param options pattern options, an array of two elements: pattern, max length (defaults to 100 on invalid value).
 * @return instance of class.
 */
public static MaxLengthConverter newInstance(final Configuration config, final String[] options) {
    if (options.length != 2) {
        LOGGER.error("Incorrect number of options on maxLength: expected 2 received {}: {}", options.length,
            options);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on maxLength");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No length supplied on maxLength");
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new MaxLengthConverter(formatters, AbstractAppender.parseInt(options[1], 100));
}
 
Example 5
Source File: HighlightConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config The current Configuration.
 * @param options pattern options, may be null. If first element is "short", only the first line of the
 *                throwable will be formatted.
 * @return instance of class.
 */
public static HighlightConverter newInstance(final Configuration config, final String[] options) {
    if (options.length < 1) {
        LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on style");
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    final boolean disableAnsi = Arrays.toString(options).contains(PatternParser.DISABLE_ANSI + "=true");
    final boolean noConsoleNoAnsi = Arrays.toString(options).contains(PatternParser.NO_CONSOLE_NO_ANSI + "=true");
    final boolean hideAnsi = disableAnsi || (noConsoleNoAnsi && System.console() == null);
    return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi);
}
 
Example 6
Source File: EqualsIgnoreCaseReplacementConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config
 *            The current Configuration.
 * @param options
 *            pattern options, an array of three elements: pattern, testString, and substitution.
 * @return instance of class.
 */
public static EqualsIgnoreCaseReplacementConverter newInstance(final Configuration config, final String[] options) {
    if (options.length != 3) {
        LOGGER.error("Incorrect number of options on equalsIgnoreCase. Expected 3 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on equalsIgnoreCase");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No test string supplied on equalsIgnoreCase");
        return null;
    }
    if (options[2] == null) {
        LOGGER.error("No substitution supplied on equalsIgnoreCase");
        return null;
    }
    final String p = options[1];
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2], parser);
}
 
Example 7
Source File: RegexReplacementConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config The current Configuration.
 * @param options pattern options, may be null.  If first element is "short",
 *                only the first line of the throwable will be formatted.
 * @return instance of class.
 */
public static RegexReplacementConverter newInstance(final Configuration config, final String[] options) {
    if (options.length != 3) {
        LOGGER.error("Incorrect number of options on replace. Expected 3 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on replace");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No regular expression supplied on replace");
        return null;
    }
    if (options[2] == null) {
        LOGGER.error("No substitution supplied on replace");
        return null;
    }
    final Pattern p = Pattern.compile(options[1]);
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new RegexReplacementConverter(formatters, p, options[2]);
}
 
Example 8
Source File: EqualsReplacementConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config  The current Configuration.
 * @param options pattern options, an array of three elements: pattern, testString, and substitution.
 * @return instance of class.
 */
public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) {
    if (options.length != 3) {
        LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on equals");
        return null;
    }
    if (options[1] == null) {
        LOGGER.error("No test string supplied on equals");
        return null;
    }
    if (options[2] == null) {
        LOGGER.error("No substitution supplied on equals");
        return null;
    }
    final String p = options[1];
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new EqualsReplacementConverter(formatters, p, options[2], parser);
}
 
Example 9
Source File: ConsolePatternSelector.java    From teku with Apache License 2.0 5 votes vote down vote up
public ConsolePatternSelector(
    final AbstractConfiguration configuration, final boolean omitStackTraces) {
  this.omitStackTraces = omitStackTraces;

  final PatternParser patternParser = PatternLayout.createPatternParser(configuration);
  omitStackTraceFormat =
      patternParser.parse(CONSOLE_EXCEPTION_FORMAT, false, true).toArray(PatternFormatter[]::new);
  defaultFormat =
      patternParser.parse(CONSOLE_FORMAT, true, true).toArray(PatternFormatter[]::new);
}
 
Example 10
Source File: LoggerNamePatternSelector.java    From TerminalConsoleAppender with MIT License 5 votes vote down vote up
/**
 * Constructs a new {@link LoggerNamePatternSelector}.
 *
 * @param defaultPattern The default pattern to use if no logger name matches
 * @param properties The pattern match rules to use
 * @param alwaysWriteExceptions Write exceptions even if pattern does not
 *     include exception conversion
 * @param disableAnsi If true, disable all ANSI escape codes
 * @param noConsoleNoAnsi If true and {@link System#console()} is null,
 *     disable ANSI escape codes
 * @param config The configuration
 */
protected LoggerNamePatternSelector(String defaultPattern, PatternMatch[] properties,
        boolean alwaysWriteExceptions, boolean disableAnsi, boolean noConsoleNoAnsi, Configuration config) {
    PatternParser parser = PatternLayout.createPatternParser(config);
    PatternFormatter[] emptyFormatters = new PatternFormatter[0];
    this.defaultFormatters = parser.parse(defaultPattern, alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi)
            .toArray(emptyFormatters);
    for (PatternMatch property : properties) {
        PatternFormatter[] formatters = parser.parse(property.getPattern(), alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi)
                .toArray(emptyFormatters);
        for (String name : property.getKey().split(",")) {
            this.formatters.add(new LoggerNameSelector(name, formatters));
        }
    }
}
 
Example 11
Source File: HighlightErrorConverter.java    From TerminalConsoleAppender with MIT License 5 votes vote down vote up
/**
 * Gets a new instance of the {@link HighlightErrorConverter} with the
 * specified options.
 *
 * @param config The current configuration
 * @param options The pattern options
 * @return The new instance
 */
public static @Nullable HighlightErrorConverter newInstance(Configuration config, String[] options) {
    if (options.length != 1) {
        LOGGER.error("Incorrect number of options on highlightError. Expected 1 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on highlightError");
        return null;
    }

    PatternParser parser = PatternLayout.createPatternParser(config);
    List<PatternFormatter> formatters = parser.parse(options[0]);
    return new HighlightErrorConverter(formatters);
}
 
Example 12
Source File: IbisPatternLayout.java    From iaf with Apache License 2.0 5 votes vote down vote up
/**
 * @param pattern the pattern to use or DEFAULT when null
 * @param alwaysWriteExceptions defaults to true
 * @param disableAnsi defaults to false
 * @param noConsoleNoAnsi defaults to false
 */
IbisPatternLayout(final Configuration config, final String pattern, final Charset charset, final boolean alwaysWriteExceptions, final boolean disableAnsi, final boolean noConsoleNoAnsi) {
	super(config, charset);

	try {
		final PatternParser parser = PatternLayout.createPatternParser(configuration);
		final List<PatternFormatter> list = parser.parse(pattern, alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi);
		final PatternFormatter[] formatters = list.toArray(new PatternFormatter[0]);
		serializer = new PatternSerializer(formatters);
	} catch (final RuntimeException ex) {
		throw new IllegalArgumentException("Cannot parse pattern '" + pattern + "'", ex);
	}
}
 
Example 13
Source File: VariablesNotEmptyReplacementConverter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an instance of the class.
 *
 * @param config
 *            The current Configuration.
 * @param options
 *            pattern options, may be null.
 * @return instance of class.
 */
public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config,
        final String[] options) {
    if (options.length != 1) {
        LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length);
        return null;
    }
    if (options[0] == null) {
        LOGGER.error("No pattern supplied on varsNotEmpty");
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    final List<PatternFormatter> formatters = parser.parse(options[0]);
    return new VariablesNotEmptyReplacementConverter(formatters);
}
 
Example 14
Source File: AbstractStyleNameConverter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a list of PatternFormatter from the given configuration and options or null if no pattern is supplied.
 *
 * @param config A configuration.
 * @param options pattern options.
 * @return a list of PatternFormatter from the given configuration and options or null if no pattern is supplied.
 */
private static List<PatternFormatter> toPatternFormatterList(final Configuration config, final String[] options) {
    if (options.length == 0 || options[0] == null) {
        LOGGER.error("No pattern supplied on style for config=" + config);
        return null;
    }
    final PatternParser parser = PatternLayout.createPatternParser(config);
    if (parser == null) {
        LOGGER.error("No PatternParser created for config=" + config + ", options=" + Arrays.toString(options));
        return null;
    }
    return parser.parse(options[0]);
}