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.rewrite;
    18
    19  import org.apache.log4j.Appender;
    20  import org.apache.log4j.AppenderSkeleton;
    21  import org.apache.log4j.helpers.AppenderAttachableImpl;
    22  import org.apache.log4j.spi.AppenderAttachable;
    23  import org.apache.log4j.spi.LoggingEvent;
    24  import org.apache.log4j.spi.OptionHandler;
    25  import org.apache.log4j.xml.UnrecognizedElementHandler;
    26  import org.w3c.dom.Element;
    27
    28  import java.util.Enumeration;
    29  import java.util.Properties;
    30
    31  /**
    32   * This appender forwards a logging request to another
    33   * appender after possibly rewriting the logging event.
    34   *
    35   * This appender (with the appropriate policy)
    36   * replaces the MapFilter, PropertyFilter and ReflectionFilter
    37   * from log4j 1.3.
    38   */
    39  public class RewriteAppender extends AppenderSkeleton
    40       implements AppenderAttachable, UnrecognizedElementHandler {
    41      /**
    42       * Rewrite policy.
    43       */
    44      private RewritePolicy policy;
    45      /**
    46       * Nested appenders.
    47       */
    48      private final AppenderAttachableImpl appenders;
    49
    50      public RewriteAppender() {
    51          appenders = new AppenderAttachableImpl();
    52      }
    53
    54      /**
    55       * {@inheritDoc}
    56       */
    57      protected void append(final LoggingEvent event) {
    58          LoggingEvent rewritten = event;
    59          if (policy != null) {
    60              rewritten = policy.rewrite(event);
    61          }
    62          if (rewritten != null) {
    63              synchronized (appenders) {
    64                appenders.appendLoopOnAppenders(rewritten);
    65              }
    66          }
    67      }
    68
    69      /**
    70       * Add appender.
    71       *
    72       * @param newAppender appender to add, may not be null.
    73       */
    74      public void addAppender(final Appender newAppender) {
    75        synchronized (appenders) {
    76          appenders.addAppender(newAppender);
    77        }
    78      }
    79
    80      /**
    81       * Get iterator over attached appenders.
    82       * @return iterator or null if no attached appenders.
    83       */
    84      public Enumeration getAllAppenders() {
    85        synchronized (appenders) {
    86          return appenders.getAllAppenders();
    87        }
    88      }
    89
    90      /**
    91       * Get appender by name.
    92       *
    93       * @param name name, may not be null.
    94       * @return matching appender or null.
    95       */
    96      public Appender getAppender(final String name) {
    97        synchronized (appenders) {
    98          return appenders.getAppender(name);
    99        }
    100     }
    101
    102
    103     /**
    104      * Close this <code>AsyncAppender</code> by interrupting the dispatcher
    105      * thread which will process all pending events before exiting.
    106      */
    107     public void close() {
    108       closed = true;
    109       //
    110       //    close all attached appenders.
    111       //
    112       synchronized (appenders) {
    113         Enumeration iter = appenders.getAllAppenders();
    114
    115         if (iter != null) {
    116           while (iter.hasMoreElements()) {
    117             Object next = iter.nextElement();
    118
    119             if (next instanceof Appender) {
    120               ((Appender) next).close();
    121             }
    122           }
    123         }
    124       }
    125     }
    126
    127     /**
    128      * Determines if specified appender is attached.
    129      * @param appender appender.
    130      * @return true if attached.
    131      */
    132     public boolean isAttached(final Appender appender) {
    133       synchronized (appenders) {
    134         return appenders.isAttached(appender);
    135       }
    136     }
    137
    138     /**
    139      * {@inheritDoc}
    140      */
    141     public boolean requiresLayout() {
    142       return false;
    143     }
    144
    145     /**
    146      * Removes and closes all attached appenders.
    147      */
    148     public void removeAllAppenders() {
    149       synchronized (appenders) {
    150         appenders.removeAllAppenders();
    151       }
    152     }
    153
    154     /**
    155      * Removes an appender.
    156      * @param appender appender to remove.
    157      */
    158     public void removeAppender(final Appender appender) {
    159       synchronized (appenders) {
    160         appenders.removeAppender(appender);
    161       }
    162     }
    163
    164     /**
    165      * Remove appender by name.
    166      * @param name name.
    167      */
    168     public void removeAppender(final String name) {
    169       synchronized (appenders) {
    170         appenders.removeAppender(name);
    171       }
    172     }
    173
    174
    175     public void setRewritePolicy(final RewritePolicy rewritePolicy) {
    176         policy = rewritePolicy;
    177     }
    178     /**
    179      * {@inheritDoc}
    180      */
    181     public boolean parseUnrecognizedElement(final Element element,
    182                                             final Properties props) throws Exception {
    183         final String nodeName = element.getNodeName();
    184         if ("rewritePolicy".equals(nodeName)) {
    185             Object rewritePolicy =
    186                     org.apache.log4j.xml.DOMConfigurator.parseElement(
    187                             element, props, RewritePolicy.class);
    188             if (rewritePolicy != null) {
    189                 if (rewritePolicy instanceof OptionHandler) {
    190                     ((OptionHandler) rewritePolicy).activateOptions();
    191                 }
    192                 this.setRewritePolicy((RewritePolicy) rewritePolicy);
    193             }
    194             return true;
    195         }
    196         return false;
    197     }
    198
    199 }