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