| <!DOCTYPE html> |
| <!-- |
| | Generated by Apache Maven Doxia Site Renderer 1.9.1 from src/site/xdoc/manual/lookups.xml at 2019-12-11 |
| | Rendered using Apache Maven Fluido Skin 1.8 |
| --> |
| <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| <meta name="generator" content="Apache Maven Doxia Site Renderer 1.9.1" /> |
| <meta name="author" content="Ralph Goers" /> |
| <title>Log4j – Log4j 2 Lookups</title> |
| <link rel="stylesheet" href="../css/apache-maven-fluido-1.8.min.css" /> |
| <link rel="stylesheet" href="../css/site.css" /> |
| <link rel="stylesheet" href="../css/print.css" media="print" /> |
| <script src="../js/apache-maven-fluido-1.8.min.js"></script> |
| </head> |
| <body class="topBarDisabled"> |
| <div class="container-fluid"> |
| <header> |
| <div id="banner"> |
| <div class="pull-left"><a href="http://logging.apache.org" id="bannerLeft"><img src="../images/ls-logo.jpg" alt=""/></a></div> |
| <div class="pull-right"><a href="http://logging.apache.org/log4j/2.x" id="bannerRight"><img src="../images/logo.png" alt=""/></a></div> |
| <div class="clear"><hr/></div> |
| </div> |
| |
| <div id="breadcrumbs"> |
| <ul class="breadcrumb"> |
| <li id="publishDate">Last Published: 2019-12-11<span class="divider">|</span> |
| </li> |
| <li id="projectVersion">Version: 2.13.0</li> |
| <li class="pull-right"><span class="divider">|</span> |
| <a href="https://github.com/apache/logging-log4j2" class="externalLink" title="GitHub">GitHub</a></li> |
| <li class="pull-right"><span class="divider">|</span> |
| <a href="https://analysis.apache.org/dashboard/index/org.apache.logging.log4j:log4j" class="externalLink" title="Sonar">Sonar</a></li> |
| <li class="pull-right"><span class="divider">|</span> |
| <a href="../../../" title="Logging Services">Logging Services</a></li> |
| <li class="pull-right"><span class="divider">|</span> |
| <a href="https://www.apache.org/" class="externalLink" title="Apache">Apache</a></li> |
| <li class="pull-right"><a href="https://cwiki.apache.org/confluence/display/LOGGING/Log4j" class="externalLink" title="Logging Wiki">Logging Wiki</a></li> |
| </ul> |
| </div> |
| </header> |
| <div class="row-fluid"> |
| <header id="leftColumn" class="span2"> |
| <nav class="well sidebar-nav"> |
| <ul class="nav nav-list"> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/home.png" alt="Apache Log4j™ 2" border="0"/> Apache Log4j™ 2</li> |
| <li><a href="../index.html" title="About"><span class="none"></span>About</a></li> |
| <li><a href="../download.html" title="Download"><span class="none"></span>Download</a></li> |
| <li><a href="../javadoc.html" title="Javadoc"><span class="icon-chevron-right"></span>Javadoc</a></li> |
| <li><a href="../maven-artifacts.html" title="Maven, Ivy, Gradle Artifacts"><span class="icon-chevron-right"></span>Maven, Ivy, Gradle Artifacts</a></li> |
| <li><a href="../runtime-dependencies.html" title="Runtime Dependencies"><span class="none"></span>Runtime Dependencies</a></li> |
| <li><a href="../changelog.html" title="Changelog"><span class="none"></span>Changelog</a></li> |
| <li><a href="../faq.html" title="FAQ"><span class="none"></span>FAQ</a></li> |
| <li><a href="../performance.html" title="Performance"><span class="icon-chevron-right"></span>Performance</a></li> |
| <li><a href="../articles.html" title="Articles and Tutorials"><span class="none"></span>Articles and Tutorials</a></li> |
| <li><a href="../thanks.html" title="Thanks"><span class="none"></span>Thanks</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/pencil.png" alt="For Contributors" border="0"/> For Contributors</li> |
| <li><a href="../build.html" title="Building Log4j from Source"><span class="none"></span>Building Log4j from Source</a></li> |
| <li><a href="../guidelines.html" title="Guidelines"><span class="none"></span>Guidelines</a></li> |
| <li><a href="../javastyle.html" title="Style Guide"><span class="none"></span>Style Guide</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/book.png" alt="Manual" border="0"/> Manual</li> |
| <li><a href="../manual/index.html" title="Introduction"><span class="none"></span>Introduction</a></li> |
| <li><a href="../manual/architecture.html" title="Architecture"><span class="none"></span>Architecture</a></li> |
| <li><a href="../manual/compatibility.html" title="Log4j 1.x Compatibility"><span class="none"></span>Log4j 1.x Compatibility</a></li> |
| <li><a href="../manual/migration.html" title="Log4j 1.x Migration"><span class="none"></span>Log4j 1.x Migration</a></li> |
| <li><a href="../manual/api.html" title="Java API"><span class="icon-chevron-right"></span>Java API</a></li> |
| <li><a href="../manual/scala-api.html" title="Scala API"><span class="none"></span>Scala API</a></li> |
| <li><a href="../manual/configuration.html" title="Configuration"><span class="icon-chevron-right"></span>Configuration</a></li> |
| <li><a href="../manual/usage.html" title="Usage"><span class="icon-chevron-right"></span>Usage</a></li> |
| <li><a href="../manual/webapp.html" title="Web Applications and JSPs"><span class="icon-chevron-right"></span>Web Applications and JSPs</a></li> |
| <li class="active"><a href="#"><span class="icon-chevron-down"></span>Lookups</a> |
| <ul class="nav nav-list"> |
| <li><a href="../manual/lookups.html#ContextMapLookup" title="Context Map"><span class="none"></span>Context Map</a></li> |
| <li><a href="../manual/lookups.html#DateLookup" title="Date"><span class="none"></span>Date</a></li> |
| <li><a href="../manual/lookups.html#DockerLookup" title="Docker"><span class="none"></span>Docker</a></li> |
| <li><a href="../manual/lookups.html#EnvironmentLookup" title="Environment"><span class="none"></span>Environment</a></li> |
| <li><a href="../manual/lookups.html#JavaLookup" title="Java"><span class="none"></span>Java</a></li> |
| <li><a href="../manual/lookups.html#JndiLookup" title="JNDI"><span class="none"></span>JNDI</a></li> |
| <li><a href="../manual/lookups.html#JmxRuntimeInputArgumentsLookup" title="JVM Arguments"><span class="none"></span>JVM Arguments</a></li> |
| <li><a href="../manual/lookups.html#Log4jConfigLookup" title="Log4j Config"><span class="none"></span>Log4j Config</a></li> |
| <li><a href="../manual/lookups.html#AppMainArgsLookup" title="Main Arguments"><span class="none"></span>Main Arguments</a></li> |
| <li><a href="../manual/lookups.html#MapLookup" title="Map"><span class="none"></span>Map</a></li> |
| <li><a href="../manual/lookups.html#StructuredDataLookup" title="Structured Data"><span class="none"></span>Structured Data</a></li> |
| <li><a href="../manual/lookups.html#SystemPropertiesLookup" title="System Properties"><span class="none"></span>System Properties</a></li> |
| <li><a href="../manual/lookups.html#WebLookup" title="Web"><span class="none"></span>Web</a></li> |
| </ul></li> |
| <li><a href="../manual/appenders.html" title="Appenders"><span class="icon-chevron-right"></span>Appenders</a></li> |
| <li><a href="../manual/layouts.html" title="Layouts"><span class="icon-chevron-right"></span>Layouts</a></li> |
| <li><a href="../manual/filters.html" title="Filters"><span class="icon-chevron-right"></span>Filters</a></li> |
| <li><a href="../manual/async.html" title="Async Loggers"><span class="icon-chevron-right"></span>Async Loggers</a></li> |
| <li><a href="../manual/garbagefree.html" title="Garbage-free Logging"><span class="icon-chevron-right"></span>Garbage-free Logging</a></li> |
| <li><a href="../manual/jmx.html" title="JMX"><span class="none"></span>JMX</a></li> |
| <li><a href="../manual/logsep.html" title="Logging Separation"><span class="none"></span>Logging Separation</a></li> |
| <li><a href="../manual/extending.html" title="Extending Log4j"><span class="icon-chevron-right"></span>Extending Log4j</a></li> |
| <li><a href="../manual/plugins.html" title="Plugins"><span class="icon-chevron-right"></span>Plugins</a></li> |
| <li><a href="../manual/customconfig.html" title="Programmatic Log4j Configuration"><span class="icon-chevron-right"></span>Programmatic Log4j Configuration</a></li> |
| <li><a href="../manual/customloglevels.html" title="Custom Log Levels"><span class="icon-chevron-right"></span>Custom Log Levels</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/tag.png" alt="Related Projects" border="0"/> Related Projects</li> |
| <li><a href="http://logging.apache.org/log4j/scala/index.html" class="externalLink" title="Log4j-Scala"><span class="none"></span>Log4j-Scala</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/link.png" alt="Legacy Sites" border="0"/> Legacy Sites</li> |
| <li><a href="http://logging.apache.org/log4j/1.2/" class="externalLink" title="Log4j 1.2 - End of Life"><span class="none"></span>Log4j 1.2 - End of Life</a></li> |
| <li><a href="http://logging.apache.org/log4j/log4j-2.3/" class="externalLink" title="Log4j 2.3 - Java 6"><span class="none"></span>Log4j 2.3 - Java 6</a></li> |
| <li><a href="http://logging.apache.org/log4j/log4j-2.12.1" class="externalLink" title="Log4j 2.12.1 - Java 7"><span class="none"></span>Log4j 2.12.1 - Java 7</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/cog.png" alt="Components" border="0"/> Components</li> |
| <li><a href="../log4j-api/index.html" title="API"><span class="none"></span>API</a></li> |
| <li><a href="../log4j-core/index.html" title="Implementation"><span class="none"></span>Implementation</a></li> |
| <li><a href="../log4j-jcl/index.html" title="Commons Logging Bridge"><span class="none"></span>Commons Logging Bridge</a></li> |
| <li><a href="../log4j-1.2-api/index.html" title="Log4j 1.2 API"><span class="none"></span>Log4j 1.2 API</a></li> |
| <li><a href="../log4j-slf4j-impl/index.html" title="SLF4J Binding"><span class="none"></span>SLF4J Binding</a></li> |
| <li><a href="../log4j-jul/index.html" title="JUL Adapter"><span class="none"></span>JUL Adapter</a></li> |
| <li><a href="../log4j-to-slf4j/index.html" title="Log4j 2 to SLF4J Adapter"><span class="none"></span>Log4j 2 to SLF4J Adapter</a></li> |
| <li><a href="../log4j-flume-ng/index.html" title="Apache Flume Appender"><span class="none"></span>Apache Flume Appender</a></li> |
| <li><a href="../log4j-taglib/index.html" title="Log4j Tag Library"><span class="none"></span>Log4j Tag Library</a></li> |
| <li><a href="../log4j-jmx-gui/index.html" title="Log4j JMX GUI"><span class="none"></span>Log4j JMX GUI</a></li> |
| <li><a href="../log4j-web/index.html" title="Log4j Web Application Support"><span class="none"></span>Log4j Web Application Support</a></li> |
| <li><a href="../log4j-appserver/index.html" title="Log4j Application Server Integration"><span class="none"></span>Log4j Application Server Integration</a></li> |
| <li><a href="../log4j-couchdb/index.html" title="Log4j CouchDB appender"><span class="none"></span>Log4j CouchDB appender</a></li> |
| <li><a href="../log4j-mongodb2/index.html" title="Log4j MongoDB2 appender"><span class="none"></span>Log4j MongoDB2 appender</a></li> |
| <li><a href="../log4j-mongodb3/index.html" title="Log4j MongoDB3 appender"><span class="none"></span>Log4j MongoDB3 appender</a></li> |
| <li><a href="../log4j-cassandra/index.html" title="Log4j Cassandra appender"><span class="none"></span>Log4j Cassandra appender</a></li> |
| <li><a href="../log4j-iostreams/index.html" title="Log4j IO Streams"><span class="none"></span>Log4j IO Streams</a></li> |
| <li><a href="../log4j-liquibase/index.html" title="Log4j Liquibase Binding"><span class="none"></span>Log4j Liquibase Binding</a></li> |
| <li><a href="../log4j-docker/index.html" title="Log4j Docker Support"><span class="none"></span>Log4j Docker Support</a></li> |
| <li><a href="../log4j-spring-cloud-config/log4j-spring-cloud-config-client/index.html" title="Log4j Spring Cloud Config Client"><span class="none"></span>Log4j Spring Cloud Config Client</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/info.png" alt="Project Information" border="0"/> Project Information</li> |
| <li><a href="../dependency-convergence.html" title="Dependency Convergence"><span class="none"></span>Dependency Convergence</a></li> |
| <li><a href="../dependency-management.html" title="Dependency Management"><span class="none"></span>Dependency Management</a></li> |
| <li><a href="../team-list.html" title="Project Team"><span class="none"></span>Project Team</a></li> |
| <li><a href="../mail-lists.html" title="Mailing Lists"><span class="none"></span>Mailing Lists</a></li> |
| <li><a href="../issue-tracking.html" title="Issue Tracking"><span class="none"></span>Issue Tracking</a></li> |
| <li><a href="../license.html" title="Project License"><span class="none"></span>Project License</a></li> |
| <li><a href="../source-repository.html" title="Source Repository"><span class="none"></span>Source Repository</a></li> |
| <li><a href="../project-summary.html" title="Project Summary"><span class="none"></span>Project Summary</a></li> |
| <li class="nav-header"><img class="imageLink" src="../img/glyphicons/layers.png" alt="Project Reports" border="0"/> Project Reports</li> |
| <li><a href="../changes-report.html" title="Changes Report"><span class="none"></span>Changes Report</a></li> |
| <li><a href="../jira-report.html" title="JIRA Report"><span class="none"></span>JIRA Report</a></li> |
| <li><a href="../rat-report.html" title="RAT Report"><span class="none"></span>RAT Report</a></li> |
| </ul> |
| </nav> |
| <div class="well sidebar-nav"> |
| <hr /> |
| <div id="poweredBy"> |
| <div class="clear"></div> |
| <div class="clear"></div> |
| <div class="clear"></div> |
| <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a> |
| </div> |
| </div> |
| </header> |
| <main id="bodyColumn" class="span10" > |
| |
| |
| <section> |
| <h2><a name="Lookups"></a>Lookups</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> |
| <a name="ContextMapLookup"></a> |
| <section> |
| <h3><a name="Context_Map_Lookup"></a>Context Map Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${ctx:loginId} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="DateLookup"></a> |
| <section> |
| <h3><a name="Date_Lookup"></a>Date Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <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> |
| </section> |
| <a name="DockerLookup"></a> |
| <section> |
| <h3><a name="Docker_Lookup"></a>Docker Lookup</h3> |
| |
| <p> |
| The DockerLookup can be used to lookup attributes from the Docker container the application is running |
| in. |
| </p> |
| Log4j Docker provides access to the following container attributes: |
| |
| <table border="0" class="table table-striped"> |
| |
| <tr class="a"> |
| <td>containerId</td> |
| <td>The full id assigned to the container.</td></tr> |
| |
| <tr class="b"> |
| <td>containerName</td> |
| <td>The name assigned to the container.</td></tr> |
| |
| <tr class="a"> |
| <td>imageId</td> |
| <td>The id assigned to the image.</td></tr> |
| |
| <tr class="b"> |
| <td>imageName</td> |
| <td>The name assigned to the image.</td></tr> |
| |
| <tr class="a"> |
| <td>shortContainerId</td> |
| <td>The first 12 characters of the container id.</td></tr> |
| |
| <tr class="b"> |
| <td>shortImageId</td> |
| <td>The first 12 characters of the image id.</td></tr> |
| </table> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <JsonLayout properties="true" compact="true" eventEol="true"> |
| <KeyValuePair key="containerId" value="${docker:containerId}"/> |
| <KeyValuePair key="containerName" value="${docker:containerName}"/> |
| <KeyValuePair key="imageName" value="${docker:imageName}"/> |
| </JsonLayout></pre></div> |
| |
| <p> |
| This Lookup is subject to the requirements listed at <a href="../log4j-docker/index.html">Log4j Docker Support</a> |
| </p> |
| </section> |
| <a name="EnvironmentLookup"></a> |
| <section> |
| <h3><a name="Environment_Lookup"></a>Environment Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${env:USER} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| |
| <p> |
| This lookup also supports default value syntax. In the sample below, when the USER environment |
| variable is undefined, the default value jdoe is used: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${env:USER:-jdoe} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="JavaLookup"></a> |
| <section> |
| <h3><a name="Java_Lookup"></a>Java Lookup</h3> |
| |
| <p> |
| The JavaLookup allows Java environment information to be retrieved in convenient preformatted strings |
| using the java: prefix. |
| </p> |
| |
| <table border="0" class="table table-striped"> |
| |
| <tr class="a"> |
| |
| <th>Key</th> |
| |
| <th>Description</th> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>version</td> |
| |
| <td> |
| |
| <p>The short Java version, like:</p> |
| |
| <p>Java version 1.7.0_67</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>runtime</td> |
| |
| <td> |
| |
| <p>The Java runtime version, like:</p> |
| |
| <p>Java(TM) SE Runtime Environment (build 1.7.0_67-b01) from Oracle Corporation</p> |
| </td> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>vm</td> |
| |
| <td> |
| |
| <p>The Java VM version, like:</p> |
| |
| <p>Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>os</td> |
| |
| <td> |
| |
| <p>The OS version, like:</p> |
| |
| <p>Windows 7 6.1 Service Pack 1, architecture: amd64-64</p> |
| </td> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>locale</td> |
| |
| <td> |
| |
| <p>Hardware information, like:</p> |
| |
| <p>default locale: en_US, platform encoding: Cp1252</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>hw</td> |
| |
| <td> |
| |
| <p>Hardware information, like:</p> |
| |
| <p>processors: 4, architecture: amd64-64, instruction sets: amd64</p> |
| </td> |
| </tr> |
| </table> |
| |
| <p> |
| For example: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout header="${java:runtime} - ${java:vm} - ${java:os}"> |
| <Pattern>%d %m%n</Pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="JndiLookup"></a> |
| <section> |
| <h3><a name="Jndi_Lookup"></a>Jndi Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${jndi:logging/context-name} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| |
| <p><b>Java's JNDI module is not available on Android.</b></p> |
| </section> |
| <a name="JmxRuntimeInputArgumentsLookup"></a> |
| <section> |
| <h3><a name="JVM_Input_Arguments_Lookup_.28JMX.29"></a>JVM Input Arguments Lookup (JMX)</h3> |
| |
| <p> |
| Maps JVM input arguments -- but not <i>main</i> arguments -- using JMX to acquire the JVM arguments. |
| </p> |
| |
| <p> |
| Use the prefix jvmrunargs to access JVM arguments. |
| </p> |
| |
| <p> |
| See the Javadocs for |
| <a class="externalLink" href="http://docs.oracle.com/javase/8/docs/api/java/lang/management/RuntimeMXBean.html#getInputArguments--"> |
| java.lang.management.RuntimeMXBean.getInputArguments() |
| </a>. |
| </p> |
| |
| <p><b>Java's JMX module is not available on Android or on Google App Engine.</b></p> |
| </section> |
| <a name="KubernetesLookup"></a> |
| <section> |
| <h3><a name="Kubernetes_Lookup"></a>Kubernetes Lookup</h3> |
| |
| <p> |
| The KubernetesLookup can be used to lookup attributes from the Kubernetes environment for the container |
| the application is running in. |
| </p> |
| Log4j Kubernetes provides access to the following container attributes: |
| |
| <table border="0" class="table table-striped"> |
| |
| <tr class="a"> |
| <td>accountName</td> |
| <td>The service account name</td></tr> |
| |
| <tr class="b"> |
| <td>clusterName</td> |
| <td>The name of the cluster the application is deployed in</td></tr> |
| |
| <tr class="a"> |
| <td>containerId</td> |
| <td>The full id assigned to the container</td></tr> |
| |
| <tr class="b"> |
| <td>containerName</td> |
| <td>The name assigned to the container</td></tr> |
| |
| <tr class="a"> |
| <td>host</td> |
| <td>The name assigned to the host operating system</td></tr> |
| |
| <tr class="b"> |
| <td>hostIp</td> |
| <td>The host's ip address</td></tr> |
| |
| <tr class="a"> |
| <td>imageId</td> |
| <td>The id assigned to the container image</td></tr> |
| |
| <tr class="b"> |
| <td>imageName</td> |
| <td>The name assigned to the container image</td></tr> |
| |
| <tr class="a"> |
| <td>labels</td> |
| <td>All labels formatted in a list</td></tr> |
| |
| <tr class="b"> |
| <td>labesl.app</td> |
| <td>The application name</td></tr> |
| |
| <tr class="a"> |
| <td>labels.podTemplateHash</td> |
| <td>The pod's template hash value</td></tr> |
| |
| <tr class="b"> |
| <td>masterUrl</td> |
| <td>The URL used to access the API server</td></tr> |
| |
| <tr class="a"> |
| <td>namespaceId</td> |
| <td>The id of the namespace the various kubernetes components are located within</td></tr> |
| |
| <tr class="b"> |
| <td>namespaceName</td> |
| <td>The namespace the various kubernetes components are located within</td></tr> |
| |
| <tr class="a"> |
| <td>podId</td> |
| <td>The pod's ip number</td></tr> |
| |
| <tr class="b"> |
| <td>podIp</td> |
| <td>The pod's ip address</td></tr> |
| |
| <tr class="a"> |
| <td>podName</td> |
| <td>The name of the pod</td></tr> |
| </table> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <GelfLayout includeStackTrace="true" host="${hostName}" includeThreadContext="true" includeNullDelimiter="true" |
| compressionType="OFF"> |
| <ThreadContextIncludes>requestId,sessionId,loginId,userId,ipAddress,callingHost</ThreadContextIncludes> |
| <MessagePattern>%d [%t] %-5p %X{requestId, sessionId, loginId, userId, ipAddress} %C{1.}.%M:%L - %m%n</MessagePattern> |
| <KeyValuePair key="docker.containerId" value="${docker:containerId:-}"/> |
| <KeyValuePair key="application" value="$${lower:${spring:spring.application.name}}"/> |
| <KeyValuePair key="kubernetes.serviceAccountName" value="${k8s:accountName:-}"/> |
| <KeyValuePair key="kubernetes.clusterName" value="${k8s:clusterName:-}/> |
| <KeyValuePair key="kubernetes.containerId" value="${k8s:containerId:-}"/> |
| <KeyValuePair key="kubernetes.containerName" value="${k8s:containerName:-}"/> |
| <KeyValuePair key="kubernetes.host" value="${k8s:host:-}"/> |
| <KeyValuePair key="kubernetes.labels.app" value="${k8s:labels.app:-}"/> |
| <KeyValuePair key="kubernetes.labels.pod-template-hash" value="${k8s:labels.podTemplateHash:-}"/> |
| <KeyValuePair key="kubernetes.master_url" value="${k8s:masterUrl:-}"/> |
| <KeyValuePair key="kubernetes.namespaceId" value="${k8s:namespaceId:-}"/> |
| <KeyValuePair key="kubernetes.namespaceName" value="${k8s:namespaceName:-}"/> |
| <KeyValuePair key="kubernetes.podID" value="${k8s:podId:-}"/> |
| <KeyValuePair key="kubernetes.podIP" value="${k8s:podIp:-}"/> |
| <KeyValuePair key="kubernetes.podName" value="${k8s:podName:-}"/> |
| <KeyValuePair key="kubernetes.imageId" value="${k8s:imageId:-}"/> |
| <KeyValuePair key="kubernetes.imageName" value="${k8s:imageName:-}"/> |
| </GelfLayout></pre></div> |
| |
| <p> |
| This Lookup is subject to the configuration requirements listed at <a href="../log4j-kubernetes/index.html">Log4j Kubernetes Support</a> |
| </p> |
| </section> |
| <a name="Log4jConfigLookup"></a> |
| <section> |
| <h3><a name="Log4j_Configuration_Location_Lookup"></a>Log4j Configuration Location Lookup</h3> |
| |
| <p> |
| Log4j configuration properties. The expressions |
| ${log4j:configLocation} and ${log4j:configParentLocation} |
| respectively provide the absolute path to the log4j configuration file |
| and its parent folder. |
| </p> |
| <p> |
| The example below uses this lookup to place log files in a directory relative |
| to the log4j configuration file. |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="${log4j:configParentLocation}/logs/application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="LowerLookup"></a> |
| <section> |
| <h3><a name="Lower_Lookup"></a>Lower Lookup</h3> |
| |
| <p> |
| The LowerLookup converts the passed in argument to lower case. Presumably the value will be the |
| result of a nested lookup. |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${lower:{${spring:spring.application.name}} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="AppMainArgsLookup"></a> |
| <section> |
| <h3><a name="Main_Arguments_Lookup_.28Application.29"></a>Main Arguments Lookup (Application)</h3> |
| |
| <p> |
| This lookup requires that you manually provide |
| the main arguments of the application to Log4j: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| import org.apache.logging.log4j.core.lookup.MainMapLookup; |
| |
| public static void main(String args[]) { |
| MainMapLookup.setMainArguments(args); |
| ... |
| }</pre></div> |
| |
| <p> |
| If the main arguments have been set, this lookup allows applications to retrieve |
| these main argument values from within the logging configuration. |
| The key that follows the main: prefix can either be a 0-based index into the argument list, |
| or a string, where ${main:myString} is substituted with the value that follows |
| myString in the main argument list. |
| </p> |
| |
| <p> |
| For example, suppose the static void main String[] arguments are: |
| </p> |
| |
| <div> |
| <pre>--file foo.txt --verbose -x bar</pre></div> |
| |
| <p> |
| Then the following substitutions are possible: |
| </p> |
| |
| <table border="0" class="table table-striped" style="width: 40%"> |
| |
| <tr class="a"> |
| |
| <th>Expression</th> |
| |
| <th>Result</th> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>${main:0}</td> |
| |
| <td> |
| |
| <p>--file</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>${main:1}</td> |
| |
| <td> |
| |
| <p>foo.txt</p> |
| </td> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>${main:2}</td> |
| |
| <td> |
| |
| <p>--verbose</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>${main:3}</td> |
| |
| <td> |
| |
| <p>-x</p> |
| </td> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>${main:4}</td> |
| |
| <td> |
| |
| <p>bar</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>${main:--file}</td> |
| |
| <td> |
| |
| <p>foo.txt</p> |
| </td> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>${main:-x}</td> |
| |
| <td> |
| |
| <p>bar</p> |
| </td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>${main:bar}</td> |
| |
| <td> |
| |
| <p>null</p> |
| </td> |
| </tr> |
| </table> |
| |
| <p> |
| Example usage: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout header="File: ${main:--file}"> |
| <Pattern>%d %m%n</Pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| <a name="MapLookup"></a> |
| <section> |
| <h3><a name="Map_Lookup"></a>Map Lookup</h3> |
| |
| <p> |
| The MapLookup serves several 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> |
| |
| <li>Retrieve values set with <a href="../log4j-core/apidocs/org/apache/logging/log4j/core/lookup/MapLookup.html#setMainArguments28java.lang.String29">MapLookup.setMainArguments(String[])</a></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. ${name}. |
| 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> |
| <pre class="prettyprint linenums"> |
| <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> |
| </section> |
| <section> |
| <h3><a name="Marker_Lookup"></a>Marker Lookup</h3> |
| |
| <p> |
| The marker lookup allows you to use markers in interesting configurations like a routing appender. |
| Consider the following YAML configuration and code that logs to different files based on markers: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| Configuration: |
| status: debug |
| |
| Appenders: |
| Console: |
| RandomAccessFile: |
| - name: SQL_APPENDER |
| fileName: logs/sql.log |
| PatternLayout: |
| Pattern: "%d{ISO8601_BASIC} %-5level %logger{1} %X %msg%n" |
| - name: PAYLOAD_APPENDER |
| fileName: logs/payload.log |
| PatternLayout: |
| Pattern: "%d{ISO8601_BASIC} %-5level %logger{1} %X %msg%n" |
| - name: PERFORMANCE_APPENDER |
| fileName: logs/performance.log |
| PatternLayout: |
| Pattern: "%d{ISO8601_BASIC} %-5level %logger{1} %X %msg%n" |
| |
| Routing: |
| name: ROUTING_APPENDER |
| Routes: |
| pattern: "$${marker:}" |
| Route: |
| - key: PERFORMANCE |
| ref: PERFORMANCE_APPENDER |
| - key: PAYLOAD |
| ref: PAYLOAD_APPENDER |
| - key: SQL |
| ref: SQL_APPENDER |
| |
| Loggers: |
| Root: |
| level: trace |
| AppenderRef: |
| - ref: ROUTING_APPENDER</pre></div> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| public static final Marker SQL = MarkerFactory.getMarker("SQL"); |
| public static final Marker PAYLOAD = MarkerFactory.getMarker("PAYLOAD"); |
| public static final Marker PERFORMANCE = MarkerFactory.getMarker("PERFORMANCE"); |
| |
| final Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); |
| |
| logger.info(SQL, "Message in Sql.log"); |
| logger.info(PAYLOAD, "Message in Payload.log"); |
| logger.info(PERFORMANCE, "Message in Performance.log");</pre></div> |
| |
| <p> |
| Note the key part of the configuration is pattern: "$${marker:}". This will produce three log files, |
| each with a log event for a specific marker. Log4j will route the log event with the SQL marker to |
| sql.log, the log event with the PAYLOAD marker to payload.log, and so on. |
| </p> |
| |
| <p> |
| You can use the notation "${marker:name}" and "$${marker:name}" to check for the |
| existence of a marker where name is the marker name. If the marker exists, the expression returns |
| the name, otherwise null. |
| </p> |
| </section> |
| <a name="SpringLookup"></a> |
| <section> |
| <h3><a name="Spring_Boot_Lookup"></a>Spring Boot Lookup</h3> |
| |
| <p> |
| The Spring Boot Lookup retrieves the values of Spring properties from the Spring configuration. This Lookup |
| will return null values until Spring Boot initializes application logging. |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $${spring:spring.application.name} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| |
| <p>This Lookup requires log4j-spring-cloud-config-client be included in the application.</p> |
| </section> |
| <a name="StructuredDataLookup"></a> |
| <section> |
| <h3><a name="Structured_Data_Lookup"></a>Structured Data Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <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> |
| </section> |
| <a name="SystemPropertiesLookup"></a> |
| <section> |
| <h3><a name="System_Properties_Lookup"></a>System Properties Lookup</h3> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <Appenders> |
| <File name="ApplicationLog" fileName="${sys:logPath}/app.log"/> |
| </Appenders></pre></div> |
| |
| <p> |
| This lookup also supports default value syntax. In the sample below, when the logPath system |
| property is undefined, the default value /var/logs is used: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <Appenders> |
| <File name="ApplicationLog" fileName="${sys:logPath:-/var/logs}/app.log"/> |
| </Appenders></pre></div> |
| </section> |
| <a name="WebLookup"></a> |
| <section> |
| <h3><a name="Web_Lookup"></a>Web Lookup</h3> |
| |
| <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="table table-striped"> |
| |
| <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> |
| <pre class="prettyprint linenums"> |
| <Appenders> |
| <File name="ApplicationLog" fileName="${web:rootDir}/app.log"/> |
| </Appenders></pre></div> |
| </section> |
| <a name="UpperLookup"></a> |
| <section> |
| <h3><a name="Upper_Lookup"></a>Upper Lookup</h3> |
| |
| <p> |
| The UpperLookup converts the passed in argument to upper case. Presumably the value will be the |
| result of a nested lookup. |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <File name="Application" fileName="application.log"> |
| <PatternLayout> |
| <pattern>%d %p %c{1.} [%t] $$upper{${spring:spring.application.name}} %m%n</pattern> |
| </PatternLayout> |
| </File></pre></div> |
| </section> |
| </section> |
| |
| |
| </main> |
| </div> |
| </div> |
| <hr/> |
| <footer> |
| <div class="container-fluid"> |
| <div class="row-fluid"> |
| <p align="center">Copyright © 1999-2019 <a class="external" href="http://www.apache.org">The Apache Software Foundation</a>. All Rights Reserved.<br> |
| Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the Apache Logging project logo are trademarks of The Apache Software Foundation.</p> |
| </div> |
| </div> |
| </footer> |
| </body> |
| </html> |