LookupsLookups provide a way to add values to the Log4j configuration at arbitrary places. They are a particular type of Plugin that implements the StrLookup interface. Information on how to use Lookups in configuration files can be found in the Property Substitution section of the Configuration page. Context Map LookupThe 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. <File name="Application" fileName="application.log"> <PatternLayout> <pattern>%d %p %c{1.} [%t] $${ctx:loginId} %m%n</pattern> </PatternLayout> </File> Date LookupThe 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 SimpleDateFormat. The current date, or the date associated with the current log event will be formatted as specified. <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> Environment LookupThe 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. <File name="Application" fileName="application.log"> <PatternLayout> <pattern>%d %p %c{1.} [%t] $${env:USER} %m%n</pattern> </PatternLayout> </File> Java LookupThe JavaLookup allows Java environment information to be retrieved in convenient preformatted strings using the java: prefix.
For example: <File name="Application" fileName="application.log"> <PatternLayout header="${java:runtime} - ${java:vm} - ${java:os}"> <Pattern>%d %m%n</Pattern> </PatternLayout> </File> Jndi LookupAs of Log4j 2.13.1 JNDI operations require that log4j2.enableJndiLookup=true be set as a system property or the corresponding environment variable for this lookup to function. See the enableJndiLookup system property. 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. <File name="Application" fileName="application.log"> <PatternLayout> <pattern>%d %p %c{1.} [%t] $${jndi:logging/context-name} %m%n</pattern> </PatternLayout> </File> Java's JNDI module is not available on Android. JVM Input Arguments Lookup (JMX)Maps JVM input arguments -- but not main arguments -- using JMX to acquire the JVM arguments. Use the prefix jvmrunargs to access JVM arguments. See the Javadocs for java.lang.management.RuntimeMXBean.getInputArguments() . Java's JMX module is not available on Android. Main Arguments Lookup (Application)This lookup requires that you manually provide the main arguments of the application to Log4j: import org.apache.logging.log4j.core.lookup.MapLookup; public static void main(String args[]) { MapLookup.setMainArguments(args); ... } 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. For example, suppose the static void main String[] arguments are: --file foo.txt --verbose -x bar Then the following substitutions are possible:
Example usage: <File name="Application" fileName="application.log"> <PatternLayout header="File: ${main:--file}"> <Pattern>%d %m%n</Pattern> </PatternLayout> </File> Map LookupThe MapLookup serves several purposes.
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 MapMessage, 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 Property Substitution section of the Configuration page for information on how to set the default values. <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> Structured Data LookupThe 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 StructuredDataMessage while "type" would have to be an item in the Map in a MapMessage. <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> System Properties LookupAs 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: <Appenders> <File name="ApplicationLog" fileName="${sys:logPath}/app.log"/> </Appenders> Web LookupThe 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:
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. <Appenders> <File name="ApplicationLog" fileName="${web:rootDir}/app.log"/> </Appenders> |