Search in sources :

Example 1 with PatternParser

use of org.apache.logging.log4j.core.pattern.PatternParser in project logging-log4j2 by apache.

the class StringBuilderPool method createFormatters.

/**
     */
private static PatternFormatter[] createFormatters() {
    final Configuration config = new DefaultConfiguration();
    final PatternParser parser = new PatternParser(config, "Converter", LogEventPatternConverter.class);
    final List<PatternFormatter> result = parser.parse(LOG4JPATTERN, false, true);
    return result.toArray(new PatternFormatter[result.size()]);
}
Also used : PatternParser(org.apache.logging.log4j.core.pattern.PatternParser) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Example 2 with PatternParser

use of org.apache.logging.log4j.core.pattern.PatternParser in project logging-log4j2 by apache.

the class PatternLayout method createPatternParser.

/**
     * Creates a PatternParser.
     * @param config The Configuration.
     * @return The PatternParser.
     */
public static PatternParser createPatternParser(final Configuration config) {
    if (config == null) {
        return new PatternParser(config, KEY, LogEventPatternConverter.class);
    }
    PatternParser parser = config.getComponent(KEY);
    if (parser == null) {
        parser = new PatternParser(config, KEY, LogEventPatternConverter.class);
        config.addComponent(KEY, parser);
        parser = config.getComponent(KEY);
    }
    return parser;
}
Also used : LogEventPatternConverter(org.apache.logging.log4j.core.pattern.LogEventPatternConverter) PatternParser(org.apache.logging.log4j.core.pattern.PatternParser)

Example 3 with PatternParser

use of org.apache.logging.log4j.core.pattern.PatternParser in project logging-log4j2 by apache.

the class Rfc5424Layout method createFieldFormatters.

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;
}
Also used : StructuredDataId(org.apache.logging.log4j.message.StructuredDataId) PatternParser(org.apache.logging.log4j.core.pattern.PatternParser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 4 with PatternParser

use of org.apache.logging.log4j.core.pattern.PatternParser in project logging-log4j2 by apache.

the class Rfc5424Layout method createPatternParser.

/**
     * Create a PatternParser.
     *
     * @param config The Configuration.
     * @param filterClass Filter the returned plugins after calling the plugin manager.
     * @return The PatternParser.
     */
private static PatternParser createPatternParser(final Configuration config, final Class<? extends PatternConverter> filterClass) {
    if (config == null) {
        return new PatternParser(config, PatternLayout.KEY, LogEventPatternConverter.class, filterClass);
    }
    PatternParser parser = config.getComponent(COMPONENT_KEY);
    if (parser == null) {
        parser = new PatternParser(config, PatternLayout.KEY, ThrowablePatternConverter.class);
        config.addComponent(COMPONENT_KEY, parser);
        parser = config.getComponent(COMPONENT_KEY);
    }
    return parser;
}
Also used : ThrowablePatternConverter(org.apache.logging.log4j.core.pattern.ThrowablePatternConverter) PatternParser(org.apache.logging.log4j.core.pattern.PatternParser)

Example 5 with PatternParser

use of org.apache.logging.log4j.core.pattern.PatternParser in project spring-boot by spring-projects.

the class ColorConverter method newInstance.

/**
	 * Creates a new instance of the class. Required by Log4J2.
	 * @param config the configuration
	 * @param options the options
	 * @return a new instance, or {@code null} if the options are invalid
	 */
public static ColorConverter newInstance(Configuration config, 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;
    }
    PatternParser parser = PatternLayout.createPatternParser(config);
    List<PatternFormatter> formatters = parser.parse(options[0]);
    AnsiElement element = (options.length == 1 ? null : ELEMENTS.get(options[1]));
    return new ColorConverter(formatters, element);
}
Also used : PatternParser(org.apache.logging.log4j.core.pattern.PatternParser) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) AnsiElement(org.springframework.boot.ansi.AnsiElement)

Aggregations

PatternParser (org.apache.logging.log4j.core.pattern.PatternParser)5 PatternFormatter (org.apache.logging.log4j.core.pattern.PatternFormatter)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)1 LogEventPatternConverter (org.apache.logging.log4j.core.pattern.LogEventPatternConverter)1 ThrowablePatternConverter (org.apache.logging.log4j.core.pattern.ThrowablePatternConverter)1 StructuredDataId (org.apache.logging.log4j.message.StructuredDataId)1 AnsiElement (org.springframework.boot.ansi.AnsiElement)1