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