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        super();
048    }
049
050    protected AppenderSkeleton(final boolean isActive) {
051        super();
052    }
053
054    public void activateOptions() {
055    }
056
057    public void addFilter(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    public void clearFilters() {
069        headFilter = tailFilter = null;
070    }
071
072    public void finalize() {
073    }
074
075    public ErrorHandler getErrorHandler() {
076        return this.errorHandler;
077    }
078
079    public Filter getFilter() {
080        return headFilter;
081    }
082
083    public final Filter getFirstFilter() {
084        return headFilter;
085    }
086
087    public Layout getLayout() {
088        return layout;
089    }
090
091    public final String getName() {
092        return this.name;
093    }
094
095    public Priority getThreshold() {
096        return threshold;
097    }
098
099    public boolean isAsSevereAsThreshold(Priority priority) {
100        return ((threshold == null) || priority.isGreaterOrEqual(threshold));
101    }
102
103    /**
104     * This method is never going to be called in Log4j 2 so there isn't much point in having any code in it.
105     * @param event The LoggingEvent.
106     */
107    public void doAppend(LoggingEvent event) {
108    }
109
110    /**
111     * Set the {@link ErrorHandler} for this Appender.
112     *
113     * @since 0.9.0
114     */
115    public synchronized void setErrorHandler(ErrorHandler eh) {
116        if (eh != null) {
117            this.errorHandler = eh;
118        }
119    }
120
121    public void setLayout(Layout layout) {
122        this.layout = layout;
123    }
124
125    public void setName(String name) {
126        this.name = name;
127    }
128
129    public void setThreshold(Priority threshold) {
130        this.threshold = threshold;
131    }
132
133    public static class NoOpErrorHandler implements ErrorHandler {
134        @Override
135        public void setLogger(Logger logger) {
136
137        }
138
139        @Override
140        public void error(String message, Exception e, int errorCode) {
141
142        }
143
144        @Override
145        public void error(String message) {
146
147        }
148
149        @Override
150        public void error(String message, Exception e, int errorCode, LoggingEvent event) {
151
152        }
153
154        @Override
155        public void setAppender(Appender appender) {
156
157        }
158
159        @Override
160        public void setBackupAppender(Appender appender) {
161
162        }
163    }
164}