Search in sources :

Example 1 with PatternFormatter

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

the class ColorConverter method format.

@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
    StringBuilder buf = new StringBuilder();
    for (PatternFormatter formatter : this.formatters) {
        formatter.format(event, buf);
    }
    if (buf.length() > 0) {
        AnsiElement element = this.styling;
        if (element == null) {
            // Assume highlighting
            element = LEVELS.get(event.getLevel().intLevel());
            element = (element == null ? AnsiColor.GREEN : element);
        }
        appendAnsiString(toAppendTo, buf.toString(), element);
    }
}
Also used : PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) AnsiElement(org.springframework.boot.ansi.AnsiElement)

Example 2 with PatternFormatter

use of org.apache.logging.log4j.core.pattern.PatternFormatter 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 3 with PatternFormatter

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

the class Rfc5424Layout method appendMessage.

private void appendMessage(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    // This layout formats StructuredDataMessages instead of delegating to the Message itself.
    final String text = (message instanceof StructuredDataMessage) ? message.getFormat() : message.getFormattedMessage();
    if (text != null && text.length() > 0) {
        buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
    }
    if (exceptionFormatters != null && event.getThrown() != null) {
        final StringBuilder exception = new StringBuilder(LF);
        for (final PatternFormatter formatter : exceptionFormatters) {
            formatter.format(event, exception);
        }
        buffer.append(escapeNewlines(exception.toString(), escapeNewLine));
    }
    if (includeNewLine) {
        buffer.append(LF);
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Example 4 with PatternFormatter

use of org.apache.logging.log4j.core.pattern.PatternFormatter 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 5 with PatternFormatter

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

the class ScriptPatternSelector method getFormatters.

@Override
public PatternFormatter[] getFormatters(final LogEvent event) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    bindings.put("logEvent", event);
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    if (object == null) {
        return defaultFormatters;
    }
    final PatternFormatter[] patternFormatter = formatterMap.get(object.toString());
    return patternFormatter == null ? defaultFormatters : patternFormatter;
}
Also used : SimpleBindings(javax.script.SimpleBindings) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter)

Aggregations

PatternFormatter (org.apache.logging.log4j.core.pattern.PatternFormatter)6 PatternParser (org.apache.logging.log4j.core.pattern.PatternParser)3 AnsiElement (org.springframework.boot.ansi.AnsiElement)2 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 SimpleBindings (javax.script.SimpleBindings)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)1 Message (org.apache.logging.log4j.message.Message)1 StructuredDataId (org.apache.logging.log4j.message.StructuredDataId)1 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)1