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.db;
018
019import java.util.concurrent.locks.Lock;
020import java.util.concurrent.locks.ReadWriteLock;
021import java.util.concurrent.locks.ReentrantReadWriteLock;
022
023import org.apache.logging.log4j.LoggingException;
024import org.apache.logging.log4j.core.Filter;
025import org.apache.logging.log4j.core.Layout;
026import org.apache.logging.log4j.core.LogEvent;
027import org.apache.logging.log4j.core.appender.AbstractAppender;
028import org.apache.logging.log4j.core.appender.AppenderLoggingException;
029
030/**
031 * An abstract Appender for writing events to a database of some type, be it relational or NoSQL. All database appenders
032 * should inherit from this base appender. Three implementations are currently provided:
033 * {@link org.apache.logging.log4j.core.appender.db.jdbc JDBC}, {@link org.apache.logging.log4j.core.appender.db.jpa
034 * JPA}, and <a href="/log4j/2.x/log4j-nosql/apidocs/">NoSQL</a>.
035 *
036 * @param <T> Specifies which type of {@link AbstractDatabaseManager} this Appender requires.
037 */
038public abstract class AbstractDatabaseAppender<T extends AbstractDatabaseManager> extends AbstractAppender {
039
040    private final ReadWriteLock lock = new ReentrantReadWriteLock();
041    private final Lock readLock = lock.readLock();
042    private final Lock writeLock = lock.writeLock();
043
044    private T manager;
045
046    /**
047     * Instantiates the base appender.
048     *
049     * @param name The appender name.
050     * @param filter The filter, if any, to use.
051     * @param ignoreExceptions If {@code true} exceptions encountered when appending events are logged; otherwise
052     *                         they are propagated to the caller.
053     * @param manager The matching {@link AbstractDatabaseManager} implementation.
054     */
055    protected AbstractDatabaseAppender(final String name, final Filter filter, final boolean ignoreExceptions,
056                                       final T manager) {
057        super(name, filter, null, ignoreExceptions);
058        this.manager = manager;
059    }
060
061    /**
062     * This always returns {@code null}, as database appenders do not use a single layout. The JPA and NoSQL appenders
063     * do not use a layout at all. The JDBC appender has a layout-per-column pattern.
064     *
065     * @return {@code null}.
066     */
067    @Override
068    public final Layout<LogEvent> getLayout() {
069        return null;
070    }
071
072    /**
073     * Returns the underlying manager in use within this appender.
074     *
075     * @return the manager.
076     */
077    public final T getManager() {
078        return this.manager;
079    }
080
081    @Override
082    public final void start() {
083        if (this.getManager() == null) {
084            LOGGER.error("No AbstractDatabaseManager set for the appender named [{}].", this.getName());
085        }
086        super.start();
087        if (this.getManager() != null) {
088            this.getManager().startup();
089        }
090    }
091
092    @Override
093    public final void stop() {
094        super.stop();
095        if (this.getManager() != null) {
096            this.getManager().release();
097        }
098    }
099
100    @Override
101    public final void append(final LogEvent event) {
102        this.readLock.lock();
103        try {
104            this.getManager().write(event);
105        } catch (final LoggingException e) {
106            LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
107                    this.getName(), e);
108            throw e;
109        } catch (final Exception e) {
110            LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
111                    this.getName(), e);
112            throw new AppenderLoggingException("Unable to write to database in appender: " + e.getMessage(), e);
113        } finally {
114            this.readLock.unlock();
115        }
116    }
117
118    /**
119     * Replaces the underlying manager in use within this appender. This can be useful for manually changing the way log
120     * events are written to the database without losing buffered or in-progress events. The existing manager is
121     * released only after the new manager has been installed. This method is thread-safe.
122     *
123     * @param manager The new manager to install.
124     */
125    protected final void replaceManager(final T manager) {
126        this.writeLock.lock();
127        try {
128            final T old = this.getManager();
129            if (!manager.isRunning()) {
130                manager.startup();
131            }
132            this.manager = manager;
133            old.release();
134        } finally {
135            this.writeLock.unlock();
136        }
137    }
138}