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; 018 019import java.io.Serializable; 020 021import org.apache.logging.log4j.core.Appender; 022import org.apache.logging.log4j.core.ErrorHandler; 023import org.apache.logging.log4j.core.Filter; 024import org.apache.logging.log4j.core.Layout; 025import org.apache.logging.log4j.core.LogEvent; 026import org.apache.logging.log4j.core.filter.AbstractFilterable; 027import org.apache.logging.log4j.core.util.Integers; 028 029/** 030 * Abstract base class for Appenders. Although Appenders do not have to extend this class, doing so will simplify their 031 * implementation. 032 */ 033public abstract class AbstractAppender extends AbstractFilterable implements Appender { 034 035 private final String name; 036 private final boolean ignoreExceptions; 037 private final Layout<? extends Serializable> layout; 038 private ErrorHandler handler = new DefaultErrorHandler(this); 039 040 /** 041 * Constructor that defaults to suppressing exceptions. 042 * 043 * @param name The Appender name. 044 * @param filter The Filter to associate with the Appender. 045 * @param layout The layout to use to format the event. 046 */ 047 protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout) { 048 this(name, filter, layout, true); 049 } 050 051 /** 052 * Constructor. 053 * 054 * @param name The Appender name. 055 * @param filter The Filter to associate with the Appender. 056 * @param layout The layout to use to format the event. 057 * @param ignoreExceptions If true, exceptions will be logged and suppressed. If false errors will be logged and 058 * then passed to the application. 059 */ 060 protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, 061 final boolean ignoreExceptions) { 062 super(filter); 063 this.name = name; 064 this.layout = layout; 065 this.ignoreExceptions = ignoreExceptions; 066 } 067 068 public static int parseInt(final String s, final int defaultValue) { 069 try { 070 return Integers.parseInt(s, defaultValue); 071 } catch (final NumberFormatException e) { 072 LOGGER.error("Could not parse \"{}\" as an integer, using default value {}: {}", s, defaultValue, e); 073 return defaultValue; 074 } 075 } 076 077 /** 078 * Handle an error with a message using the {@link ErrorHandler} configured for this Appender. 079 * 080 * @param msg The message. 081 */ 082 public void error(final String msg) { 083 handler.error(msg); 084 } 085 086 /** 087 * Handle an error with a message, exception, and a logging event, using the {@link ErrorHandler} configured for 088 * this Appender. 089 * 090 * @param msg The message. 091 * @param event The LogEvent. 092 * @param t The Throwable. 093 */ 094 public void error(final String msg, final LogEvent event, final Throwable t) { 095 handler.error(msg, event, t); 096 } 097 098 /** 099 * Handle an error with a message and an exception using the {@link ErrorHandler} configured for this Appender. 100 * 101 * @param msg The message. 102 * @param t The Throwable. 103 */ 104 public void error(final String msg, final Throwable t) { 105 handler.error(msg, t); 106 } 107 108 /** 109 * Returns the ErrorHandler, if any. 110 * 111 * @return The ErrorHandler. 112 */ 113 @Override 114 public ErrorHandler getHandler() { 115 return handler; 116 } 117 118 /** 119 * Returns the Layout for the appender. 120 * 121 * @return The Layout used to format the event. 122 */ 123 @Override 124 public Layout<? extends Serializable> getLayout() { 125 return layout; 126 } 127 128 /** 129 * Returns the name of the Appender. 130 * 131 * @return The name of the Appender. 132 */ 133 @Override 134 public String getName() { 135 return name; 136 } 137 138 /** 139 * Some appenders need to propagate exceptions back to the application. When {@code ignoreExceptions} is 140 * {@code false} the AppenderControl will allow the exception to percolate. 141 * 142 * @return {@code true} if exceptions will be logged but now thrown, {@code false} otherwise. 143 */ 144 @Override 145 public boolean ignoreExceptions() { 146 return ignoreExceptions; 147 } 148 149 /** 150 * The handler must be set before the appender is started. 151 * 152 * @param handler The ErrorHandler to use. 153 */ 154 @Override 155 public void setHandler(final ErrorHandler handler) { 156 if (handler == null) { 157 LOGGER.error("The handler cannot be set to null"); 158 } 159 if (isStarted()) { 160 LOGGER.error("The handler cannot be changed once the appender is started"); 161 return; 162 } 163 this.handler = handler; 164 } 165 166 @Override 167 public String toString() { 168 return name; 169 } 170 171}