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.log4j.pattern;
018
019import org.apache.logging.log4j.core.LogEvent;
020import org.apache.logging.log4j.core.config.plugins.Plugin;
021import org.apache.logging.log4j.core.pattern.ConverterKeys;
022import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
023import org.apache.logging.log4j.core.pattern.PatternConverter;
024import org.apache.logging.log4j.util.Strings;
025
026import java.util.List;
027
028
029/**
030 * Returns the event's NDC in a StringBuilder.
031 */
032@Plugin(name = "Log4j1NdcPatternConverter", category = PatternConverter.CATEGORY)
033@ConverterKeys({ "ndc" })
034public final class Log4j1NdcPatternConverter extends LogEventPatternConverter {
035    /**
036     * Singleton.
037     */
038    private static final Log4j1NdcPatternConverter INSTANCE =
039        new Log4j1NdcPatternConverter();
040
041    /**
042     * Private constructor.
043     */
044    private Log4j1NdcPatternConverter() {
045        super("Log4j1NDC", "ndc");
046    }
047
048    /**
049     * Obtains an instance of NdcPatternConverter.
050     *
051     * @param options options, may be null.
052     * @return instance of NdcPatternConverter.
053     */
054    public static Log4j1NdcPatternConverter newInstance(final String[] options) {
055        return INSTANCE;
056    }
057
058    @Override
059    public void format(final LogEvent event, final StringBuilder toAppendTo) {
060        final List<String> ndc = event.getContextStack().asList();
061        toAppendTo.append(Strings.join(ndc, ' '));
062    }
063}