| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <!-- |
| 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. |
| --> |
| <!-- Generated by Apache Maven Doxia at Aug 29, 2014 --> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| <title> |
| Log4j 2 Lookups - Apache Log4j 2</title> |
| <link rel="stylesheet" href="../css/bootstrap.min.css" type="text/css" /> |
| <link rel="stylesheet" href="../css/site.css" type="text/css" /> |
| <script type="text/javascript" src="../js/jquery.min.js"></script> |
| <script type="text/javascript" src="../js/bootstrap.min.js"></script> |
| <script type="text/javascript" src="../js/prettify.min.js"></script> |
| <script type="text/javascript" src="../js/site.js"></script> |
| <meta name="author" content="Ralph Goers" /> |
| <meta name="Date-Revision-yyyymmdd" content="20140829" /> |
| <meta http-equiv="Content-Language" content="en" /> |
| |
| </head> |
| <body class="composite"> |
| <img class="logo-left" src="../images/ls-logo.jpg" alt="Apache logging services logo" /> |
| <img class="logo-right" src="../images/logo.jpg" alt="Apache log4j logo" /> |
| <div class="clear"></div> |
| |
| <div class="navbar"> |
| <div class="navbar-inner"> |
| <div class="container-fluid"> |
| <a class="brand" href="http://logging.apache.org/log4j/2.x/">Apache Log4j 2 ™</a> |
| <ul class="nav"> |
| <li> |
| |
| |
| <a href="http://wiki.apache.org/logging" class="external" target="_blank" title="Logging Wiki">Logging Wiki</a> |
| </li> |
| <li> |
| |
| |
| <a href="http://www.apache.org/" class="external" target="_blank" title="Apache">Apache</a> |
| </li> |
| <li> |
| <a href="../../../" title="Logging Services">Logging Services</a> |
| </li> |
| <li> |
| |
| |
| <a href="https://analysis.apache.org/dashboard/index/org.apache.logging.log4j:log4j" class="external" target="_blank" title="Sonar">Sonar</a> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| |
| <div class="container-fluid"> |
| <table class="layout-table"> |
| <tr> |
| <td class="sidebar"> |
| <div class="well sidebar-nav"> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><i class="icon-home"></i>Apache Log4j™ 2</li> |
| <li class="none"> |
| <a href="../index.html" title="About">About</a> |
| </li> |
| <li class="none"> |
| <a href="../download.html" title="Download">Download</a> |
| </li> |
| <li class="none"> |
| <a href="../maven-artifacts.html" title="Maven and Ivy">Maven and Ivy</a> |
| </li> |
| <li class="none"> |
| <a href="../build.html" title="Build">Build</a> |
| </li> |
| <li class="none"> |
| <a href="../guidelines.html" title="Guidelines">Guidelines</a> |
| </li> |
| <li class="none"> |
| <a href="../javastyle.html" title="Style Guide">Style Guide</a> |
| </li> |
| <li class="none"> |
| <a href="../changelog.html" title="Changelog">Changelog</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../javadoc.html" title="Javadoc">Javadoc</a> |
| </li> |
| <li class="none"> |
| <a href="../faq.html" title="FAQ">FAQ</a> |
| </li> |
| </ul> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><i class="icon-book"></i>Manual</li> |
| <li class="none"> |
| <a href="../manual/index.html" title="Introduction">Introduction</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/architecture.html" title="Architecture">Architecture</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/migration.html" title="Log4j 1.x Migration">Log4j 1.x Migration</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/api.html" title="API">API</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/configuration.html" title="Configuration">Configuration</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/webapp.html" title="Web Applications and JSPs">Web Applications and JSPs</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/plugins.html" title="Plugins">Plugins</a> |
| </li> |
| <li class="expanded active"> |
| <a href="../manual/lookups.html" title="Lookups">Lookups</a> |
| <ul> |
| <li class="none"> |
| <a href="../manual/lookups.html#ContextMapLookup" title="Context Map">Context Map</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#DateLookcup" title="Date">Date</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#EnvironmentLookup" title="Environment">Environment</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#JNDILookup" title="JNDI">JNDI</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#MapLookup" title="Map">Map</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#StructuredDataLookup" title="Structured Data">Structured Data</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#SystemPropertiesLookup" title="System Properties">System Properties</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/lookups.html#WebLookup" title="Web">Web</a> |
| </li> |
| </ul> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/appenders.html" title="Appenders">Appenders</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/layouts.html" title="Layouts">Layouts</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/filters.html" title="Filters">Filters</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/async.html" title="Async Loggers">Async Loggers</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/jmx.html" title="JMX">JMX</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/logsep.html" title="Logging Separation">Logging Separation</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/extending.html" title="Extending Log4j">Extending Log4j</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/customconfig.html" title="Extending Log4j Configuration">Extending Log4j Configuration</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/customloglevels.html" title="Custom Log Levels">Custom Log Levels</a> |
| </li> |
| </ul> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><i class="icon-cog"></i>Components</li> |
| <li class="none"> |
| <a href="../log4j-api/index.html" title="API">API</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-core/index.html" title="Implementation">Implementation</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-jcl/index.html" title="Commons Logging Bridge">Commons Logging Bridge</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-1.2-api/index.html" title="Log4j 1.2 API">Log4j 1.2 API</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-slf4j-impl/index.html" title="SLF4J Binding">SLF4J Binding</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-to-slf4j/index.html" title="Log4j 2 to SLF4J Adapter">Log4j 2 to SLF4J Adapter</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-flume-ng/index.html" title="Apache Flume NG Appender">Apache Flume NG Appender</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-taglib/index.html" title="Log4j Tag Library">Log4j Tag Library</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-jmx-gui/index.html" title="Log4j JMX GUI">Log4j JMX GUI</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-web/index.html" title="Log4j Web Application Support">Log4j Web Application Support</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-nosql/index.html" title="Log4j NoSQL support">Log4j NoSQL support</a> |
| </li> |
| </ul> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><i class="icon-info-sign"></i>Project Information</li> |
| <li class="none"> |
| <a href="../dependencies.html" title="Dependencies">Dependencies</a> |
| </li> |
| <li class="none"> |
| <a href="../dependency-convergence.html" title="Dependency Convergence">Dependency Convergence</a> |
| </li> |
| <li class="none"> |
| <a href="../dependency-management.html" title="Dependency Management">Dependency Management</a> |
| </li> |
| <li class="none"> |
| <a href="../team-list.html" title="Project Team">Project Team</a> |
| </li> |
| <li class="none"> |
| <a href="../mail-lists.html" title="Mailing Lists">Mailing Lists</a> |
| </li> |
| <li class="none"> |
| <a href="../issue-tracking.html" title="Issue Tracking">Issue Tracking</a> |
| </li> |
| <li class="none"> |
| <a href="../license.html" title="Project License">Project License</a> |
| </li> |
| <li class="none"> |
| <a href="../source-repository.html" title="Source Repository">Source Repository</a> |
| </li> |
| <li class="none"> |
| <a href="../project-summary.html" title="Project Summary">Project Summary</a> |
| </li> |
| </ul> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><i class="icon-cog"></i>Project Reports</li> |
| <li class="none"> |
| <a href="../changes-report.html" title="Changes Report">Changes Report</a> |
| </li> |
| <li class="none"> |
| <a href="../jira-report.html" title="JIRA Report">JIRA Report</a> |
| </li> |
| <li class="none"> |
| <a href="../surefire-report.html" title="Surefire Report">Surefire Report</a> |
| </li> |
| <li class="none"> |
| <a href="../rat-report.html" title="RAT Report">RAT Report</a> |
| </li> |
| </ul> |
| </div> |
| <div id="poweredBy"> |
| <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"> |
| <img class="poweredBy" alt="Built by Maven" src="../images/maven-feather.png" /> |
| </a> |
| </div> |
| </td> |
| <td class="content"> |
| <!-- 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. --> |
| |
| <div class="section"><h2>Lookups<a name="Lookups"></a></h2> |
| <p> |
| Lookups provide a way to add values to the Log4j configuration at arbitrary places. They are |
| a particular type of Plugin that implements the |
| <a href="../log4j-core/apidocs/org/apache/logging/log4j/core/lookup/StrLookup.html">StrLookup</a> interface. |
| Information on how to use Lookups in configuration files can be found in the |
| <a href="./configuration.html#PropertySubstitution">Property Substitution</a> section of the |
| <a href="./configuration.html">Configuration</a> page. |
| </p> |
| <div class="section"><h3>ContextMapLookup<a name="ContextMapLookup"></a></h3> |
| <a name="ContextMapLookup"></a> |
| <p> |
| The ContextMapLookup allows applications to store data in the Log4j ThreadContext Map and |
| then retrieve the values in the Log4j configuration. In the example below, the application |
| would store the current user's login id in the ThreadContext Map with the key "loginId". During |
| initial configuration processing the first '$' will be removed. The PatternLayout supports |
| interpolation with Lookups and will then resolve the variable for each event. Note that |
| the pattern "%X{loginId}" would achieve the same result. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${ctx:loginId} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </div> |
| <div class="section"><h3>DateLookup<a name="DateLookup"></a></h3> |
| <a name="DateLookup"></a> |
| <p> |
| The DateLookup is somewhat unusual from the other lookups as it doesn't use the key to locate an item. |
| Instead, the key can be used to specify a date format string that is valid for |
| <a class="externalLink" href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a>. |
| The current date, or the date associated with the current log event will be formatted as specified. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <RollingFile name="Rolling-${map:type}" fileName="${filename}" filePattern="target/rolling1/test1-$${date:MM-dd-yyyy}.%i.log.gz"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] %m%n</pattern> |
| </PatternLayout> |
| <SizeBasedTriggeringPolicy size="500" /> |
| </RollingFile></pre></div> |
| </div> |
| <div class="section"><h3>EnvironmentLookup<a name="EnvironmentLookup"></a></h3> |
| <a name="EnvironmentLookup"></a> |
| <p> |
| The EnvironmentLookup allows systems to configure environment variables, either in global files |
| such as /etc/profile or in the startup scripts for applications, and then retrieve those variables |
| from within the logging configuration. The example below includes the name of the currently logged |
| in user in the application log. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${env:USER} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </div> |
| <div class="section"><h3>JNDILookup<a name="JNDILookup"></a></h3> |
| <a name="JNDILookup"></a> |
| <p> |
| The JNDILookup allows variables to be retrieved via JNDI. By default the key will be prefixed with |
| java:comp/env/, however if the key contains a ":" no prefix will be added. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${jndi:logging/context-name} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </div> |
| <div class="section"><h3>MapLookup<a name="MapLookup"></a></h3> |
| <a name="MapLookup"></a> |
| <p> |
| The MapLookup serves two purposes. |
| </p> |
| <ol style="list-style-type: decimal"> |
| <li>Provide the base for Properties declared in the configuration file.</li> |
| <li>Retrieve values from MapMessages in LogEvents.</li> |
| </ol> |
| <p> |
| The first item simply means that the MapLookup is used to substitute properties that are defined |
| in the configuration file. These variables are specified without a prefix - e.g. <tt>${name}</tt>. |
| The second usage allows a value from the current |
| <a href="../log4j-api/apidocs/org/apache/logging/log4j/message/MapMessage.html">MapMessage</a>, |
| if one is part of the current log event, to be substituted. In the example below the RoutingAppender will |
| use a different RollingFileAppender for each unique value of the key named "type" in the MapMessage. Note |
| that when used this way a value for "type" should be declared in the properties declaration to provide |
| a default value in case the message is not a MapMessage or the MapMessage does not contain the key. See the |
| <a href="./configuration.html#PropertySubstitution">Property Substitution</a> section of the |
| <a href="./configuration.html">Configuration</a> page for information on how to set the default values. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <Routing name="Routing"> |
| <Routes pattern="$${map:type}"> |
| <Route> |
| <RollingFile name="Rolling-${map:type}" fileName="${filename}" |
| filePattern="target/rolling1/test1-${map:type}.%i.log.gz"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] %m%n</pattern> |
| </PatternLayout> |
| <SizeBasedTriggeringPolicy size="500" /> |
| </RollingFile> |
| </Route> |
| </Routes> |
| </Routing></pre></div> |
| </div> |
| <div class="section"><h3>StructuredDataLookup<a name="StructuredDataLookup"></a></h3> |
| <a name="StructuredDataLookup"></a> |
| <p> |
| The StructuredDataLookup is very similar to the MapLookup in that it will retrieve values from |
| StructuredDataMessages. In addition to the Map values it will also return the name portion of the |
| id (not including the enterprise number) and the type field. The main difference between the |
| example below and the example for MapMessage is that the "type" is an attribute of the |
| <a href="../log4j-api/apidocs/org/apache/logging/log4j/message/StructuredDataMessage.html">StructuredDataMessage</a> |
| while "type" would have to be an item in the Map in a MapMessage. |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <Routing name="Routing"> |
| <Routes pattern="$${sd:type}"> |
| <Route> |
| <RollingFile name="Rolling-${sd:type}" fileName="${filename}" |
| filePattern="target/rolling1/test1-${sd:type}.%i.log.gz"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] %m%n</pattern> |
| </PatternLayout> |
| <SizeBasedTriggeringPolicy size="500" /> |
| </RollingFile> |
| </Route> |
| </Routes> |
| </Routing></pre></div> |
| </div> |
| <div class="section"><h3>SystemPropertiesLookup<a name="SystemPropertiesLookup"></a></h3> |
| <a name="SystemPropertiesLookup"></a> |
| <p> |
| As it is quite common to define values inside and outside the application by using System Properties, |
| it is only natural that they should be accessible via a Lookup. As system properties are often |
| defined outside the application it would be quite common to see something like: |
| </p> |
| <div class="prettyprint linenums"><pre> |
| <Appenders> |
| <File name="ApplicationLog" fileName="${sys:logPath}/app.log"/> |
| </Appenders></pre></div> |
| </div> |
| <div class="section"><h3>WebLookup<a name="WebLookup"></a></h3> |
| <a name="WebLookup"></a> |
| <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 border="0" class="bodyTable"> |
| <tr class="a"> |
| <th>Key</th> |
| <th>Description</th> |
| </tr> |
| <tr class="b"> |
| <td>attr.<i>name</i></td> |
| <td>Returns the ServletContext attribute with the specified name</td> |
| </tr> |
| <tr class="a"> |
| <td>contextPath</td> |
| <td>The context path of the web application</td> |
| </tr> |
| <tr class="b"> |
| <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 class="a"> |
| <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 class="b"> |
| <td>initParam.<i>name</i></td> |
| <td>Returns the ServletContext initialization parameter with the specified name</td> |
| </tr> |
| <tr class="a"> |
| <td>majorVersion</td> |
| <td>Returns the major version of the Servlet API that this servlet container supports.</td> |
| </tr> |
| <tr class="b"> |
| <td>minorVersion</td> |
| <td>Returns the minor version of the Servlet API that this servlet container supports.</td> |
| </tr> |
| <tr class="a"> |
| <td>rootDir</td> |
| <td>Returns the result of calling getRealPath with a value of "/".</td> |
| </tr> |
| <tr class="b"> |
| <td>serverInfo</td> |
| <td>Returns the name and version of the servlet container on which the servlet is running.</td> |
| </tr> |
| <tr class="a"> |
| <td>servletContextName</td> |
| <td>Returns the name of the web application as defined in the display-name element of the |
| deployment descriptor</td> |
| </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> |
| <div class="prettyprint linenums"><pre> |
| <Appenders> |
| <File name="ApplicationLog" fileName="${web:rootDir}/app.log"/> |
| </Appenders></pre></div> |
| </div> |
| |
| </div> |
| |
| |
| </td> |
| </tr> |
| </table> |
| </div> |
| |
| <div class="footer"> |
| <p>Copyright © 1999-2014 <a class="external" href="http://www.apache.org">Apache Software Foundation</a>. All Rights Reserved.</p> |
| <p>Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the Apache Logging project logo are trademarks of The Apache Software Foundation.</p> |
| <p>Site powered by <a class="external" href="http://getbootstrap.com/">Twitter Bootstrap</a>. Icons from <a class="external" href="http://glyphicons.com/">Glyphicons Free</a>.</p> |
| </div> |
| </div> |
| </body> |
| </html> |