/** * 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); }
/** * Constructor. * @param pattern The file pattern. */ public PatternProcessor(final String pattern) { final PatternParser parser = createPatternParser(); final List<PatternConverter> converters = new ArrayList<PatternConverter>(); final List<FormattingInfo> fields = new ArrayList<FormattingInfo>(); parser.parse(pattern, converters, fields); final FormattingInfo[] infoArray = new FormattingInfo[fields.size()]; patternFields = fields.toArray(infoArray); final ArrayPatternConverter[] converterArray = new ArrayPatternConverter[converters.size()]; patternConverters = converters.toArray(converterArray); for (final ArrayPatternConverter converter : patternConverters) { if (converter instanceof DatePatternConverter) { final DatePatternConverter dateConverter = (DatePatternConverter) converter; frequency = calculateFrequency(dateConverter.getPattern()); } } }
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; }
/** * Constructor. * @param pattern The file pattern. */ public PatternProcessor(final String pattern) { this.pattern = pattern; final PatternParser parser = createPatternParser(); 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); for (final ArrayPatternConverter converter : patternConverters) { if (converter instanceof DatePatternConverter) { final DatePatternConverter dateConverter = (DatePatternConverter) converter; frequency = calculateFrequency(dateConverter.getPattern()); } } }
@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; }
/** * 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); }
/** * Constructs a EnhancedPatternLayout using the supplied conversion pattern. * * @param config The Configuration. * @param replace The regular expression to match. * @param pattern conversion pattern. * @param charset The character set. * @param alwaysWriteExceptions Whether or not exceptions should always be handled in this pattern (if {@code true}, * exceptions will be written even if the pattern does not specify so). */ private PatternLayout(final Configuration config, final RegexReplacement replace, final String pattern, final Charset charset, final boolean alwaysWriteExceptions) { super(charset); this.replace = replace; this.conversionPattern = pattern; this.config = config; this.alwaysWriteExceptions = alwaysWriteExceptions; final PatternParser parser = createPatternParser(config); formatters = parser.parse(pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern, this.alwaysWriteExceptions); }
/** * Set the <b>ConversionPattern</b> option. This is the string which * controls formatting and consists of a mix of literal content and * conversion specifiers. * * @param conversionPattern conversion pattern. */ public void setConversionPattern(final String conversionPattern) { final String pattern = OptionConverter.convertSpecialChars(conversionPattern); if (pattern == null) { return; } final PatternParser parser = createPatternParser(this.config); formatters = parser.parse(pattern, this.alwaysWriteExceptions); }
/** */ 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()]); }
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 PatternParser createPatternParser() { return new PatternParser(null, KEY, null); }