001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.pattern; 018 019import java.util.List; 020 021import org.apache.logging.log4j.core.LogEvent; 022import org.apache.logging.log4j.core.config.Configuration; 023import org.apache.logging.log4j.core.config.plugins.Plugin; 024import org.apache.logging.log4j.core.layout.PatternLayout; 025 026/** 027 * Equals pattern converter. 028 */ 029@Plugin(name = "equals", category = PatternConverter.CATEGORY) 030@ConverterKeys({ "equals" }) 031public final class EqualsReplacementConverter extends LogEventPatternConverter { 032 033 /** 034 * Gets an instance of the class. 035 * 036 * @param config The current Configuration. 037 * @param options pattern options, an array of three elements: pattern, testString, and substitution. 038 * @return instance of class. 039 */ 040 public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) { 041 if (options.length != 3) { 042 LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length); 043 return null; 044 } 045 if (options[0] == null) { 046 LOGGER.error("No pattern supplied on equals"); 047 return null; 048 } 049 if (options[1] == null) { 050 LOGGER.error("No test string supplied on equals"); 051 return null; 052 } 053 if (options[2] == null) { 054 LOGGER.error("No substitution supplied on equals"); 055 return null; 056 } 057 final String p = options[1]; 058 final PatternParser parser = PatternLayout.createPatternParser(config); 059 final List<PatternFormatter> formatters = parser.parse(options[0]); 060 return new EqualsReplacementConverter(formatters, p, options[2], parser); 061 } 062 063 private final List<PatternFormatter> formatters; 064 065 private final String substitution; 066 067 private final String testString; 068 069 private final PatternParser parser; 070 071 /** 072 * Construct the converter. 073 * 074 * @param formatters The PatternFormatters to generate the text to manipulate. 075 * @param testString The test string. 076 * @param substitution The substitution string. 077 * @param parser The PatternParser. 078 */ 079 private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString, 080 final String substitution, PatternParser parser) { 081 super("equals", "equals"); 082 this.testString = testString; 083 this.substitution = substitution; 084 this.formatters = formatters; 085 this.parser = parser; 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 @Override 092 public void format(final LogEvent event, final StringBuilder toAppendTo) { 093 final StringBuilder buf = new StringBuilder(); 094 for (final PatternFormatter formatter : formatters) { 095 formatter.format(event, buf); 096 } 097 final String string = buf.toString(); 098 toAppendTo.append(testString.equals(string) ? parseSubstitution(event) : string); 099 } 100 101 /** 102 * Returns the parsed substitution text. 103 * 104 * @param event the current log event 105 * @return the parsed substitution text 106 */ 107 String parseSubstitution(final LogEvent event) { 108 final StringBuilder substitutionBuffer = new StringBuilder(); 109 // check if substitution needs to be parsed 110 if (substitution.contains("%")) { 111 // parse substitution pattern 112 final List<PatternFormatter> substitutionFormatters = parser.parse(substitution); 113 for (final PatternFormatter formatter : substitutionFormatters) { 114 formatter.format(event, substitutionBuffer); 115 } 116 } else { 117 substitutionBuffer.append(substitution); 118 } 119 return substitutionBuffer.toString(); 120 } 121}