org.apache.logging.log4j.core.pattern.PatternParser Java Examples

The following examples show how to use org.apache.logging.log4j.core.pattern.PatternParser. 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: PatternProcessor.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 * @param pattern The file pattern.
 */
public PatternProcessor(final String pattern) {
    this.pattern = pattern;
    final PatternParser parser = createPatternParser();
    // FIXME: this seems to expect List<ArrayPatternConverter> in practice; types need to be fixed around this
    final List<PatternConverter> converters = new ArrayList<>();
    final List<FormattingInfo> fields = new ArrayList<>();
    parser.parse(pattern, converters, fields, false, false, false);
    final FormattingInfo[] infoArray = new FormattingInfo[fields.size()];
    patternFields = fields.toArray(infoArray);
    final ArrayPatternConverter[] converterArray = new ArrayPatternConverter[converters.size()];
    patternConverters = converters.toArray(converterArray);
    this.fileExtension = FileExtension.lookupForFile(pattern);

    for (final ArrayPatternConverter converter : patternConverters) {
        // TODO: extract common interface
        if (converter instanceof DatePatternConverter) {
            final DatePatternConverter dateConverter = (DatePatternConverter) converter;
            frequency = calculateFrequency(dateConverter.getPattern());
        } else if (converter instanceof FileDatePatternConverter) {
            frequency = calculateFrequency(((FileDatePatternConverter) converter).getPattern());
        }
    }
}
 
Example #3
Source File: PatternLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public Serializer build() {
    if (Strings.isEmpty(pattern) && Strings.isEmpty(defaultPattern)) {
        return null;
    }
    if (patternSelector == null) {
        try {
            final PatternParser parser = createPatternParser(configuration);
            final List<PatternFormatter> list = parser.parse(pattern == null ? defaultPattern : pattern,
                    alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi);
            final PatternFormatter[] formatters = list.toArray(new PatternFormatter[0]);
            return new PatternSerializer(formatters, replace);
        } catch (final RuntimeException ex) {
            throw new IllegalArgumentException("Cannot parse pattern '" + pattern + "'", ex);
        }
    }
    return new PatternSelectorSerializer(patternSelector, replace);
}
 
Example #4
Source File: Rfc5424Layout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private Map<String, FieldFormatter> createFieldFormatters(final LoggerFields[] loggerFields,
        final Configuration config) {
    final Map<String, FieldFormatter> sdIdMap = new HashMap<>(loggerFields == null ? 0 : loggerFields.length);
    if (loggerFields != null) {
        for (final LoggerFields loggerField : loggerFields) {
            final StructuredDataId key = loggerField.getSdId() == null ? mdcSdId : loggerField.getSdId();
            final Map<String, List<PatternFormatter>> sdParams = new HashMap<>();
            final Map<String, String> fields = loggerField.getMap();
            if (!fields.isEmpty()) {
                final PatternParser fieldParser = createPatternParser(config, null);

                for (final Map.Entry<String, String> entry : fields.entrySet()) {
                    final List<PatternFormatter> formatters = fieldParser.parse(entry.getValue());
                    sdParams.put(entry.getKey(), formatters);
                }
                final FieldFormatter fieldFormatter = new FieldFormatter(sdParams,
                        loggerField.getDiscardIfAllFieldsAreEmpty());
                sdIdMap.put(key.toString(), fieldFormatter);
            }
        }
    }
    return sdIdMap.size() > 0 ? sdIdMap : null;
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: ThreadLocalVsPoolBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private static List<PatternFormatter> createFormatters() {
    final Configuration config = new DefaultConfiguration();
    final PatternParser parser = new PatternParser(config, "Converter", LogEventPatternConverter.class);
    return parser.parse("%d %5p [%t] %c{1} %X{transactionId} - %m%n", false, true);
}
 
Example #10
Source File: PatternProcessor.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
private PatternParser createPatternParser() {

        return new PatternParser(null, KEY, null);
    }