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.web.appender;
018    
019    import java.io.Serializable;
020    import javax.servlet.ServletContext;
021    
022    import org.apache.logging.log4j.core.Filter;
023    import org.apache.logging.log4j.core.Layout;
024    import org.apache.logging.log4j.core.LogEvent;
025    import org.apache.logging.log4j.core.appender.AbstractAppender;
026    import org.apache.logging.log4j.core.config.plugins.Plugin;
027    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
028    import org.apache.logging.log4j.core.config.plugins.PluginElement;
029    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
030    import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
031    import org.apache.logging.log4j.core.layout.AbstractStringLayout;
032    import org.apache.logging.log4j.core.layout.PatternLayout;
033    import org.apache.logging.log4j.web.WebLoggerContextUtils;
034    
035    /**
036     * Logs using the ServletContext's log method
037     */
038    @Plugin(name = "Servlet", category = "Core", elementType = "appender", printObject = true)
039    public class ServletAppender extends AbstractAppender {
040    
041        private static final long serialVersionUID = 1L;
042    
043        private final ServletContext servletContext;
044    
045        private ServletAppender(final String name, final AbstractStringLayout layout, final Filter filter,
046                                final ServletContext servletContext, final boolean ignoreExceptions) {
047            super(name, filter, layout, ignoreExceptions);
048            this.servletContext = servletContext;
049        }
050    
051        @Override
052        public void append(final LogEvent event) {
053            servletContext.log(((AbstractStringLayout) getLayout()).toSerializable(event));
054        }
055    
056        /**
057         * Create a Servlet Appender.
058         * @param layout The layout to use (required). Must extend {@link AbstractStringLayout}.
059         * @param filter The Filter or null.
060         * @param name The name of the Appender (required).
061         * @param ignoreExceptions If {@code true} (default) exceptions encountered when appending events are logged;
062         *                         otherwise they are propagated to the caller.
063         * @return The ServletAppender.
064         */
065        @PluginFactory
066        public static ServletAppender createAppender(
067                @PluginElement("Layout") Layout<? extends Serializable> layout,
068                @PluginElement("Filter") final Filter filter,
069                @PluginAttribute("name")
070                @Required(message = "No name provided for ServletAppender")
071                final String name,
072                @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions) {
073            final ServletContext servletContext = WebLoggerContextUtils.getServletContext();
074            if (servletContext == null) {
075                LOGGER.error("No servlet context is available");
076                return null;
077            }
078            if (layout == null) {
079                layout = PatternLayout.createDefaultLayout();
080            } else if (!(layout instanceof AbstractStringLayout)) {
081                LOGGER.error("Layout must be a StringLayout to log to ServletContext");
082                return null;
083            }
084            return new ServletAppender(name, (AbstractStringLayout) layout, filter, servletContext, ignoreExceptions);
085        }
086    
087    }