001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.flume.appender;
018    
019    import org.apache.flume.ChannelException;
020    import org.apache.flume.EventDrivenSource;
021    import org.apache.flume.instrumentation.SourceCounter;
022    import org.apache.flume.source.AbstractSource;
023    import org.slf4j.LoggerFactory;
024    import org.slf4j.Logger;
025    
026    /**
027     *
028     */
029    public class Log4jEventSource extends AbstractSource implements EventDrivenSource {
030    
031        private static final Logger LOGGER = LoggerFactory.getLogger(Log4jEventSource.class);
032    
033        private final SourceCounter sourceCounter = new SourceCounter("log4j");
034    
035        public Log4jEventSource() {
036            setName("Log4jEvent");
037        }
038    
039        @Override
040        public synchronized void start() {
041            super.start();
042    
043            LOGGER.info("Log4j Source started");
044        }
045    
046        @Override
047        public synchronized void stop() {
048            super.stop();
049    
050            LOGGER.info("Log4j Source stopped. Metrics {}", sourceCounter);
051        }
052    
053    
054        public void send(final FlumeEvent event) {
055            sourceCounter.incrementAppendReceivedCount();
056            sourceCounter.incrementEventReceivedCount();
057            try {
058                getChannelProcessor().processEvent(event);
059            } catch (final ChannelException ex) {
060                LOGGER.warn("Unabled to process event {}" + event, ex);
061                throw ex;
062            }
063            sourceCounter.incrementAppendAcceptedCount();
064            sourceCounter.incrementEventAcceptedCount();
065        }
066    }