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.log4j;
018
019import org.apache.log4j.spi.ErrorHandler;
020import org.apache.log4j.spi.Filter;
021import org.apache.log4j.spi.LoggingEvent;
022import org.apache.log4j.spi.OptionHandler;
023
024/**
025 * The base class for Appenders in Log4j 1. Appenders constructed using this are ignored in Log4j 2.
026 */
027public abstract class AppenderSkeleton implements Appender, OptionHandler {
028
029    protected Layout layout;
030
031    protected String name;
032
033    protected Priority threshold;
034
035    protected ErrorHandler errorHandler = new NoOpErrorHandler();
036
037    protected Filter headFilter;
038
039    protected Filter tailFilter;
040
041    protected boolean closed = false;
042
043    /**
044     * Create new instance.
045     */
046    public AppenderSkeleton() {
047    }
048
049    protected AppenderSkeleton(final boolean isActive) {
050    }
051
052    @Override
053    public void activateOptions() {
054    }
055
056    @Override
057    public void addFilter(final Filter newFilter) {
058        if(headFilter == null) {
059            headFilter = tailFilter = newFilter;
060        } else {
061            tailFilter.setNext(newFilter);
062            tailFilter = newFilter;
063        }
064    }
065
066    protected abstract void append(LoggingEvent event);
067
068    @Override
069    public void clearFilters() {
070        headFilter = tailFilter = null;
071    }
072
073    @Override
074    public void finalize() {
075    }
076
077    @Override
078    public ErrorHandler getErrorHandler() {
079        return this.errorHandler;
080    }
081
082    @Override
083    public Filter getFilter() {
084        return headFilter;
085    }
086
087    public final Filter getFirstFilter() {
088        return headFilter;
089    }
090
091    @Override
092    public Layout getLayout() {
093        return layout;
094    }
095
096    @Override
097    public final String getName() {
098        return this.name;
099    }
100
101    public Priority getThreshold() {
102        return threshold;
103    }
104
105    public boolean isAsSevereAsThreshold(final Priority priority) {
106        return ((threshold == null) || priority.isGreaterOrEqual(threshold));
107    }
108
109    /**
110     * This method is never going to be called in Log4j 2 so there isn't much point in having any code in it.
111     * @param event The LoggingEvent.
112     */
113    @Override
114    public void doAppend(final LoggingEvent event) {
115        append(event);
116    }
117
118    /**
119     * Sets the {@link ErrorHandler} for this Appender.
120     *
121     * @since 0.9.0
122     */
123    @Override
124    public synchronized void setErrorHandler(final ErrorHandler eh) {
125        if (eh != null) {
126            this.errorHandler = eh;
127        }
128    }
129
130    @Override
131    public void setLayout(final Layout layout) {
132        this.layout = layout;
133    }
134
135    @Override
136    public void setName(final String name) {
137        this.name = name;
138    }
139
140    public void setThreshold(final Priority threshold) {
141        this.threshold = threshold;
142    }
143
144    public static class NoOpErrorHandler implements ErrorHandler {
145        @Override
146        public void setLogger(final Logger logger) {
147
148        }
149
150        @Override
151        public void error(final String message, final Exception e, final int errorCode) {
152
153        }
154
155        @Override
156        public void error(final String message) {
157
158        }
159
160        @Override
161        public void error(final String message, final Exception e, final int errorCode, final LoggingEvent event) {
162
163        }
164
165        @Override
166        public void setAppender(final Appender appender) {
167
168        }
169
170        @Override
171        public void setBackupAppender(final Appender appender) {
172
173        }
174    }
175}