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