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 /** 034 * Gets an instance of the class. 035 * 036 * @param config 037 * The current Configuration. 038 * @param options 039 * pattern options, may be null. 040 * @return instance of class. 041 */ 042 public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config, 043 final String[] options) { 044 if (options.length != 1) { 045 LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length); 046 return null; 047 } 048 if (options[0] == null) { 049 LOGGER.error("No pattern supplied on varsNotEmpty"); 050 return null; 051 } 052 final PatternParser parser = PatternLayout.createPatternParser(config); 053 final List<PatternFormatter> formatters = parser.parse(options[0]); 054 return new VariablesNotEmptyReplacementConverter(formatters); 055 } 056 057 private final List<PatternFormatter> formatters; 058 059 /** 060 * Construct the converter. 061 * 062 * @param formatters 063 * The PatternFormatters to generate the text to manipulate. 064 */ 065 private VariablesNotEmptyReplacementConverter(final List<PatternFormatter> formatters) { 066 super("notEmpty", "notEmpty"); 067 this.formatters = 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}