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 org.apache.logging.log4j.core.LogEvent; 020import org.apache.logging.log4j.core.impl.LocationAware; 021 022/** 023 * 024 */ 025public class PatternFormatter { 026 027 /** 028 * The empty array. 029 */ 030 public static final PatternFormatter[] EMPTY_ARRAY = {}; 031 032 private final LogEventPatternConverter converter; 033 private final FormattingInfo field; 034 private final boolean skipFormattingInfo; 035 036 public PatternFormatter(final LogEventPatternConverter converter, final FormattingInfo field) { 037 this.converter = converter; 038 this.field = field; 039 this.skipFormattingInfo = field == FormattingInfo.getDefault(); 040 } 041 042 public void format(final LogEvent event, final StringBuilder buf) { 043 if (skipFormattingInfo) { 044 converter.format(event, buf); 045 } else { 046 formatWithInfo(event, buf); 047 } 048 } 049 private void formatWithInfo(final LogEvent event, final StringBuilder buf) { 050 final int startField = buf.length(); 051 converter.format(event, buf); 052 field.format(startField, buf); 053 } 054 055 public LogEventPatternConverter getConverter() { 056 return converter; 057 } 058 059 public FormattingInfo getFormattingInfo() { 060 return field; 061 } 062 063 /** 064 * Normally pattern formatters are not meant to handle Exceptions although few pattern formatters might. 065 * <p> 066 * By examining the return values for this method, the containing layout will determine whether it handles 067 * throwables or not. 068 * </p> 069 * 070 * @return true if this PatternConverter handles throwables 071 */ 072 public boolean handlesThrowable() { 073 return converter.handlesThrowable(); 074 } 075 076 /** 077 * Most pattern formatters do not use location information. When they do they should return true here 078 * so that the logging system can efficiently capture it. 079 * @return true if location information is required. 080 */ 081 public boolean requiresLocation() { 082 return converter instanceof LocationAware && ((LocationAware) converter).requiresLocation(); 083 } 084 085 /** 086 * Returns a String suitable for debugging. 087 * 088 * @return a String suitable for debugging. 089 */ 090 @Override 091 public String toString() { 092 final StringBuilder sb = new StringBuilder(); 093 sb.append(super.toString()); 094 sb.append("[converter="); 095 sb.append(converter); 096 sb.append(", field="); 097 sb.append(field); 098 sb.append(']'); 099 return sb.toString(); 100 } 101}