logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1517403 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java src/site/xdoc/manual/lookups.xml
Date Mon, 26 Aug 2013 00:59:39 GMT
Author: rgoers
Date: Mon Aug 26 00:59:38 2013
New Revision: 1517403

URL: http://svn.apache.org/r1517403
Log:
LOG4J2-364 - Add WebLookup

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java
Modified:
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/lookups.xml

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java?rev=1517403&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/WebLookup.java
Mon Aug 26 00:59:38 2013
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+package org.apache.logging.log4j.core.lookup;
+
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
+import org.apache.logging.log4j.core.lookup.StrLookup;
+
+import javax.servlet.ServletContext;
+
+
+@Plugin(name = "web", category = "Lookup")
+public class WebLookup implements StrLookup {
+    private static final String ATTR_PREFIX = "attr.";
+    private static final String INIT_PARAM_PREFIX = "initParam.";
+
+    protected ServletContext getServletContext() {
+        LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
+        if (lc == null) {
+            lc = (LoggerContext) LogManager.getContext(false);
+        }
+        if (lc != null) {
+            Object obj = lc.getExternalContext();
+            return obj != null && obj instanceof ServletContext ? (ServletContext)
obj : null;
+        }
+        return null;
+    }
+
+    @Override
+    public String lookup(String key) {
+        ServletContext ctx = getServletContext();
+        if (ctx == null) {
+            return null;
+        }
+
+        if (key.startsWith(ATTR_PREFIX)) {
+            String attrName = key.substring(ATTR_PREFIX.length());
+            Object attrValue = ctx.getAttribute(attrName);
+            return attrValue == null ? null : attrValue.toString();
+        }
+
+        if (key.startsWith(INIT_PARAM_PREFIX)) {
+            String paramName = key.substring(INIT_PARAM_PREFIX.length());
+            return ctx.getInitParameter(paramName);
+        }
+
+        if ("rootDir".equals(key)) {
+            String root = ctx.getRealPath("/");
+            if (root == null) {
+                String msg = "failed to resolve web:rootDir -- " +
+                        "servlet container unable to translate virtual path " +
+                        " to real path (probably not deployed as exploded";
+                throw new RuntimeException(msg);
+
+            }
+            return root;
+        }
+
+        if ("contextPath".equals(key)) {
+            return ctx.getContextPath();
+        }
+
+        if ("servletContextName".equals(key)) {
+            return ctx.getServletContextName();
+        }
+
+        if ("serverInfo".equals(key)) {
+            return ctx.getServerInfo();
+        }
+
+        if ("effectiveMajorVersion".equals(key)) {
+            return String.valueOf(ctx.getEffectiveMajorVersion());
+        }
+
+        if ("effectiveMinorVersion".equals(key)) {
+            return String.valueOf(ctx.getEffectiveMinorVersion());
+        }
+
+        if ("majorVersion".equals(key)) {
+            return String.valueOf(ctx.getMajorVersion());
+        }
+
+        if ("minorVersion".equals(key)) {
+            return String.valueOf(ctx.getMinorVersion());
+        }
+
+        if (ctx.getAttribute(key) != null) {
+            return ctx.getAttribute(key).toString();
+        }
+
+        if (ctx.getInitParameter(key) != null) {
+            return ctx.getInitParameter(key);
+        }
+
+        ctx.log(getClass().getName() + " unable to resolve key '" + key + "'");
+        return null;
+    }
+
+    @Override
+    public String lookup(LogEvent event, String key) {
+        return lookup(key);
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/lookups.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/lookups.xml?rev=1517403&r1=1517402&r2=1517403&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/lookups.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/lookups.xml Mon Aug 26 00:59:38 2013
@@ -164,6 +164,76 @@
   <File name="ApplicationLog" fileName="${sys:logPath}/app.log"/>
 </Appenders>]]></pre>
         </subsection>
+        <a name="WebLookup"/>
+        <subsection name="WebLookup">
+          <p>
+            The WebLookup allows applications to retrieve variables that are associated with
the ServletContext.
+            In addition to being able to retrieve various fields in the ServletContext, WebLookup
supports looking
+            up values stored as attributes or configured as initialization parameters. The
following table lists
+            various keys that can be retrieved:
+          </p>
+          <table>
+            <tr>
+              <th>Key</th>
+              <th>Description</th>
+            </tr>
+            <tr>
+              <td>attr.<i>name</i></td>
+              <td>Returns the ServletContext attribute with the specified name</td>
+            </tr>
+            <tr>
+              <td>contextPath</td>
+              <td>The context path of the web application</td>
+            </tr>
+            <tr>
+              <td>effectiveMajorVersion</td>
+              <td>Gets the major version of the Servlet specification that the application
represented by this
+                ServletContext is based on.</td>
+            </tr>
+            <tr>
+              <td>effectiveMinorVersion</td>
+              <td>Gets the minor version of the Servlet specification that the application
represented by this
+                ServletContext is based on.</td>
+            </tr>
+            <tr>
+              <td>initParam.<i>name</i></td>
+              <td>Returns the ServletContext initialization parameter with the specified
name</td>
+            </tr>
+            <tr>
+              <td>majorVersion</td>
+              <td>Returns the major version of the Servlet API that this servlet container
supports.</td>
+            </tr>
+            <tr>
+              <td>minorVersion</td>
+              <td>Returns the minor version of the Servlet API that this servlet container
supports.</td>
+            </tr>
+            <tr>
+              <td>rootDir</td>
+              <td>Returns the result of calling getRealPath with a value of "/".</td>
+            </tr>
+            <tr>
+              <td>serverInfo</td>
+              <td>Returns the name and version of the servlet container on which the
servlet is running.</td>
+            </tr>
+            <tr>
+              <td>servletContextName</td>
+              <td>Returns the name of the web application as defined in the display-name
element of the
+                deployment descriptor</td>
+            </tr>
+            <tr>
+
+            </tr>
+
+          </table>
+          <p>Any other key names specified will first be checked to see if a ServletContext
attribute exists with
+            that name and then will be checked to see if an initialization parameter of that
name exists. If the
+            key is located then the corresponding value will be returned.</p>
+          <pre class="prettyprint linenums"><![CDATA[
+<Appenders>
+  <File name="ApplicationLog" fileName="${web:rootDir}/app.log"/>
+</Appenders>]]></pre>
+        </subsection>
+
       </section>
     </body>
 </document>



Mime
View raw message