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.config.plugins.PluginBuilderAttribute;
027import org.apache.logging.log4j.core.config.plugins.PluginElement;
028import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
029import org.apache.logging.log4j.core.filter.AbstractFilterable;
030import org.apache.logging.log4j.core.layout.PatternLayout;
031import org.apache.logging.log4j.core.util.Integers;
032
033/**
034 * Abstract base class for Appenders. Although Appenders do not have to extend this class, doing so will simplify their
035 * implementation.
036 */
037public abstract class AbstractAppender extends AbstractFilterable implements Appender {
038
039    /**
040     * Subclasses can extend this abstract Builder. 
041     * 
042     * @param <B> This builder class.
043     */
044    public abstract static class Builder<B extends Builder<B>> extends AbstractFilterable.Builder<B> {
045
046        @PluginBuilderAttribute
047        private boolean ignoreExceptions = true;
048        
049        @PluginElement("Layout")
050        private Layout<? extends Serializable> layout;
051
052        @PluginBuilderAttribute
053        @Required
054        private String name;
055
056        public String getName() {
057            return name;
058        }
059
060        public boolean isIgnoreExceptions() {
061            return ignoreExceptions;
062        }
063
064        public Layout<? extends Serializable> getLayout() {
065            return layout;
066        }
067
068        public B withName(final String name) {
069            this.name = name;
070            return asBuilder();
071        }
072
073        public B withIgnoreExceptions(final boolean ignoreExceptions) {
074            this.ignoreExceptions = ignoreExceptions;
075            return asBuilder();
076        }
077
078        public B withLayout(final Layout<? extends Serializable> layout) {
079            this.layout = layout;
080            return asBuilder();
081        }
082
083        public Layout<? extends Serializable> getOrCreateLayout() {
084            if (layout == null) {
085                return PatternLayout.createDefaultLayout();
086            }
087            return layout;
088        }
089        
090    }
091    
092    private final String name;
093    private final boolean ignoreExceptions;
094    private final Layout<? extends Serializable> layout;
095    private ErrorHandler handler = new DefaultErrorHandler(this);
096
097    /**
098     * Constructor that defaults to suppressing exceptions.
099     * 
100     * @param name The Appender name.
101     * @param filter The Filter to associate with the Appender.
102     * @param layout The layout to use to format the event.
103     */
104    protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
105        this(name, filter, layout, true);
106    }
107
108    /**
109     * Constructor.
110     * 
111     * @param name The Appender name.
112     * @param filter The Filter to associate with the Appender.
113     * @param layout The layout to use to format the event.
114     * @param ignoreExceptions If true, exceptions will be logged and suppressed. If false errors will be logged and
115     *            then passed to the application.
116     */
117    protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
118            final boolean ignoreExceptions) {
119        super(filter);
120        this.name = name;
121        this.layout = layout;
122        this.ignoreExceptions = ignoreExceptions;
123    }
124
125    public static int parseInt(final String s, final int defaultValue) {
126        try {
127            return Integers.parseInt(s, defaultValue);
128        } catch (final NumberFormatException e) {
129            LOGGER.error("Could not parse \"{}\" as an integer,  using default value {}: {}", s, defaultValue, e);
130            return defaultValue;
131        }
132    }
133
134    /**
135     * Handle an error with a message using the {@link ErrorHandler} configured for this Appender.
136     * 
137     * @param msg The message.
138     */
139    public void error(final String msg) {
140        handler.error(msg);
141    }
142
143    /**
144     * Handle an error with a message, exception, and a logging event, using the {@link ErrorHandler} configured for
145     * this Appender.
146     * 
147     * @param msg The message.
148     * @param event The LogEvent.
149     * @param t The Throwable.
150     */
151    public void error(final String msg, final LogEvent event, final Throwable t) {
152        handler.error(msg, event, t);
153    }
154
155    /**
156     * Handle an error with a message and an exception using the {@link ErrorHandler} configured for this Appender.
157     * 
158     * @param msg The message.
159     * @param t The Throwable.
160     */
161    public void error(final String msg, final Throwable t) {
162        handler.error(msg, t);
163    }
164
165    /**
166     * Returns the ErrorHandler, if any.
167     * 
168     * @return The ErrorHandler.
169     */
170    @Override
171    public ErrorHandler getHandler() {
172        return handler;
173    }
174
175    /**
176     * Returns the Layout for the appender.
177     * 
178     * @return The Layout used to format the event.
179     */
180    @Override
181    public Layout<? extends Serializable> getLayout() {
182        return layout;
183    }
184
185    /**
186     * Returns the name of the Appender.
187     * 
188     * @return The name of the Appender.
189     */
190    @Override
191    public String getName() {
192        return name;
193    }
194
195    /**
196     * Some appenders need to propagate exceptions back to the application. When {@code ignoreExceptions} is
197     * {@code false} the AppenderControl will allow the exception to percolate.
198     *
199     * @return {@code true} if exceptions will be logged but now thrown, {@code false} otherwise.
200     */
201    @Override
202    public boolean ignoreExceptions() {
203        return ignoreExceptions;
204    }
205
206    /**
207     * The handler must be set before the appender is started.
208     * 
209     * @param handler The ErrorHandler to use.
210     */
211    @Override
212    public void setHandler(final ErrorHandler handler) {
213        if (handler == null) {
214            LOGGER.error("The handler cannot be set to null");
215        }
216        if (isStarted()) {
217            LOGGER.error("The handler cannot be changed once the appender is started");
218            return;
219        }
220        this.handler = handler;
221    }
222
223    @Override
224    public String toString() {
225        return name;
226    }
227
228}