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 * VariablesNotEmpty pattern converter. 028 */ 029@Plugin(name = "notEmpty", category = PatternConverter.CATEGORY) 030@ConverterKeys({ "notEmpty", "varsNotEmpty", "variablesNotEmpty", }) 031public final class VariablesNotEmptyReplacementConverter extends LogEventPatternConverter { 032 033 private final List<PatternFormatter> formatters; 034 035 /** 036 * Construct the converter. 037 * 038 * @param formatters 039 * The PatternFormatters to generate the text to manipulate. 040 */ 041 private VariablesNotEmptyReplacementConverter(final List<PatternFormatter> formatters) { 042 super("notEmpty", "notEmpty"); 043 this.formatters = formatters; 044 } 045 046 /** 047 * Gets an instance of the class. 048 * 049 * @param config 050 * The current Configuration. 051 * @param options 052 * pattern options, may be null. 053 * @return instance of class. 054 */ 055 public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config, 056 final String[] options) { 057 if (options.length != 1) { 058 LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length); 059 return null; 060 } 061 if (options[0] == null) { 062 LOGGER.error("No pattern supplied on varsNotEmpty"); 063 return null; 064 } 065 final PatternParser parser = PatternLayout.createPatternParser(config); 066 final List<PatternFormatter> formatters = parser.parse(options[0]); 067 return new VariablesNotEmptyReplacementConverter(formatters); 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 @Override 074 public void format(final LogEvent event, final StringBuilder toAppendTo) { 075 final StringBuilder buf = new StringBuilder(); 076 int emptyVars = 0; 077 int vars = 0; 078 for (final PatternFormatter formatter : formatters) { 079 int start = buf.length(); 080 formatter.format(event, buf); 081 final boolean isVariable = formatter.getConverter().isVariable(); 082 vars += isVariable ? 1 : 0; 083 if (isVariable && buf.length() - start == 0) { 084 emptyVars++; 085 } 086 } 087 if (vars > 0 && emptyVars != vars) { 088 toAppendTo.append(buf.toString()); 089 } 090 } 091}