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  package org.apache.log4j.helpers;
18  
19  import org.apache.log4j.Appender;
20  import org.apache.log4j.spi.AppenderAttachable;
21  import org.apache.log4j.spi.LoggingEvent;
22  
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentMap;
27  
28  /**
29   * Allows Classes to attach Appenders.
30   */
31  public class AppenderAttachableImpl implements AppenderAttachable {
32  
33      private final ConcurrentMap<String, Appender> appenders = new ConcurrentHashMap<>();
34  
35      @Override
36      public void addAppender(Appender newAppender) {
37          if (newAppender != null) {
38              appenders.put(newAppender.getName(), newAppender);
39          }
40      }
41  
42      @Override
43      public Enumeration getAllAppenders() {
44          return Collections.enumeration(appenders.values());
45      }
46  
47      @Override
48      public Appender getAppender(String name) {
49          return appenders.get(name);
50      }
51  
52      @Override
53      public boolean isAttached(Appender appender) {
54          return appenders.containsValue(appender);
55      }
56  
57      @Override
58      public void removeAllAppenders() {
59          appenders.clear();
60      }
61  
62      @Override
63      public void removeAppender(Appender appender) {
64          appenders.remove(appender.getName(), appender);
65      }
66  
67      @Override
68      public void removeAppender(String name) {
69          appenders.remove(name);
70      }
71  
72      /**
73       * Call the <code>doAppend</code> method on all attached appenders.
74       * @param event The event to log.
75       * @return The number of appenders.
76       */
77      public int appendLoopOnAppenders(LoggingEvent event) {
78          for (Appender appender : appenders.values()) {
79              appender.doAppend(event);
80          }
81          return appenders.size();
82      }
83  
84      public void close() {
85          for (Appender appender : appenders.values()) {
86              appender.close();
87          }
88      }
89  }