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 */ 017 018package org.apache.logging.log4j.core.pattern; 019 020import java.util.List; 021 022import org.apache.logging.log4j.core.LogEvent; 023import org.apache.logging.log4j.core.appender.AbstractAppender; 024import org.apache.logging.log4j.core.config.Configuration; 025import org.apache.logging.log4j.core.config.plugins.Plugin; 026import org.apache.logging.log4j.core.layout.PatternLayout; 027import org.apache.logging.log4j.util.PerformanceSensitive; 028 029/** 030 * Max length pattern converter. Limit contained text to a maximum length. 031 * On invalid length the default value 100 is used (and an error message is logged). 032 * If max length is greater than 20, an abbreviated text will get ellipsis ("...") appended. 033 * Example usage (for email subject): 034 * {@code "%maxLen{[AppName, ${hostName}, ${web:contextPath}] %p: %c{1} - %m%notEmpty{ =>%ex{short}}}{160}"} 035 * 036 * @author Thies Wellpott 037 */ 038@Plugin(name = "maxLength", category = PatternConverter.CATEGORY) 039@ConverterKeys({"maxLength", "maxLen"}) 040@PerformanceSensitive("allocation") 041public final class MaxLengthConverter extends LogEventPatternConverter { 042 043 /** 044 * Gets an instance of the class. 045 * 046 * @param config The current Configuration. 047 * @param options pattern options, an array of two elements: pattern, max length (defaults to 100 on invalid value). 048 * @return instance of class. 049 */ 050 public static MaxLengthConverter newInstance(final Configuration config, final String[] options) { 051 if (options.length != 2) { 052 LOGGER.error("Incorrect number of options on maxLength: expected 2 received {}: {}", options.length, 053 options); 054 return null; 055 } 056 if (options[0] == null) { 057 LOGGER.error("No pattern supplied on maxLength"); 058 return null; 059 } 060 if (options[1] == null) { 061 LOGGER.error("No length supplied on maxLength"); 062 return null; 063 } 064 final PatternParser parser = PatternLayout.createPatternParser(config); 065 final List<PatternFormatter> formatters = parser.parse(options[0]); 066 return new MaxLengthConverter(formatters, AbstractAppender.parseInt(options[1], 100)); 067 } 068 069 070 private final List<PatternFormatter> formatters; 071 private final int maxLength; 072 073 /** 074 * Construct the converter. 075 * 076 * @param formatters The PatternFormatters to generate the text to manipulate. 077 * @param maxLength The max. length of the resulting string. Ellipsis ("...") is appended on shorted string, if greater than 20. 078 */ 079 private MaxLengthConverter(final List<PatternFormatter> formatters, final int maxLength) { 080 super("MaxLength", "maxLength"); 081 this.maxLength = maxLength; 082 this.formatters = formatters; 083 LOGGER.trace("new MaxLengthConverter with {}", maxLength); 084 } 085 086 087 @Override 088 public void format(final LogEvent event, final StringBuilder toAppendTo) { 089 final int initialLength = toAppendTo.length(); 090 for (int i = 0; i < formatters.size(); i++) { 091 final PatternFormatter formatter = formatters.get(i); 092 formatter.format(event, toAppendTo); 093 if (toAppendTo.length() > initialLength + maxLength) { // stop early 094 break; 095 } 096 } 097 if (toAppendTo.length() > initialLength + maxLength) { 098 toAppendTo.setLength(initialLength + maxLength); 099 if (maxLength > 20) { // only append ellipses if length is not very short 100 toAppendTo.append("..."); 101 } 102 } 103 } 104}