View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.logging.log4j.log4j12;
19  
20  import java.util.List;
21  
22  import org.apache.commons.logging.log4j.StandardTests;
23  import org.apache.log4j.AppenderSkeleton;
24  import org.apache.log4j.spi.LoggingEvent;
25  
26  /**
27   * A custom implementation of {@code org.apache.log4j.Appender} which
28   * converts the Log4j-specific log event record into a representation that
29   * doesn't have a dependency on log4j and stores that new representation into
30   * an external list.
31   */
32  
33  public class TestAppender extends AppenderSkeleton {
34  
35      // The set of logged events for this appender
36      private final List events;
37  
38      /**
39       * Constructs a new instance.
40       */
41      public TestAppender(final List logEvents) {
42          events = logEvents;
43      }
44  
45      @Override
46      protected void append(final LoggingEvent event) {
47          final StandardTests.LogEvent lev = new StandardTests.LogEvent();
48  
49          lev.level = event.getLevel().toString();
50  
51          if (event.getMessage() == null) {
52              lev.msg = null;
53          } else {
54              lev.msg = event.getMessage().toString();
55          }
56  
57          if (event.getThrowableInformation() == null) {
58              lev.throwable = null;
59          } else {
60              lev.throwable = event.getThrowableInformation().getThrowable();
61          }
62  
63          events.add(lev);
64      }
65  
66      @Override
67      public void close() {
68      }
69  
70      @Override
71      public boolean requiresLayout() {
72          return false;
73      }
74  
75  }