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