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 org.apache.logging.log4j.LogManager;
020    import org.apache.logging.log4j.core.Filter;
021    import org.apache.logging.log4j.core.Layout;
022    import org.apache.logging.log4j.core.LogEvent;
023    import org.apache.logging.log4j.core.LoggerContext;
024    import org.apache.logging.log4j.core.appender.AbstractAppender;
025    import org.apache.logging.log4j.core.config.plugins.Plugin;
026    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
027    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
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.impl.ContextAnchor;
031    import org.apache.logging.log4j.core.layout.AbstractStringLayout;
032    import org.apache.logging.log4j.core.layout.PatternLayout;
033    import org.apache.logging.log4j.core.util.Booleans;
034    
035    import javax.servlet.ServletContext;
036    import java.io.Serializable;
037    
038    /**
039     * Logs using the ServletContext's log method
040     */
041    @Plugin(name = "Servlet", category = "Core", elementType = "appender", printObject = true)
042    public class ServletAppender extends AbstractAppender {
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 Console Appender.
058         * @param layout The layout to use (required).
059         * @param filter The Filter or null.
060         * @param name The name of the Appender (required).
061         * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
062         *               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") final String name,
070                @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final String ignore) {
071            if (name == null) {
072                LOGGER.error("No name provided for ConsoleAppender");
073                return null;
074            }
075            final ServletContext servletContext = getServletContext();
076            if (servletContext == null) {
077                LOGGER.error("No servlet context is available");
078                return null;
079            }
080            if (layout == null) {
081                layout = PatternLayout.createDefaultLayout();
082            } else if (!(layout instanceof AbstractStringLayout)) {
083                LOGGER.error("Layout must be a StringLayout to log to ServletContext");
084                return null;
085            }
086            final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
087            return new ServletAppender(name, (AbstractStringLayout) layout, filter, servletContext, ignoreExceptions);
088        }
089    
090        private static ServletContext getServletContext() {
091            LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
092            if (lc == null) {
093                lc = (LoggerContext) LogManager.getContext(false);
094            }
095            if (lc != null) {
096                final Object obj = lc.getExternalContext();
097                return obj != null && obj instanceof ServletContext ? (ServletContext) obj : null;
098            }
099            return null;
100        }
101    }