View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2021, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.PatternLayout;
17  import ch.qos.logback.classic.spi.ILoggingEvent;
18  import ch.qos.logback.core.pattern.CompositeConverter;
19  import ch.qos.logback.core.pattern.Converter;
20  
21  public class PrefixCompositeConverter extends CompositeConverter<ILoggingEvent> {
22  
23      public String convert(ILoggingEvent event) {
24          StringBuilder buf = new StringBuilder();
25          Converter<ILoggingEvent> childConverter = this.getChildConverter();
26  
27          for (Converter<ILoggingEvent> c = childConverter; c != null; c = c.getNext()) {
28              if (c instanceof MDCConverter) {
29                  MDCConverter mdcConverter = (MDCConverter) c;
30  
31                  String key = mdcConverter.getKey();
32                  if (key != null) {
33                      buf.append(key).append("=");
34                  }
35              } else if (c instanceof PropertyConverter) {
36                  PropertyConverter pc = (PropertyConverter) c;
37                  String key = pc.getKey();
38                  if (key != null) {
39                      buf.append(key).append("=");
40                  }
41              } else {
42                  String classOfConverter = c.getClass().getName();
43  
44                  String key = PatternLayout.CONVERTER_CLASS_TO_KEY_MAP.get(classOfConverter);
45                  if (key != null)
46                      buf.append(key).append("=");
47              }
48              buf.append(c.convert(event));
49          }
50          return buf.toString();
51      }
52  
53      protected String transform(ILoggingEvent event, String in) {
54          throw new UnsupportedOperationException();
55      }
56  }