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.config.Configuration;
021import org.apache.logging.log4j.core.util.OptionConverter;
022import org.apache.logging.log4j.util.PerformanceSensitive;
023
024
025/**
026 * Formats a string literal.
027 */
028@PerformanceSensitive("allocation") // except for replacements
029public final class LiteralPatternConverter extends LogEventPatternConverter implements ArrayPatternConverter {
030
031    /**
032     * String literal.
033     */
034    private final String literal;
035
036    private final Configuration config;
037
038    private final boolean substitute;
039
040    /**
041     * Create a new instance.
042     *
043     * @param config The Configuration.
044     * @param literal string literal.
045     * @param convertBackslashes if {@code true}, backslash characters are treated as escape characters and character
046     *            sequences like "\" followed by "t" (backslash+t) are converted to special characters like '\t' (tab).
047     */
048    public LiteralPatternConverter(final Configuration config, final String literal, final boolean convertBackslashes) {
049        super("Literal", "literal");
050        this.literal = convertBackslashes ? OptionConverter.convertSpecialChars(literal) : literal; // LOG4J2-829
051        this.config = config;
052        substitute = config != null && containsSubstitutionSequence(literal);
053    }
054
055    static boolean containsSubstitutionSequence(final String literal) {
056        return literal != null && literal.contains("${");
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public void format(final LogEvent event, final StringBuilder toAppendTo) {
064        toAppendTo.append(substitute ? config.getStrSubstitutor().replace(event, literal) : literal);
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public void format(final Object obj, final StringBuilder output) {
072        output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public void format(final StringBuilder output, final Object... objects) {
080        output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
081    }
082
083    public String getLiteral() {
084        return literal;
085    }
086
087    @Override
088    public boolean isVariable() {
089        return false;
090    }
091
092    @Override
093    public String toString() {
094        return "LiteralPatternConverter[literal=" + literal + ", config=" + config + ", substitute=" + substitute + "]";
095    }
096
097}