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;
021
022
023/**
024 * Formats a string literal.
025 */
026public final class LiteralPatternConverter extends LogEventPatternConverter implements ArrayPatternConverter {
027    /**
028     * String literal.
029     */
030    private final String literal;
031
032    private final Configuration config;
033
034    private final boolean substitute;
035
036    /**
037     * Create a new instance.
038     *
039     * @param config The Configuration.
040     * @param literal string literal.
041     */
042    public LiteralPatternConverter(final Configuration config, final String literal) {
043        super("Literal", "literal");
044        this.literal = literal;
045        this.config = config;
046        substitute = config != null && literal.contains("${");
047    }
048
049    /**
050     * {@inheritDoc}
051     */
052    @Override
053    public void format(final LogEvent event, final StringBuilder toAppendTo) {
054        toAppendTo.append(substitute ? config.getStrSubstitutor().replace(event, literal) : literal);
055    }
056    
057    /**
058     * {@inheritDoc}
059     */
060    @Override
061    public void format(final Object obj, final StringBuilder output) {
062        output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public void format(final StringBuilder output, final Object... objects) {
070        output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
071    }
072
073    public String getLiteral() {
074        return literal;
075    }
076}