/** * 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); }
@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); } }
private Map<String, FieldFormatter> createFieldFormatters(final LoggerFields[] loggerFields, final Configuration config) { final Map<String, FieldFormatter> sdIdMap = new HashMap<String, FieldFormatter>(); if (loggerFields != null) { for (final LoggerFields lField : loggerFields) { final StructuredDataId key = lField.getSdId() == null ? mdcSDID : lField.getSdId(); final Map<String, List<PatternFormatter>> sdParams = new HashMap<String, List<PatternFormatter>>(); final Map<String, String> fields = lField.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(), false); sdParams.put(entry.getKey(), formatters); } final FieldFormatter fieldFormatter = new FieldFormatter(sdParams, lField.getDiscardIfAllFieldsAreEmpty()); sdIdMap.put(key.toString(), fieldFormatter); } } } return sdIdMap.size() > 0 ? sdIdMap : null; }
private void appendMessage(final StringBuilder buffer, final LogEvent event) { final Message message = event.getMessage(); final String text = message.getFormat(); 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); } }
@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); }
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; }
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 instanceof MessageCollectionMessage) ? 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); } }
@Override public String toSerializable(LogEvent event) { PatternSelector selector = getSelector(event); final StringBuilder buf = getStringBuilder(); PatternFormatter[] formatters = selector.getFormatters(event); final int len = formatters.length; for (int i = 0; i < len; i++) { formatters[i].format(event, buf); } String str = buf.toString(); return str; }
@Override public boolean handlesThrowable() { for (PatternFormatter formatter : this.formatters) { if (formatter.handlesThrowable()) { return true; } } return super.handlesThrowable(); }
/** * Obtains an instance of pattern converter. * * @param config The Configuration. * @param options options, may be null. * @return instance of pattern converter. */ public static SimpleJSONPatternConverter newInstance(final Configuration config, final String[] options) { if (options.length != 1) { LOGGER.error("Incorrect number of options on json. Expected 1, received " + options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on json"); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new SimpleJSONPatternConverter(formatters); }
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); } EncoderJSON.escapeJSON(buf, toAppendTo); }
public StructuredDataElement format(final LogEvent event) { final Map<String, String> map = new HashMap<String, String>(); for (final Map.Entry<String, List<PatternFormatter>> entry : delegateMap.entrySet()) { final StringBuilder buffer = new StringBuilder(); for (final PatternFormatter formatter : entry.getValue()) { formatter.format(event, buffer); } map.put(entry.getKey(), buffer.toString()); } return new StructuredDataElement(map, discardIfEmpty); }
/** * Formats a logging event to a writer. * * * @param event logging event to be formatted. * @return The event formatted as a String. */ @Override public String toSerializable(final LogEvent event) { final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); } String str = buf.toString(); if (replace != null) { str = replace.format(str); } return str; }
/** */ 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()]); }
@Override public PatternFormatter[] getFormatters(final LogEvent event) { final Marker marker = event.getMarker(); if (marker == null) { return defaultFormatters; } for (final String key : formatterMap.keySet()) { if (marker.isInstanceOf(key)) { return formatterMap.get(key); } } return defaultFormatters; }
@Override public StringBuilder toSerializable(final LogEvent event, final StringBuilder buffer) { final PatternFormatter[] formatters = patternSelector.getFormatters(event); final int len = formatters.length; for (int i = 0; i < len; i++) { formatters[i].format(event, buffer); } if (replace != null) { // creates temporary objects String str = buffer.toString(); str = replace.format(str); buffer.setLength(0); buffer.append(str); } return buffer; }
@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; }
public StructuredDataElement format(final LogEvent event) { final Map<String, String> map = new HashMap<>(delegateMap.size()); for (final Map.Entry<String, List<PatternFormatter>> entry : delegateMap.entrySet()) { final StringBuilder buffer = new StringBuilder(); for (final PatternFormatter formatter : entry.getValue()) { formatter.format(event, buffer); } map.put(entry.getKey(), buffer.toString()); } return new StructuredDataElement(map, eventPrefix, discardIfEmpty); }
private ColorConverter(List<PatternFormatter> formatters, AnsiElement styling) { super("style", "style"); this.formatters = formatters; this.styling = styling; }
public FieldFormatter(final Map<String, List<PatternFormatter>> fieldMap, final boolean discardIfEmpty) { this.discardIfEmpty = discardIfEmpty; this.delegateMap = fieldMap; }
PatternSerializer2() { this(new PatternFormatter[]{ new PatternFormatter(NoGcMessagePatternConverter.newInstance(null, null), FormattingInfo.getDefault()), }); }
private PatternSerializer2(final PatternFormatter[] formatters) { super(); this.formatters = formatters; }
private PatternSerializer(final PatternFormatter[] formatters, final RegexReplacement replace) { super(); this.formatters = formatters; this.replace = replace; }
public int getGroupCount() { int groupCount = 0; if ((logPatternString != null) && (!"".equals(logPatternString))) { PatternParser patternParser = new PatternParser("Converter"); List<PatternFormatter> patternFormatterList = patternParser.parse(logPatternString); groupCount = patternFormatterList.size(); } return groupCount; }
/** * Private constructor. * * @param formatters The PatternFormatters to generate the text to manipulate. */ private SimpleJSONPatternConverter(final List<PatternFormatter> formatters) { super("json", "json"); this.formatters = formatters; }
PatternFormatter[] getFormatters(LogEvent event);