View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.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   * @param <E>
28   * @since 1.3.0
29   */
30  public class WithLayoutListAppender extends AppenderBase<ILoggingEvent> {
31  
32      public List<String> list = new ArrayList<>();
33  
34      String pattern;
35  
36      PatternLayout patternLayout;
37  
38      @Override
39      public void start() {
40          if (pattern == null) {
41              addError("null pattern disallowed");
42              return;
43          }
44          patternLayout = new PatternLayout();
45          patternLayout.setContext(context);
46          patternLayout.setPattern(pattern);
47          patternLayout.start();
48          if (patternLayout.isStarted())
49              super.start();
50      }
51  
52      protected void append(ILoggingEvent e) {
53          String result = patternLayout.doLayout(e);
54          list.add(result);
55      }
56  
57      public String getPattern() {
58          return pattern;
59      }
60  
61      public void setPattern(String pattern) {
62          this.pattern = pattern;
63      }
64  
65  }