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