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