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