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.Event;
021    import org.apache.flume.EventDrivenSource;
022    import org.apache.flume.instrumentation.SourceCounter;
023    import org.apache.flume.source.AbstractSource;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    /**
028     *
029     */
030    public class Log4jEventSource extends AbstractSource implements EventDrivenSource {
031    
032        private static final Logger LOGGER = LoggerFactory.getLogger(Log4jEventSource.class);
033    
034        private final SourceCounter sourceCounter = new SourceCounter("log4j");
035    
036        public Log4jEventSource() {
037            setName("Log4jEvent");
038        }
039    
040        @Override
041        public synchronized void start() {
042            super.start();
043    
044            LOGGER.info("Log4j Source started");
045        }
046    
047        @Override
048        public synchronized void stop() {
049            super.stop();
050    
051            LOGGER.info("Log4j Source stopped. Metrics {}", sourceCounter);
052        }
053    
054    
055        public void send(final Event event) {
056            sourceCounter.incrementAppendReceivedCount();
057            sourceCounter.incrementEventReceivedCount();
058            try {
059                getChannelProcessor().processEvent(event);
060            } catch (final ChannelException ex) {
061                LOGGER.warn("Unabled to process event {}" + event, ex);
062                throw ex;
063            }
064            sourceCounter.incrementAppendAcceptedCount();
065            sourceCounter.incrementEventAcceptedCount();
066        }
067    }