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.core.appender;
018    
019    import java.io.Serializable;
020    import java.util.concurrent.locks.Lock;
021    import java.util.concurrent.locks.ReadWriteLock;
022    import java.util.concurrent.locks.ReentrantReadWriteLock;
023    
024    import org.apache.logging.log4j.core.Filter;
025    import org.apache.logging.log4j.core.Layout;
026    import org.apache.logging.log4j.core.LogEvent;
027    
028    /**
029     * Appends log events as bytes to a byte output stream. The stream encoding is defined in the layout.
030     * 
031     * @param <M> The kind of {@link OutputStreamManager} under management
032     */
033    public abstract class AbstractOutputStreamAppender<M extends OutputStreamManager> extends AbstractAppender {
034    
035        private static final long serialVersionUID = 1L;
036    
037        /**
038         * Immediate flush means that the underlying writer or output stream
039         * will be flushed at the end of each append operation. Immediate
040         * flush is slower but ensures that each append request is actually
041         * written. If <code>immediateFlush</code> is set to
042         * {@code false}, then there is a good chance that the last few
043         * logs events are not actually written to persistent media if and
044         * when the application crashes.
045         */
046        protected final boolean immediateFlush;
047    
048        private final M manager;
049    
050        private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
051        private final Lock readLock = rwLock.readLock();
052        
053        /**
054         * Instantiate a WriterAppender and set the output destination to a
055         * new {@link java.io.OutputStreamWriter} initialized with <code>os</code>
056         * as its {@link java.io.OutputStream}.
057         * @param name The name of the Appender.
058         * @param layout The layout to format the message.
059         * @param manager The OutputStreamManager.
060         */
061        protected AbstractOutputStreamAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
062                                               final boolean ignoreExceptions, final boolean immediateFlush,
063                                               final M manager) {
064            super(name, filter, layout, ignoreExceptions);
065            this.manager = manager;
066            this.immediateFlush = immediateFlush;
067        }
068    
069        /**
070         * Gets the manager.
071         * 
072         * @return the manager.
073         */
074        public M getManager() {
075            return manager;
076        }
077    
078        @Override
079        public void start() {
080            if (getLayout() == null) {
081                LOGGER.error("No layout set for the appender named [" + getName() + "].");
082            }
083            if (manager == null) {
084                LOGGER.error("No OutputStreamManager set for the appender named [" + getName() + "].");
085            }
086            super.start();
087        }
088    
089        @Override
090        public void stop() {
091            super.stop();
092            manager.release();
093        }
094    
095        /**
096         * Actual writing occurs here.
097         * <p>
098         * Most subclasses of <code>AbstractOutputStreamAppender</code> will need to override this method.
099         * </p>
100         * 
101         * @param event
102         *        The LogEvent.
103         */
104        @Override
105        public void append(final LogEvent event) {
106            readLock.lock();
107            try {
108                final byte[] bytes = getLayout().toByteArray(event);
109                if (bytes.length > 0) {
110                    manager.write(bytes);
111                    if (this.immediateFlush || event.isEndOfBatch()) {
112                        manager.flush();
113                    }
114                }
115            } catch (final AppenderLoggingException ex) {
116                error("Unable to write to stream " + manager.getName() + " for appender " + getName());
117                throw ex;
118            } finally {
119                readLock.unlock();
120            }
121        }
122    }