001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2021, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.pattern;
015
016import ch.qos.logback.classic.PatternLayout;
017import ch.qos.logback.classic.spi.ILoggingEvent;
018import ch.qos.logback.core.pattern.CompositeConverter;
019import ch.qos.logback.core.pattern.Converter;
020
021public class PrefixCompositeConverter extends CompositeConverter<ILoggingEvent> {
022
023    public String convert(ILoggingEvent event) {
024        StringBuilder buf = new StringBuilder();
025        Converter<ILoggingEvent> childConverter = this.getChildConverter();
026
027        for (Converter<ILoggingEvent> c = childConverter; c != null; c = c.getNext()) {
028            if (c instanceof MDCConverter) {
029                MDCConverter mdcConverter = (MDCConverter) c;
030
031                String key = mdcConverter.getKey();
032                if (key != null) {
033                    buf.append(key).append("=");
034                }
035            } else if (c instanceof PropertyConverter) {
036                PropertyConverter pc = (PropertyConverter) c;
037                String key = pc.getKey();
038                if (key != null) {
039                    buf.append(key).append("=");
040                }
041            } else {
042                String classOfConverter = c.getClass().getName();
043
044                String key = PatternLayout.CONVERTER_CLASS_TO_KEY_MAP.get(classOfConverter);
045                if (key != null)
046                    buf.append(key).append("=");
047            }
048            buf.append(c.convert(event));
049        }
050        return buf.toString();
051    }
052
053    protected String transform(ILoggingEvent event, String in) {
054        throw new UnsupportedOperationException();
055    }
056}