View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.web.appender;
18  
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.core.Filter;
21  import org.apache.logging.log4j.core.Layout;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.LoggerContext;
24  import org.apache.logging.log4j.core.appender.AbstractAppender;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
28  import org.apache.logging.log4j.core.config.plugins.PluginElement;
29  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
30  import org.apache.logging.log4j.core.impl.ContextAnchor;
31  import org.apache.logging.log4j.core.layout.AbstractStringLayout;
32  import org.apache.logging.log4j.core.layout.PatternLayout;
33  import org.apache.logging.log4j.core.util.Booleans;
34  
35  import javax.servlet.ServletContext;
36  import java.io.Serializable;
37  
38  /**
39   * Logs using the ServletContext's log method
40   */
41  @Plugin(name = "Servlet", category = "Core", elementType = "appender", printObject = true)
42  public class ServletAppender extends AbstractAppender {
43      private final ServletContext servletContext;
44  
45      private ServletAppender(final String name, final AbstractStringLayout layout, final Filter filter,
46                              final ServletContext servletContext, final boolean ignoreExceptions) {
47          super(name, filter, layout, ignoreExceptions);
48          this.servletContext = servletContext;
49      }
50  
51      @Override
52      public void append(final LogEvent event) {
53          servletContext.log(((AbstractStringLayout) getLayout()).toSerializable(event));
54      }
55  
56      /**
57       * Create a Console Appender.
58       * @param layout The layout to use (required).
59       * @param filter The Filter or null.
60       * @param name The name of the Appender (required).
61       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
62       *               they are propagated to the caller.
63       * @return The ServletAppender.
64       */
65      @PluginFactory
66      public static ServletAppender createAppender(
67              @PluginElement("Layout") Layout<? extends Serializable> layout,
68              @PluginElement("Filter") final Filter filter,
69              @PluginAttribute("name") final String name,
70              @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final String ignore) {
71          if (name == null) {
72              LOGGER.error("No name provided for ConsoleAppender");
73              return null;
74          }
75          final ServletContext servletContext = getServletContext();
76          if (servletContext == null) {
77              LOGGER.error("No servlet context is available");
78              return null;
79          }
80          if (layout == null) {
81              layout = PatternLayout.createDefaultLayout();
82          } else if (!(layout instanceof AbstractStringLayout)) {
83              LOGGER.error("Layout must be a StringLayout to log to ServletContext");
84              return null;
85          }
86          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
87          return new ServletAppender(name, (AbstractStringLayout) layout, filter, servletContext, ignoreExceptions);
88      }
89  
90      private static ServletContext getServletContext() {
91          LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
92          if (lc == null) {
93              lc = (LoggerContext) LogManager.getContext(false);
94          }
95          if (lc != null) {
96              final Object obj = lc.getExternalContext();
97              return obj != null && obj instanceof ServletContext ? (ServletContext) obj : null;
98          }
99          return null;
100     }
101 }