Repository: logging-log4j2 Updated Branches: refs/heads/master 800db9c7a -> 817d89838
[LOG4J2-1608] ServletAppender does not provide throwable object to ServletContext. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ed3c803f Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ed3c803f Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ed3c803f Branch: refs/heads/master Commit: ed3c803f3def3a9cc80908bbca8b3abe5ed19170 Parents: b4fb664 Author: Gary Gregory <ggreg...@apache.org> Authored: Thu Sep 22 10:04:08 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Thu Sep 22 10:04:08 2016 -0700 ---------------------------------------------------------------------- .../log4j/web/appender/ServletAppender.java | 2 +- .../resources/WEB-INF/classes/log4j-servlet.xml | 64 ++++++++++---------- src/changes/changes.xml | 3 + src/site/xdoc/manual/webapp.xml | 26 ++++++++ 4 files changed, 62 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ed3c803f/log4j-web/src/main/java/org/apache/logging/log4j/web/appender/ServletAppender.java ---------------------------------------------------------------------- diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/appender/ServletAppender.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/appender/ServletAppender.java index 6cfa74e..8bddc47 100644 --- a/log4j-web/src/main/java/org/apache/logging/log4j/web/appender/ServletAppender.java +++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/appender/ServletAppender.java @@ -48,7 +48,7 @@ public class ServletAppender extends AbstractAppender { @Override public void append(final LogEvent event) { - servletContext.log(((AbstractStringLayout) getLayout()).toSerializable(event)); + servletContext.log(((AbstractStringLayout) getLayout()).toSerializable(event), event.getThrown()); } /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ed3c803f/log4j-web/src/test/resources/WEB-INF/classes/log4j-servlet.xml ---------------------------------------------------------------------- diff --git a/log4j-web/src/test/resources/WEB-INF/classes/log4j-servlet.xml b/log4j-web/src/test/resources/WEB-INF/classes/log4j-servlet.xml index b466c3d..017169c 100644 --- a/log4j-web/src/test/resources/WEB-INF/classes/log4j-servlet.xml +++ b/log4j-web/src/test/resources/WEB-INF/classes/log4j-servlet.xml @@ -1,33 +1,33 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - ---> -<Configuration status="WARN" name="ServletTest"> - - <Appenders> - <Servlet name="Servlet"> - <PatternLayout pattern="%m%n"/> - </Servlet> - </Appenders> - - <Loggers> - <Root level="debug"> - <AppenderRef ref="Servlet"/> - </Root> - </Loggers> - +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="WARN" name="ServletTest"> + + <Appenders> + <Servlet name="Servlet"> + <PatternLayout pattern="%m%n%ex{none}"/> + </Servlet> + </Appenders> + + <Loggers> + <Root level="debug"> + <AppenderRef ref="Servlet"/> + </Root> + </Loggers> + </Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ed3c803f/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 036e633..7e69b6d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -156,6 +156,9 @@ <action issue="LOG4J2-1573" dev="ggregory" type="fix" due-to="Steffen Offermann"> Layout is no longer optional. </action> + <action issue="LOG4J2-1608" dev="ggregory" type="fix"> + ServletAppender does not provide throwable object to ServletContext. + </action> <action issue="LOG4J2-1599" dev="ggregory" type="fix"> Prevent potential NPE in org.apache.logging.log4j.message.ParameterFormatter.formatMessage3(StringBuilder, char[], int, Object[], int, int[]). </action> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ed3c803f/src/site/xdoc/manual/webapp.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/webapp.xml b/src/site/xdoc/manual/webapp.xml index 57fea45..ade3236 100644 --- a/src/site/xdoc/manual/webapp.xml +++ b/src/site/xdoc/manual/webapp.xml @@ -419,6 +419,32 @@ public class TestAsyncServlet extends HttpServlet { context in a <code>finally</code> block, which will always execute. </p> </subsection> + <subsection name="Using the Servlet Appender"> + <p> + Log4j provides a Servlet Appender that uses the servlet context as the log target. For example: + </p> + <pre class="prettyprint linenums"><![CDATA[ +<Configuration status="WARN" name="ServletTest"> + + <Appenders> + <Servlet name="Servlet"> + <PatternLayout pattern="%m%n%ex{none}"/> + </Servlet> + </Appenders> + + <Loggers> + <Root level="debug"> + <AppenderRef ref="Servlet"/> + </Root> + </Loggers> + +</Configuration>]]></pre> + <p> + To avoid double logging of exceptions to the servlet context, you must use <code>%ex{none}</code> in your + <code>PatternLayout</code> as shown in the example. The exception will be omitted from the message text but + it is passed to the servlet context as the actual Throwable object. + </p> + </subsection> </section> </body>