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.util.concurrent.TimeUnit;
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.LogEvent;
026import org.apache.logging.log4j.core.StringLayout;
027import org.apache.logging.log4j.core.config.Property;
028
029/**
030 * Appends log events as strings to a writer.
031 *
032 * @param <M>
033 *            The kind of {@link WriterManager} under management
034 */
035public abstract class AbstractWriterAppender<M extends WriterManager> extends AbstractAppender {
036
037    /**
038     * Immediate flush means that the underlying writer will be flushed at the
039     * end of each append operation. Immediate flush is slower but ensures that
040     * each append request is actually written. If <code>immediateFlush</code>
041     * is set to {@code false}, then there is a good chance that the last few
042     * logs events are not actually written to persistent media if and when the
043     * application crashes.
044     */
045    protected final boolean immediateFlush;
046    private final M manager;
047    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
048    private final Lock readLock = readWriteLock.readLock();
049
050    /**
051     * Instantiates.
052     *
053     * @param name
054     *            The name of the Appender.
055     * @param layout
056     *            The layout to format the message.
057     * @param properties
058     *            Optional properties.
059     * @param manager
060     *            The OutputStreamManager.
061     */
062    protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
063            final boolean ignoreExceptions, final boolean immediateFlush, final Property[] properties, final M manager) {
064        super(name, filter, layout, ignoreExceptions, properties);
065        this.manager = manager;
066        this.immediateFlush = immediateFlush;
067    }
068
069    /**
070     * Instantiates.
071     *
072     * @param name
073     *            The name of the Appender.
074     * @param layout
075     *            The layout to format the message.
076     * @param manager
077     *            The OutputStreamManager.
078     * @deprecated Use {@link #AbstractWriterAppender(String, StringLayout, Filter, boolean, boolean, Property[], WriterManager)}.
079     */
080    @Deprecated
081    protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
082            final boolean ignoreExceptions, final boolean immediateFlush, final M manager) {
083        super(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
084        this.manager = manager;
085        this.immediateFlush = immediateFlush;
086    }
087
088    /**
089     * Actual writing occurs here.
090     * <p>
091     * Most subclasses will need to override this method.
092     * </p>
093     *
094     * @param event
095     *            The LogEvent.
096     */
097    @Override
098    public void append(final LogEvent event) {
099        readLock.lock();
100        try {
101            final String str = getStringLayout().toSerializable(event);
102            if (str.length() > 0) {
103                manager.write(str);
104                if (this.immediateFlush || event.isEndOfBatch()) {
105                    manager.flush();
106                }
107            }
108        } catch (final AppenderLoggingException ex) {
109            error("Unable to write " + manager.getName() + " for appender " + getName(), event, ex);
110            throw ex;
111        } finally {
112            readLock.unlock();
113        }
114    }
115
116    /**
117     * Gets the manager.
118     *
119     * @return the manager.
120     */
121    public M getManager() {
122        return manager;
123    }
124
125    public StringLayout getStringLayout() {
126        return (StringLayout) getLayout();
127    }
128
129    @Override
130    public void start() {
131        if (getLayout() == null) {
132            LOGGER.error("No layout set for the appender named [{}].", getName());
133        }
134        if (manager == null) {
135            LOGGER.error("No OutputStreamManager set for the appender named [{}].", getName());
136        }
137        super.start();
138    }
139
140    @Override
141    public boolean stop(final long timeout, final TimeUnit timeUnit) {
142        setStopping();
143        boolean stopped = super.stop(timeout, timeUnit, false);
144        stopped &= manager.stop(timeout, timeUnit);
145        setStopped();
146        return stopped;
147    }
148}