1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.helpers;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import ch.qos.logback.classic.PatternLayout;
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.core.AppenderBase;
22  
23  /**
24   * An appender used for testing.
25   * 
26   * @author ceki
27   * @since 1.3.0
28   */
29  public class WithLayoutListAppender extends AppenderBase<ILoggingEvent> {
30  
31      public List<String> list = new ArrayList<>();
32  
33      String pattern;
34  
35      PatternLayout patternLayout;
36  
37      @Override
38      public void start() {
39          if (pattern == null) {
40              addError("null pattern disallowed");
41              return;
42          }
43          patternLayout = new PatternLayout();
44          patternLayout.setContext(context);
45          patternLayout.setPattern(pattern);
46          patternLayout.start();
47          if (patternLayout.isStarted())
48              super.start();
49      }
50  
51      protected void append(ILoggingEvent e) {
52          String result = patternLayout.doLayout(e);
53          list.add(result);
54      }
55  
56      public String getPattern() {
57          return pattern;
58      }
59  
60      public void setPattern(String pattern) {
61          this.pattern = pattern;
62      }
63  
64  }