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 037 * The current Configuration. 038 * @param options 039 * pattern options, an array of three elements: pattern, testString, and substitution. 040 * @return instance of class. 041 */ 042 public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) { 043 if (options.length != 3) { 044 LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length); 045 return null; 046 } 047 if (options[0] == null) { 048 LOGGER.error("No pattern supplied on equals"); 049 return null; 050 } 051 if (options[1] == null) { 052 LOGGER.error("No test string supplied on equals"); 053 return null; 054 } 055 if (options[2] == null) { 056 LOGGER.error("No substitution supplied on equals"); 057 return null; 058 } 059 final String p = options[1]; 060 final PatternParser parser = PatternLayout.createPatternParser(config); 061 final List<PatternFormatter> formatters = parser.parse(options[0]); 062 return new EqualsReplacementConverter(formatters, p, options[2]); 063 } 064 065 private final List<PatternFormatter> formatters; 066 067 private final String substitution; 068 069 private final String testString; 070 071 /** 072 * Construct the converter. 073 * 074 * @param formatters 075 * The PatternFormatters to generate the text to manipulate. 076 * @param testString 077 * The test string. 078 * @param substitution 079 * The substitution string. 080 */ 081 private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString, 082 final String substitution) { 083 super("equals", "equals"); 084 this.testString = testString; 085 this.substitution = substitution; 086 this.formatters = formatters; 087 } 088 089 /** 090 * {@inheritDoc} 091 */ 092 @Override 093 public void format(final LogEvent event, final StringBuilder toAppendTo) { 094 final StringBuilder buf = new StringBuilder(); 095 for (final PatternFormatter formatter : formatters) { 096 formatter.format(event, buf); 097 } 098 final String string = buf.toString(); 099 toAppendTo.append(testString.equals(string) ? substitution : string); 100 } 101}