下面列出了org.apache.logging.log4j.core.layout.PatternLayout#createPatternParser ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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));
}
/**
* 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);
}
/**
* 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);
}
/**
* 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]);
}
/**
* 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);
}
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);
}
/**
* 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));
}
}
}
/**
* 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);
}
/**
* @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);
}
}
/**
* 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);
}
/**
* 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]);
}