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.logging.log4j.flume.appender;
    18
    19  import org.apache.flume.ChannelException;
    20  import org.apache.flume.Event;
    21  import org.apache.flume.EventDrivenSource;
    22  import org.apache.flume.instrumentation.SourceCounter;
    23  import org.apache.flume.source.AbstractSource;
    24  import org.slf4j.Logger;
    25  import org.slf4j.LoggerFactory;
    26
    27  /**
    28   *
    29   */
    30  public class Log4jEventSource extends AbstractSource implements EventDrivenSource {
    31
    32      private static final Logger LOGGER = LoggerFactory.getLogger(Log4jEventSource.class);
    33
    34      private final SourceCounter sourceCounter = new SourceCounter("log4j");
    35
    36      public Log4jEventSource() {
    37          setName("Log4jEvent");
    38      }
    39
    40      @Override
    41      public synchronized void start() {
    42          super.start();
    43
    44          LOGGER.info("Log4j Source started");
    45      }
    46
    47      @Override
    48      public synchronized void stop() {
    49          super.stop();
    50
    51          LOGGER.info("Log4j Source stopped. Metrics {}", sourceCounter);
    52      }
    53
    54
    55      public void send(final Event event) {
    56          sourceCounter.incrementAppendReceivedCount();
    57          sourceCounter.incrementEventReceivedCount();
    58          try {
    59              getChannelProcessor().processEvent(event);
    60          } catch (final ChannelException ex) {
    61              LOGGER.warn("Unabled to process event {}" + event, ex);
    62              throw ex;
    63          }
    64          sourceCounter.incrementAppendAcceptedCount();
    65          sourceCounter.incrementEventAcceptedCount();
    66      }
    67  }