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.log4j.plugins;
    19
    20  import org.apache.log4j.Level;
    21  import org.apache.log4j.Logger;
    22  import org.apache.log4j.spi.LoggingEvent;
    23  import org.apache.log4j.spi.Thresholdable;
    24
    25
    26  /**
    27   * Defines the base class for Receiver plugins.
    28   * <p></p>
    29   * <p>Just as Appenders send logging events outside of the log4j
    30   * environment (to files, to smtp, to sockets, etc), Receivers bring
    31   * logging events inside the log4j environment.
    32   * <p></p>
    33   * <p>Receivers are meant to support the receiving of
    34   * remote logging events from another process.
    35   * <p></p>
    36   * <p>Receivers can also be used to "import" log messages from other
    37   * logging packages into the log4j environment.
    38   * <p></p>
    39   * <p>Receivers can be configured to post events to a given
    40   * LoggerRepository.
    41   * <p></p>
    42   * <p>Subclasses of Receiver must implement the isActive(),
    43   * activateOptions(), and shutdown() methods. The doPost() method
    44   * is provided to standardize the "import" of remote events into
    45   * the repository.
    46   *
    47   * @author Mark Womack
    48   * @author Ceki G&uuml;lc&uuml;
    49   * @author Paul Smith (psmith@apache.org)
    50   */
    51  public abstract class Receiver extends PluginSkeleton implements Thresholdable {
    52      /**
    53       * Threshold level.
    54       */
    55      protected Level thresholdLevel;
    56
    57      /**
    58       * Create new instance.
    59       */
    60      protected Receiver() {
    61          super();
    62      }
    63
    64      /**
    65       * Sets the receiver theshold to the given level.
    66       *
    67       * @param level The threshold level events must equal or be greater
    68       *              than before further processing can be done.
    69       */
    70      public void setThreshold(final Level level) {
    71          Level oldValue = this.thresholdLevel;
    72          thresholdLevel = level;
    73          firePropertyChange("threshold", oldValue, this.thresholdLevel);
    74      }
    75
    76      /**
    77       * Gets the current threshold setting of the receiver.
    78       *
    79       * @return Level The current threshold level of the receiver.
    80       */
    81      public Level getThreshold() {
    82          return thresholdLevel;
    83      }
    84
    85      /**
    86       * Returns true if the given level is equals or greater than the current
    87       * threshold value of the receiver.
    88       *
    89       * @param level The level to test against the receiver threshold.
    90       * @return boolean True if level is equal or greater than the
    91       * receiver threshold.
    92       */
    93      public boolean isAsSevereAsThreshold(final Level level) {
    94          return ((thresholdLevel == null)
    95              || level.isGreaterOrEqual(thresholdLevel));
    96      }
    97
    98      /**
    99       * Posts the logging event to a logger in the configured logger
    100      * repository.
    101      *
    102      * @param event the log event to post to the local log4j environment.
    103      */
    104     public void doPost(final LoggingEvent event) {
    105         // if event does not meet threshold, exit now
    106         if (!isAsSevereAsThreshold(event.getLevel())) {
    107             return;
    108         }
    109
    110         // get the "local" logger for this event from the
    111         // configured repository.
    112         Logger localLogger =
    113             getLoggerRepository().getLogger(event.getLoggerName());
    114
    115         // if the logger level is greater or equal to the level
    116         // of the event, use the logger to append the event.
    117         if (event.getLevel()
    118             .isGreaterOrEqual(localLogger.getEffectiveLevel())) {
    119             // call the loggers appenders to process the event
    120             localLogger.callAppenders(event);
    121         }
    122     }
    123 }