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 */ 017 package org.apache.logging.log4j.core.config; 018 019 import org.apache.logging.log4j.Level; 020 import org.apache.logging.log4j.core.Appender; 021 import org.apache.logging.log4j.core.Filter; 022 import org.apache.logging.log4j.core.LogEvent; 023 import org.apache.logging.log4j.core.appender.AppenderRuntimeException; 024 import org.apache.logging.log4j.core.filter.AbstractFilterable; 025 import org.apache.logging.log4j.core.filter.Filterable; 026 027 /** 028 * Wraps an {@link Appender} with details an appender implementation shouldn't need to know about. 029 */ 030 public class AppenderControl extends AbstractFilterable { 031 032 private final ThreadLocal<AppenderControl> recursive = new ThreadLocal<AppenderControl>(); 033 034 private final Appender appender; 035 036 private final Level level; 037 private final int intLevel; 038 039 /** 040 * Constructor. 041 * @param appender The target Appender. 042 * @param level the Level to filter on. 043 * @param filter the Filter(s) to apply. 044 */ 045 public AppenderControl(Appender appender, Level level, Filter filter) { 046 super(filter); 047 this.appender = appender; 048 this.level = level; 049 this.intLevel = level == null ? Level.ALL.intLevel() : level.intLevel(); 050 startFilter(); 051 } 052 053 /** 054 * Returns the Appender. 055 * @return the Appender. 056 */ 057 public Appender getAppender() { 058 return appender; 059 } 060 061 /** 062 * Call the appender. 063 * @param event The event to process. 064 */ 065 public void callAppender(LogEvent event) { 066 if (getFilter() != null) { 067 Filter.Result r = getFilter().filter(event); 068 if (r == Filter.Result.DENY) { 069 return; 070 } 071 } 072 if (level != null) { 073 if (intLevel < event.getLevel().intLevel()) { 074 return; 075 } 076 } 077 if (recursive.get() != null) { 078 appender.getHandler().error("Recursive call to appender " + appender.getName()); 079 return; 080 } 081 try { 082 recursive.set(this); 083 084 if (!appender.isStarted()) { 085 appender.getHandler().error("Attempted to append to non-started appender " + appender.getName()); 086 087 if (!appender.isExceptionSuppressed()) { 088 throw new AppenderRuntimeException( 089 "Attempted to append to non-started appender " + appender.getName()); 090 } 091 } 092 093 if (appender instanceof Filterable && ((Filterable) appender).isFiltered(event)) { 094 return; 095 } 096 097 try { 098 appender.append(event); 099 } catch (RuntimeException ex) { 100 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex); 101 if (!appender.isExceptionSuppressed()) { 102 throw ex; 103 } 104 } catch (Exception ex) { 105 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex); 106 if (!appender.isExceptionSuppressed()) { 107 throw new AppenderRuntimeException(ex); 108 } 109 } 110 } finally { 111 recursive.set(null); 112 } 113 } 114 115 }