Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

ndInserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately 4 percent of code is dedicated to logging. Consequently, even moderately sized applications will have thousands of logging statements embedded within their code. Given their number, it becomes imperative to manage these log statements without the need to modify them manually.

Configuration of Log4j 2 can be accomplished in 1 of 4 ways:

  1. Through a configuration file written in XML, JSON, or YAML.
  2. Programmatically, by creating a ConfigurationFactory and Configuration implementation.
  3. Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
  4. Programmatically, by calling methods on the internal Logger class.

This page focuses primarily on configuring Log4j through a configuration file. Information on programmatically configuring Log4j can be found at Extending Log4j 2.

Note that unlike Log4j 1.x, the public Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way.

 

Automatic Configuration

Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange then in weighted order from highest to lowest. As delivered, Log4j contains three ConfigurationFactory implementations: one for JSON, one for YAML, and one for XML.

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
  2. If no system property is set the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  3. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  4. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  5. If a test file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  6. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  7. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  8. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

An example application named MyApp that uses log4j can be used to illustrate how this is done.

  1. import com.foo.Bar;
  2.  
  3. // Import log4j classes.
  4. import org.apache.logging.log4j.Logger;
  5. import org.apache.logging.log4j.LogManager;
  6.  
  7. public class MyApp {
  8.  
  9.  // Define a static logger variable so that it references the
  10.  // Logger instance named "MyApp".
  11.  private static final Logger logger = LogManager.getLogger(MyApp.class);
  12.  
  13.  public static void main(final String... args) {
  14.  
  15.  // Set up a simple configuration that logs on the console.
  16.  
  17. logger.trace("Entering application.");
  18.  Bar bar = new Bar();
  19.  if (!bar.doIt()) {
  20. logger.error("Didn't do it.");
  21.  }
  22. logger.trace("Exiting application.");
  23.  }
  24. }

MyApp begins by importing log4j related classes. It then defines a static logger variable with the name MyApp which happens to be the fully qualified name of the class.

MyApp uses the Bar class defined in the packagecom.foo.

  1. package com.foo;
  2. import org.apache.logging.log4j.Logger;
  3. import org.apache.logging.log4j.LogManager;
  4.  
  5. public class Bar {
  6.  static final Logger logger = LogManager.getLogger(Bar.class.getName());
  7.  
  8.  public boolean doIt() {
  9. logger.entry();
  10. logger.error("Did it again!");
  11.  return logger.exit(false);
  12.  }
  13. }

Log4j will provide a default configuration if it cannot locate a configuration file. The default configuration, provided in the DefaultConfiguration class, will set up:

  • ConsoleAppender attached to the root logger.
  • PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppender

Note that by default Log4j assigns the root logger to Level.ERROR.

The output of MyApp would be similar to:

17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] ERROR MyApp - Didn't do it.

As was described previously, Log4j will first attempt to configure itself from configuration files. A configuration equivalent to the default would look like:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN">
  3.  <Appenders>
  4.  <Console name="Console" target="SYSTEM_OUT">
  5.  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6.  </Console>
  7.  </Appenders>
  8.  <Loggers>
  9.  <Root level="error">
  10.  <AppenderRef ref="Console"/>
  11.  </Root>
  12.  </Loggers>
  13. </Configuration>

Once the file above is placed into the classpath as log4j2.xml you will get results identical to those listed above. Changing the root level to trace will result in results similar to:

17:13:01.540 [main] TRACE MyApp - Entering application.
17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] TRACE com.foo.Bar - exit with (false)
17:13:01.540 [main] ERROR MyApp - Didn't do it.
17:13:01.540 [main] TRACE MyApp - Exiting application.

Note that status logging is disabled when the default configuration is used.

Perhaps it is desired to eliminate all the TRACE output from everything except com.foo.Bar. Simply changing the log level would not accomplish the task. Instead, the solution is to add a new logger definition to the configuration:

  1. <Logger name="com.foo.Bar" level="TRACE"/>
  2. <Root level="ERROR">
  3.  <AppenderRef ref="STDOUT">
  4. </Root>

With this configuration all log events from com.foo.Bar will be recorded while only error events will be recorded from all other components.

 

Additivity

In the previous example all the events from com.foo.Bar were still written to the Console. This is because the logger for com.foo.Bar did not have any appenders configured while its parent did. In fact, the following configuration

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN">
  3.  <Appenders>
  4.  <Console name="Console" target="SYSTEM_OUT">
  5.  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6.  </Console>
  7.  </Appenders>
  8.  <Loggers>
  9.  <Logger name="com.foo.Bar" level="trace">
  10.  <AppenderRef ref="Console"/>
  11.  </Logger>
  12.  <Root level="error">
  13.  <AppenderRef ref="Console"/>
  14.  </Root>
  15.  </Loggers>
  16. </Configuration>

would result in

17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] TRACE com.foo.Bar - exit (false)
17:13:01.540 [main] TRACE com.foo.Bar - exit (false)
17:13:01.540 [main] ERROR MyApp - Didn't do it.

Notice that the trace messages from com.foo.Bar appear twice. This is because the appender associated with logger com.foo.Bar is first used, which writes the first instance to the Console. Next, the parent of com.foo.Bar, which in this case is the root logger, is referenced. The event is then passed to its appender, which is also writes to the Console, resulting in the second instance. This is known as additivity. While additivity can be quite a convenient feature (as in the first previous example where no appender reference needed to be configured), in many cases this behavior is considered undesirable and so it is possible to disable it by setting the additivity attribute on the logger to false:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN">
  3.  <Appenders>
  4.  <Console name="Console" target="SYSTEM_OUT">
  5.  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6.  </Console>
  7.  </Appenders>
  8.  <Loggers>
  9.  <Logger name="com.foo.Bar" level="trace" additivity="false">
  10.  <AppenderRef ref="Console"/>
  11.  </Logger>
  12.  <Root level="error">
  13.  <AppenderRef ref="Console"/>
  14.  </Root>
  15.  </Loggers>
  16. </Configuration>

Once an event reaches a logger with its additivity set to false the event will not be passed to any of its parent loggers, regardless of their additivity setting.

 

Automatic Reconfiguration

When configured from a File, Log4j has the ability to automatically detect changes to the configuration file and reconfigure itself. If the monitorInterval attribute is specified on the configuration element and is set to a non-zero value then the file will be checked the next time a log event is evaluated and/or logged and the monitorInterval has elapsed since the last check. The example below shows how to configure the attribute so that the configuration file will be checked for changes only after at least 30 seconds have elapsed. The minimum interval is 5 seconds.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration monitorInterval="30">
  3. ...
  4. </Configuration>

 

Chainsaw can automatically process your log files (Advertising appender configurations)

Log4j provides the ability to 'advertise' appender configuration details for all file-based appenders as well as socket-based appenders. For example, for file-based appenders, the file location and the pattern layout in the file are included in the advertisement. Chainsaw and other external systems can discover these advertisements and use that information to intelligently process the log file.

The mechanism by which an advertisement is exposed, as well as the advertisement format, is specific to each Advertiser implementation. An external system which would like to work with a specific Advertiser implementation must understand how to locate the advertised configuration as well as the format of the advertisement. For example, a 'database' Advertiser may store configuration details in a database table. An external system can read that database table in order to discover the file location and the file format.

Log4j provides one Advertiser implementation, a 'multicastdns' Advertiser, which advertises appender configuration details via IP multicast using the http://jmdns.sourceforge.net library.

Chainsaw automatically discovers log4j's multicastdns-generated advertisements and displays those discovered advertisements in Chainsaw's Zeroconf tab (if the jmdns library is in Chainsaw's classpath). To begin parsing and tailing a log file provided in an advertisement, just double-click the advertised entry in Chainsaw's Zeroconf tab. Currently, Chainsaw only supports FileAppender advertisements.

To advertise an appender configuration:

  • Add the JmDns library from http://jmdns.sourceforge.net to the application classpath
  • Set the 'advertiser' attribute of the configuration element to 'multicastdns'
  • Set the 'advertise' attribute on the appender element to 'true'
  • If advertising a FileAppender-based configuration, set the 'advertiseURI' attribute on the appender element to an appropriate URI

FileAppender-based configurations require an additional 'advertiseURI' attribute to be specified on the appender. The 'advertiseURI' attribute provides Chainsaw with information on how the file can be accessed. For example, the file may be remotely accessible to Chainsaw via ssh/sftp by specifying a Commons VFS (http://commons.apache.org/proper/commons-vfs/) sftp:// URI, an http:// URI may be used if the file is accessible through a web server, or a file:// URI can be specified if accessing the file from a locally-running instance of Chainsaw.

Here is an example advertisement-enabled appender configuration which can be used by a locally-running Chainsaw to automatically tail the log file (notice the file:// advertiseURI):

Please note, you must add the JmDns library from http://jmdns.sourceforge.net to your application classpath in order to advertise with the 'multicastdns' advertiser.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration advertiser="multicastdns">
  3. ...
  4. </Configuration>
  5. <Appenders>
  6.  <File name="File1" fileName="output.log" bufferedIO="false" advertiseURI="file://path/to/output.log" advertise="true">
  7. ...
  8.  </File>
  9. </Appenders>

 

Configuration Syntax

As the previous examples have shown as well as those to follow, Log4j allows you to easily redefine logging behavior without needing to modify your application. It is possible to disable logging for certain parts of the application, log only when specific criteria are met such as the action being performed for a specific user, route output to Flume or a log reporting system, etc. Being able to do this requires understanding the syntax of the configuration files.

Configuration with XML

The configuration element in the XML file accepts several attributes:

Attribute NameDescription
advertiser(Optional) The Advertiser plugin name which will be used to advertise individual FileAppender or SocketAppender configurations. The only Advertiser plugin provided is 'multicastdns".
destEither "err", which will send output to stderr, or a file path or URL.
monitorIntervalThe minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes.
nameThe name of the configuration.
packagesA comma separated list of package names to search for plugins. Plugins are only loaded once per classloader so changing this value may not have any effect upon reconfiguration.
schemaIdentifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema validation will take place.
shutdownHookSpecifies whether or not Log4j should automatically shutdown when the JVM shuts down. The shutdown hook is enabled by default but may be disabled by setting this attribute to "disable"
statusThe level of internal Log4j events that should be logged to the console. Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal". Log4j will log details about initialization, rollover and other internal actions to the status logger. Setting status="trace" is one of the first tools available to you if you need to troubleshoot log4j.
strictEnables the use of the strict XML format. Not supported in JSON configurations.
verboseEnables diagnostic information while loading plugins.

Log4j can be configured using two XML flavors; concise and strict. The concise format makes configuration very easy as the element names match the components they represent however it cannot be validated with an XML schema. For example, the ConsoleAppender is configured by declaring an XML element named Console under its parent appenders element. However, element and attribute names are are not case sensitive. In addition, attributes can either be specified as an XML attribute or as an XML element that has no attributes and has a text value. So

<PatternLayout pattern="%m%n"/>

and

<PatternLayout> <Pattern>%m%n</Pattern> </PatternLayout>

are equivalent.

The file below represents the structure of an XML configuration, but note that the elements in italics below represent the concise element names that would appear in their place.

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configuration>
  3.  <Properties>
  4.  <Property name="name1">value</property>
  5.  <Property name="name2" value="value2"/>
  6.  </Properties>
  7.  <filter ... />
  8.  <Appenders>
  9.  <appender ... >
  10.  <filter ... />
  11.  </appender>
  12. ...
  13.  </Appenders>
  14.  <Loggers>
  15.  <Logger name="name1">
  16.  <filter ... />
  17.  </Logger>
  18. ...
  19.  <Root level="level">
  20.  <AppenderRef ref="name"/>
  21.  </Root>
  22.  </Loggers>
  23. </Configuration>

See the many examples on this page for sample appender, filter and logger declarations.

Strict XML

In addition to the concise XML format above, Log4j allows configurations to be specified in a more "normal" XML manner that can be validated using an XML Schema. This is accomplished by replacing the friendly element names above with their object type as shown below. For example, instead of the ConsoleAppender being configuerd using an element named Console it is instead configured as an appender element with a type attribute containing "Console".

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configuration>
  3.  <Properties>
  4.  <Property name="name1">value</property>
  5.  <Property name="name2" value="value2"/>
  6.  </Properties>
  7.  <Filter type="type" ... />
  8.  <Appenders>
  9.  <Appender type="type" name="name">
  10.  <Filter type="type" ... />
  11.  </Appender>
  12. ...
  13.  </Appenders>
  14.  <Loggers>
  15.  <Logger name="name1">
  16.  <Filter type="type" ... />
  17.  </Logger>
  18. ...
  19.  <Root level="level">
  20.  <AppenderRef ref="name"/>
  21.  </Root>
  22.  </Loggers>
  23. </Configuration>

Below is a sample configuration using the strict format.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="debug" strict="true" name="XMLConfigTest"
  3.  packages="org.apache.logging.log4j.test">
  4.  <Properties>
  5.  <Property name="filename">target/test.log</Property>
  6.  </Properties>
  7.  <Filter type="ThresholdFilter" level="trace"/>
  8.  
  9.  <Appenders>
  10.  <Appender type="Console" name="STDOUT">
  11.  <Layout type="PatternLayout" pattern="%m MDC%X%n"/>
  12.  <Filters>
  13.  <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
  14.  <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/>
  15.  </Filters>
  16.  </Appender>
  17.  <Appender type="Console" name="FLOW">
  18.  <Layout type="PatternLayout" pattern="%C{1}.%M %m %ex%n"/><!-- class and line number -->
  19.  <Filters>
  20.  <Filter type="MarkerFilter" marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
  21.  <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="ACCEPT" onMismatch="DENY"/>
  22.  </Filters>
  23.  </Appender>
  24.  <Appender type="File" name="File" fileName="${filename}">
  25.  <Layout type="PatternLayout">
  26.  <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
  27.  </Layout>
  28.  </Appender>
  29.  <Appender type="List" name="List">
  30.  </Appender>
  31.  </Appenders>
  32.  
  33.  <Loggers>
  34.  <Logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
  35.  <Filter type="ThreadContextMapFilter">
  36.  <KeyValuePair key="test" value="123"/>
  37.  </Filter>
  38.  <AppenderRef ref="STDOUT"/>
  39.  </Logger>
  40.  
  41.  <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
  42.  <AppenderRef ref="File"/>
  43.  </Logger>
  44.  
  45.  <Root level="trace">
  46.  <AppenderRef ref="List"/>
  47.  </Root>
  48.  </Loggers>
  49.  
  50. </Configuration>

Configuration with JSON

In addition to XML, Log4j can be configured using JSON. The JSON format is very similar to the concise XML format. Each key represents the name of a plugin and the key/value pairs associated with it are its attributes. Where a key contains more than a simple value it itself will be a subordinate plugin. In the example below, ThresholdFilter, Console, and PatternLayout are all plugins while the Console plugin will be assigned a value of STDOUT for its name attribute and the ThresholdFilter will be assigned a level of debug.

  1. { "configuration": { "status": "error", "name": "RoutingTest",
  2.  "packages": "org.apache.logging.log4j.test",
  3.  "properties": {
  4.  "property": { "name": "filename",
  5.  "value" : "target/rolling1/rollingtest-$${sd:type}.log" }
  6.  },
  7.  "ThresholdFilter": { "level": "debug" },
  8.  "appenders": {
  9.  "Console": { "name": "STDOUT",
  10.  "PatternLayout": { "pattern": "%m%n" }
  11.  },
  12.  "List": { "name": "List",
  13.  "ThresholdFilter": { "level": "debug" }
  14.  },
  15.  "Routing": { "name": "Routing",
  16.  "Routes": { "pattern": "$${sd:type}",
  17.  "Route": [
  18.  {
  19.  "RollingFile": {
  20.  "name": "Rolling-${sd:type}", "fileName": "${filename}",
  21.  "filePattern": "target/rolling1/test1-${sd:type}.%i.log.gz",
  22.  "PatternLayout": {"pattern": "%d %p %c{1.} [%t] %m%n"},
  23.  "SizeBasedTriggeringPolicy": { "size": "500" }
  24.  }
  25.  },
  26.  { "AppenderRef": "STDOUT", "key": "Audit"},
  27.  { "AppenderRef": "List", "key": "Service"}
  28.  ]
  29.  }
  30.  }
  31.  },
  32.  "loggers": {
  33.  "logger": { "name": "EventLogger", "level": "info", "additivity": "false",
  34.  "AppenderRef": { "ref": "Routing" }},
  35.  "root": { "level": "error", "AppenderRef": { "ref": "STDOUT" }}
  36.  }
  37.  }
  38. }

Note that in the RoutingAppender the Route element has been declared as an array. This is valid because each array element will be a Route component. This won't work for elements such as appenders and filters, where each element has a different name in the concise format. Appenders and filters can be defined as array elements if each appender or filter declares an attribute named "type" that contains the type of the appender. The following example illustrates this as well as how to declare multiple loggers as an array.

  1. { "configuration": { "status": "debug", "name": "RoutingTest",
  2.  "packages": "org.apache.logging.log4j.test",
  3.  "properties": {
  4.  "property": { "name": "filename",
  5.  "value" : "target/rolling1/rollingtest-$${sd:type}.log" }
  6.  },
  7.  "ThresholdFilter": { "level": "debug" },
  8.  "appenders": {
  9.  "appender": [
  10.  { "type": "Console", "name": "STDOUT", "PatternLayout": { "pattern": "%m%n" }},
  11.  { "type": "List", "name": "List", "ThresholdFilter": { "level": "debug" }},
  12.  { "type": "Routing", "name": "Routing",
  13.  "Routes": { "pattern": "$${sd:type}",
  14.  "Route": [
  15.  {
  16.  "RollingFile": {
  17.  "name": "Rolling-${sd:type}", "fileName": "${filename}",
  18.  "filePattern": "target/rolling1/test1-${sd:type}.%i.log.gz",
  19.  "PatternLayout": {"pattern": "%d %p %c{1.} [%t] %m%n"},
  20.  "SizeBasedTriggeringPolicy": { "size": "500" }
  21.  }
  22.  },
  23.  { "AppenderRef": "STDOUT", "key": "Audit"},
  24.  { "AppenderRef": "List", "key": "Service"}
  25.  ]
  26.  }
  27.  }
  28.  ]
  29.  },
  30.  "loggers": {
  31.  "logger": [
  32.  { "name": "EventLogger", "level": "info", "additivity": "false",
  33.  "AppenderRef": { "ref": "Routing" }},
  34.  { "name": "com.foo.bar", "level": "error", "additivity": "false",
  35.  "AppenderRef": { "ref": "Console" }}
  36.  ],
  37.  "root": { "level": "error", "AppenderRef": { "ref": "STDOUT" }}
  38.  }
  39.  }
  40. }

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

  1. <dependency>
  2.  <groupId>com.fasterxml.jackson.core</groupId>
  3.  <artifactId>jackson-core</artifactId>
  4.  <version>2.5.1</version>
  5. </dependency>
  6.  
  7. <dependency>
  8.  <groupId>com.fasterxml.jackson.core</groupId>
  9.  <artifactId>jackson-databind</artifactId>
  10.  <version>2.5.1</version>
  11. </dependency>
  12.  
  13. <dependency>
  14.  <groupId>com.fasterxml.jackson.core</groupId>
  15.  <artifactId>jackson-annotations</artifactId>
  16.  <version>2.5.1</version>
  17. </dependency>

Configuring loggers

An understanding of how loggers work in Log4j is critical before trying to configure them. Please reference the Log4j architecture if more information is required. Trying to configure Log4j without understanding those concepts will lead to frustration.

A LoggerConfig is configured using the logger element. The logger element must have a name attribute specified, will usually have a level attribute specified and may also have an additivity attribute specified. The level may be configured with one of TRACE, DEBUG, INFO, WARN, ERROR, ALL or OFF. If no level is specified it will default to ERROR. The additivity attribute may be assigned a value of true or false. If the attribute is omitted the default value of false will be used.

A LoggerConfig (including the root LoggerConfig) can be configured with properties that will be added to the properties copied from the ThreadContextMap. These properties can be referenced from Appenders, Filters, Layouts, etc just as if they were part of the ThreadContext Map. The properties can contain variables that will be resolved either when the configuration is parsed or dynamically when each event is logged. See Property Substitution for more information on using variables.

The LoggerConfig may also be configured with one or more AppenderRef elements. Each appender referenced will become associated with the specified LoggerConfig. If multiple appenders are configured on the LoggerConfig each of them be called when processing logging events.

Every configuration must have a root logger. If one is not configured the default root LoggerConfig, which has a level of ERROR and has a Console appender attached, will be used. The main differences between the root logger and other loggers are

  1. The root logger does not have a name attribute.
  2. The root logger does not support the additivity attribute since it has no parent.

Configuring Appenders

An appender is configured either using the specific appender plugin's name or with an appender element and the type attibute containing the appender plugin's name. In addition each appender must have a name attribute specified with a value that is unique within the set of appenders. The name will be used by loggers to reference the appender as described in the previous section.

Most appenders also support a layout to be configured (which again may be specified either using the specific Layout plugin's name as the element or with "layout" as the element name along with a type attribute that contains the layout plugin's name. The various appenders will contain other attributes or elements that are required for them to function properly.

Configuring Filters

Log4j allows a filter to be specified in any of 4 places:

  1. At the same level as the appenders, loggers and properties elements. These filters can accept or reject events before they have been passed to a LoggerConfig.
  2. In a logger element. These filters can accept or reject events for specific loggers.
  3. In an appender element. These filters can prevent or cause events to be processed by the appender.
  4. In an appender reference element. These filters are used to determine if a Logger should route the event to an appender.

Although only a single filter element can be configured, that element may be the filters element which represents the CompositeFilter. The filters element allows any number of filterelements to be configured within it. The following example shows how multiple filters can be configured on the ConsoleAppender.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="debug" name="XMLConfigTest" packages="org.apache.logging.log4j.test">
  3.  <Properties>
  4.  <Property name="filename">target/test.log</Property>
  5.  </Properties>
  6.  <ThresholdFilter level="trace"/>
  7.  
  8.  <Appenders>
  9.  <Console name="STDOUT">
  10.  <PatternLayout pattern="%m MDC%X%n"/>
  11.  </Console>
  12.  <Console name="FLOW">
  13.  <!-- this pattern outputs class name and line number -->
  14.  <PatternLayout pattern="%C{1}.%M %m %ex%n"/>
  15.  <filters>
  16.  <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
  17.  <MarkerFilter marker="EXCEPTION" onMatch="ACCEPT" onMismatch="DENY"/>
  18.  </filters>
  19.  </Console>
  20.  <File name="File" fileName="${filename}">
  21.  <PatternLayout>
  22.  <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  23.  </PatternLayout>
  24.  </File>
  25.  <List name="List">
  26.  </List>
  27.  </Appenders>
  28.  
  29.  <Loggers>
  30.  <Logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
  31.  <ThreadContextMapFilter>
  32.  <KeyValuePair key="test" value="123"/>
  33.  </ThreadContextMapFilter>
  34.  <AppenderRef ref="STDOUT"/>
  35.  </Logger>
  36.  
  37.  <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
  38.  <Property name="user">${sys:user.name}</Property>
  39.  <AppenderRef ref="File">
  40.  <ThreadContextMapFilter>
  41.  <KeyValuePair key="test" value="123"/>
  42.  </ThreadContextMapFilter>
  43.  </AppenderRef>
  44.  <AppenderRef ref="STDOUT" level="error"/>
  45.  </Logger>
  46.  
  47.  <Root level="trace">
  48.  <AppenderRef ref="List"/>
  49.  </Root>
  50.  </Loggers>
  51.  
  52. </Configuration>

 

Property Substitution

Log4j 2 supports the ability to specify tokens in the configuration as references to properties defined elsewhere. Some of these properties will be resolved when the configuration file is interpreted while others may be passed to components where they will be evaluated at runtime. To accomplish this, Log4j uses variations of Apache Commons Lang's StrSubstitutor and StrLookup classes. In a manner similar to Ant or Maven, this allows variables declared as ${name} to be resolved using properties declared in the configuration itself. For example, the following example shows the filename for the rolling file appender being declared as a property.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="debug" name="RoutingTest" packages="org.apache.logging.log4j.test">
  3.  <Properties>
  4.  <Property name="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
  5.  </Properties>
  6.  <ThresholdFilter level="debug"/>
  7.  
  8.  <Appenders>
  9.  <Console name="STDOUT">
  10.  <PatternLayout pattern="%m%n"/>
  11.  </Console>
  12.  <List name="List">
  13.  <ThresholdFilter level="debug"/>
  14.  </List>
  15.  <Routing name="Routing">
  16.  <Routes pattern="$${sd:type}">
  17.  <Route>
  18.  <RollingFile name="Rolling-${sd:type}" fileName="${filename}"
  19.  filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
  20.  <PatternLayout>
  21.  <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  22.  </PatternLayout>
  23.  <SizeBasedTriggeringPolicy size="500" />
  24.  </RollingFile>
  25.  </Route>
  26.  <Route ref="STDOUT" key="Audit"/>
  27.  <Route ref="List" key="Service"/>
  28.  </Routes>
  29.  </Routing>
  30.  </Appenders>
  31.  
  32.  <Loggers>
  33.  <Logger name="EventLogger" level="info" additivity="false">
  34.  <AppenderRef ref="Routing"/>
  35.  </Logger>
  36.  
  37.  <Root level="error">
  38.  <AppenderRef ref="STDOUT"/>
  39.  </Root>
  40.  </Loggers>
  41.  
  42. </Configuration>

While this is useful, there are many more places properties can originate from. To accommodate this, Log4j also supports the syntax ${prefix:name} where the prefix identifies tells Log4j that variable name should be evaluated in a specific context. The contexts that are built in to Logj4 are:

PrefixContext
bundleResource bundle. The format is ${bundle:BundleName:BundleKey}. The bundle name follows package naming conventions, for example: ${bundle:com.domain.Messages:MyKey}.
ctxThread Context Map (MDC)
dateInserts the current date and/or time using the specified format
envSystem environment variables
jvmrunargsA JVM input argument accessed through JMX, but not a main argument; see RuntimeMXBean.getInputArguments(). Not available on Android.
mainA value set with MapLookup.setMainArguments(String[])
mapA value from a MapMessage
sdA value from a StructuredDataMessage. The key "id" will return the name of the StructuredDataId without the enterprise number. The key "type" will return the message type. Other keys will retrieve individual elements from the Map.
sysSystem properties

A default property map can be declared in the configuration file. If the value cannot be located in the specified lookup the value in the default property map will be used. The default map is pre-populated with a value for "hostName" that is the current system's host name or IP address and the "contextName" with is the value of the current logging context.

An interesting feature of StrLookup processing is that when a variable reference is declared with multiple leading '$' characters each time the variable is resolved the leading '$' is simply removed. In the previous example the "Routes" element is capable of resolving the variable at runtime. To allow this the prefix value is specified as a variable with two leading '$' characters. When the configuration file is first processed the first variable is simply removed. Thus, when the Routes element is evaluated at runtime it is the variable declaration "${sd:type}" which causes the event to be inspected for a StructuredDataMessage and if one is present the value of its type attribute to be used as the routing key. Not all elements support resolving variables at runtime. Components that do will specifically call that out in their documentation.

If no value is found for the key in the Lookup associated with the prefix then the value associated with the key in the properties declaration in the configuration file will be used. If no value is found the variable declaration will be returned as the value. Default values may be declared in the configuration by doing:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration>
  3.  <Properties>
  4.  <Property name="type">Audit</property>
  5.  </Properties>
  6. ...
  7. </Configuration>

As a footnote, it is worth pointing out that the variables in the RollingFile appender declaration will also not be evaluated when the configuration is processed. This is simply because the resolution of the whole RollingFile element is deferred until a match occurs. See RoutingAppender for more information.

 

XInclude

XML configuration files can include other files with XInclude. Here is an example log4j2.xml file that includes two other files:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration xmlns:xi="http://www.w3.org/2001/XInclude"
  3.  status="warn" name="XIncludeDemo">
  4.  <properties>
  5.  <property name="filename">xinclude-demo.log</property>
  6.  </properties>
  7.  <ThresholdFilter level="debug"/>
  8.  <xi:include href="log4j-xinclude-appenders.xml" />
  9.  <xi:include href="log4j-xinclude-loggers.xml" />
  10. </configuration>

log4j-xinclude-appenders.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <appenders>
  3.  <Console name="STDOUT">
  4.  <PatternLayout pattern="%m%n" />
  5.  </Console>
  6.  <File name="File" fileName="${filename}" bufferedIO="true" immediateFlush="true">
  7.  <PatternLayout>
  8.  <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  9.  </PatternLayout>
  10.  </File>
  11. </appenders>

log4j-xinclude-loggers.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <loggers>
  3.  <logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
  4.  <ThreadContextMapFilter>
  5.  <KeyValuePair key="test" value="123" />
  6.  </ThreadContextMapFilter>
  7.  <AppenderRef ref="STDOUT" />
  8.  </logger>
  9.  
  10.  <logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
  11.  <AppenderRef ref="File" />
  12.  </logger>
  13.  
  14.  <root level="error">
  15.  <AppenderRef ref="STDOUT" />
  16.  </root>
  17. </loggers>

 

Status Messages

Troubleshooting tip for the impatient:
  • Before a configuration is found, status logger level can be controlled with system property org.apache.logging.log4j.simplelog.StatusLogger.level.
  • After a configuration is found, status logger level can be controlled in the configuration file with the "status" attribute, for example: <Configuration status="trace">.

Just as it is desirable to be able to diagnose problems in applications, it is frequently necessary to be able to diagnose problems in the logging configuration or in the configured components. Since logging has not been configured, "normal" logging cannot be used during initialization. In addition, normal logging within appenders could create infinite recursion which Log4j will detect and cause the recursive events to be ignored. To accomodate this need, the Log4j 2 API includes a StatusLogger. Components declare an instance of the StatusLogger similar to:

protected final static Logger logger = StatusLogger.getLogger();

Since StatusLogger implements the Log4j 2 API's Logger interface, all the normal Logger methods may be used.

When configuring Log4j it is sometimes necessary to view the generated status events. This can be accomplished by adding the status attribute to the configuration element or a default value can be provided by setting the "Log4jDefaultStatusLevel" system property. Valid values of the status attribute are "trace", "debug", "info", "warn", "error" and "fatal". The following configuration has the status attribute set to debug.

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configuration status="debug" name="RoutingTest">
  3.  <Properties>
  4.  <Property name="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
  5.  </Properties>
  6.  <ThresholdFilter level="debug"/>
  7.  
  8.  <Appenders>
  9.  <Console name="STDOUT">
  10.  <PatternLayout pattern="%m%n"/>
  11.  </Console>
  12.  <List name="List">
  13.  <ThresholdFilter level="debug"/>
  14.  </List>
  15.  <Routing name="Routing">
  16.  <Routes pattern="$${sd:type}">
  17.  <Route>
  18.  <RollingFile name="Rolling-${sd:type}" fileName="${filename}"
  19.  filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
  20.  <PatternLayout>
  21.  <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  22.  </PatternLayout>
  23.  <SizeBasedTriggeringPolicy size="500" />
  24.  </RollingFile>
  25.  </Route>
  26.  <Route ref="STDOUT" key="Audit"/>
  27.  <Route ref="List" key="Service"/>
  28.  </Routes>
  29.  </Routing>
  30.  </Appenders>
  31.  
  32.  <Loggers>
  33.  <Logger name="EventLogger" level="info" additivity="false">
  34.  <AppenderRef ref="Routing"/>
  35.  </Logger>
  36.  
  37.  <Root level="error">
  38.  <AppenderRef ref="STDOUT"/>
  39.  </Root>
  40.  </Loggers>
  41.  
  42. </Configuration>

During startup this configuration produces:

2011-11-23 17:08:00,769 DEBUG Generated plugins in 0.003374000 seconds
2011-11-23 17:08:00,789 DEBUG Calling createProperty on class org.apache.logging.log4j.core.config.Property for element property with params(name="filename", value="target/rolling1/rollingtest-${sd:type}.log")
2011-11-23 17:08:00,792 DEBUG Calling configureSubstitutor on class org.apache.logging.log4j.core.config.PropertiesPlugin for element properties with params(properties={filename=target/rolling1/rollingtest-${sd:type}.log})
2011-11-23 17:08:00,794 DEBUG Generated plugins in 0.001362000 seconds
2011-11-23 17:08:00,797 DEBUG Calling createFilter on class org.apache.logging.log4j.core.filter.ThresholdFilter for element ThresholdFilter with params(level="debug", onMatch="null", onMismatch="null")
2011-11-23 17:08:00,800 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%m%n", Configuration(RoutingTest), null, charset="null")
2011-11-23 17:08:00,802 DEBUG Generated plugins in 0.001349000 seconds
2011-11-23 17:08:00,804 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%m%n), null, target="null", name="STDOUT", ignoreExceptions="null")
2011-11-23 17:08:00,804 DEBUG Calling createFilter on class org.apache.logging.log4j.core.filter.ThresholdFilter for element ThresholdFilter with params(level="debug", onMatch="null", onMismatch="null")
2011-11-23 17:08:00,806 DEBUG Calling createAppender on class org.apache.logging.log4j.test.appender.ListAppender for element List with params(name="List", entryPerNewLine="null", raw="null", null, ThresholdFilter(DEBUG))
2011-11-23 17:08:00,813 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="null", key="null", Node=Route)
2011-11-23 17:08:00,823 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="STDOUT", key="Audit", Node=Route)
2011-11-23 17:08:00,824 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="List", key="Service", Node=Route)
2011-11-23 17:08:00,825 DEBUG Calling createRoutes on class org.apache.logging.log4j.core.appender.routing.Routes for element Routes with params(pattern="${sd:type}", routes={Route(type=dynamic default), Route(type=static Reference=STDOUT key='Audit'), Route(type=static Reference=List key='Service')})
2011-11-23 17:08:00,827 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.routing.RoutingAppender for element Routing with params(name="Routing", ignoreExceptions="null", Routes({Route(type=dynamic default),Route(type=static Reference=STDOUT key='Audit'),Route(type=static Reference=List key='Service')}), Configuration(RoutingTest), null, null)
2011-11-23 17:08:00,827 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.AppendersPlugin for element appenders with params(appenders={STDOUT, List, Routing})
2011-11-23 17:08:00,828 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.plugins.AppenderRefPlugin for element AppenderRef with params(ref="Routing")
2011-11-23 17:08:00,829 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element logger with params(additivity="false", level="info", name="EventLogger", AppenderRef={Routing}, null)
2011-11-23 17:08:00,830 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.plugins.AppenderRefPlugin for element AppenderRef with params(ref="STDOUT")
2011-11-23 17:08:00,831 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element root with params(additivity="null", level="error", AppenderRef={STDOUT}, null)
2011-11-23 17:08:00,833 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element loggers with params(loggers={EventLogger, root})
2011-11-23 17:08:00,834 DEBUG Reconfiguration completed
2011-11-23 17:08:00,846 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d %p %c{1.} [%t] %m%n", Configuration(RoutingTest), null, charset="null")
2011-11-23 17:08:00,849 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy for element SizeBasedTriggeringPolicy with params(size="500")
2011-11-23 17:08:00,851 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile with params(fileName="target/rolling1/rollingtest-Unknown.log", filePattern="target/rolling1/test1-Unknown.%i.log.gz", append="null", name="Rolling-Unknown", bufferedIO="null", immediateFlush="null", SizeBasedTriggeringPolicy(SizeBasedTriggeringPolicy(size=500)), null, PatternLayout(%d %p %c{1.} [%t] %m%n), null, ignoreExceptions="null")
2011-11-23 17:08:00,858 DEBUG Generated plugins in 0.002014000 seconds
2011-11-23 17:08:00,889 DEBUG Reconfiguration started for context [email protected]
2011-11-23 17:08:00,890 DEBUG Generated plugins in 0.001355000 seconds
2011-11-23 17:08:00,959 DEBUG Generated plugins in 0.001239000 seconds
2011-11-23 17:08:00,961 DEBUG Generated plugins in 0.001197000 seconds
2011-11-23 17:08:00,965 WARN No Loggers were configured, using default
2011-11-23 17:08:00,976 DEBUG Reconfiguration completed

If the status attribute is set to error than only error messages will be written to the console. This makes troubleshooting configuration errors possible. As an example, if the configuration above is changed to have the status set to error and the logger declaration is:

  1. <logger name="EventLogger" level="info" additivity="false">
  2.  <AppenderRef ref="Routng"/>
  3. </logger>

the following error message will be produced.

2011-11-24 23:21:25,517 ERROR Unable to locate appender Routng for logger EventLogger

Applications may wish to direct the status output to some other destination. This can be accomplished by setting the dest attribute to either "err" to send the output to stderr or to a file location or URL. This can also be done by insuring the configured status is set to OFF and then configuring the application programmatically such as:

  1. StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR);
  2. StatusLogger.getLogger().registerListener(listener);

 

Testing in Maven

Maven can run unit and functional tests during the build cycle. By default, any files placed in src/test/resources are automatically copied to target/test-classes and are included in the classpath during execution of any tests. As such, placing a log4j2-test.xml into this directory will cause it to be used instead of a log4j2.xml or log4j2.json that might be present. Thus a different log configuration can be used during testing than what is used in production.

A second approach, which is extensively used by Log4j 2, is to set the log4j.configurationFile property in the method annotated with @BeforeClass in the junit test class. This will allow an arbitrarily named file to be used during the test.

A third approach, also used extensively by Log4j 2, is to use the InitialLoggerContext JUnit test rule which provides additional convenience methods for testing. This requires adding the log4j-coretest-jar dependency to your test scope dependencies. For example:

  1. public class AwesomeTest {
  2.  @Rule
  3.  public InitialLoggerContext init = new InitialLoggerContext("MyTestConfig.xml");
  4.  
  5.  @Test
  6.  public void testSomeAwesomeFeature() {
  7.  final LoggerContext ctx = init.getContext();
  8.  final Logger logger = init.getLogger("org.apache.logging.log4j.my.awesome.test.logger");
  9.  final Configuration cfg = init.getConfiguration();
  10.  final ListAppender app = init.getListAppender("List");
  11. logger.warn("Test message");
  12.  final List<LogEvent> events = app.getEvents();
  13.  // etc.
  14.  }
  15. }

 

System Properties

Below follows a number of system properties that can be used to control Log4j 2 behaviour. Any spaces present in the property name are for visual flow and should be removed.

Log4j 2 System Properties
System PropertyDefault ValueDescription
log4j.configurationFile Path to an XML or JSON Log4j 2 configuration file.
Log4jContextSelectorClassLoaderContextSelectorCreates the LoggerContexts. An application can have one or more active LoggerContexts depending on the circumstances. See Log Separation for more details. Available context selector implementation classes:
org.apache.logging.log4j.core.async .AsyncLoggerContextSelector - makes all loggers asynchronous.
org.apache.logging.log4j.core.selector .BasicContextSelector - creates a single shared LoggerContext.
org.apache.logging.log4j.core.selector .ClassLoaderContextSelector - separate LoggerContexts for each web application.
org.apache.logging.log4j.core.selector .JndiContextSelector - use JNDI to locate each web application's LoggerContext.
org.apache.logging.log4j.core.osgi .BundleContextSelector - separate LoggerContexts for each OSGi bundle.
Log4jLogEventFactoryorg.apache.logging.log4j.core.impl .DefaultLogEventFactoryFactory class used by LoggerConfig to create LogEvent instances. (Ignored when the AsyncLoggerContextSelectoris used.)
log4j2.loggerContextFactoryorg.apache.logging.log4j.simple .SimpleLoggerContextFactoryFactory class used by LogManager to bootstrap the logging implementation. The core jar providesorg.apache.logging.log4j.core.impl.Log4jContextFactory.
log4j.configurationFactory Fully specified class name of a class extending org.apache.logging.log4j.core.config.ConfigurationFactory. If specified, an instance of this class is added to the list of configuration factories.
log4j.shutdownHookEnabledtrueOverrides the global flag for whether or not a shutdown hook should be used to stop a LoggerContext. By default, this is enabled and can be disabled on a per-configuration basis. When running with the log4j-web module, this is automatically disabled.
log4j.shutdownCallbackRegistryorg.apache.logging.log4j.core.util .DefaultShutdownCallbackRegistryFully specified class name of a class implementing ShutdownCallbackRegistry. If specified, an instance of this class is used instead of DefaultShutdownCallbackRegistry. The specified class must have a default constructor.
log4j.ClockSystemClockImplementation of the org.apache.logging.log4j.core.util.Clock interface that is used for timestamping the log events. 
By default, System.currentTimeMillis is called on every log event. 
You can also specify a fully qualified class name of a custom class that implements the Clock interface.
org.apache.logging.log4j.levelERRORLog level of the default configuration. The default configuration is used if the ConfigurationFactory could not successfully create a configuration (e.g. no log4j2.xml file was found).
disableThreadContextfalseIf true, the ThreadContext stack and map are disabled. (May be ignored if a custom ThreadContext map is specified.)
disableThreadContextStackfalseIf true, the ThreadContext stack is disabled.
disableThreadContextMapfalseIf true, the ThreadContext map is disabled. (May be ignored if a custom ThreadContext map is specified.)
log4j2.threadContextMap Fully specified class name of a custom ThreadContextMap implementation class.
isThreadContextMapInheritablefalseIf true use a InheritableThreadLocal to implement the ThreadContext map. Otherwise, use a plain ThreadLocal. (May be ignored if a custom ThreadContext map is specified.)
log4j2.disable.jmxfalseIf true, Log4j configuration objects like LoggerContexts, Appenders, Loggers, etc. will not be instrumented with MBeans and cannot be remotely monitored and managed.
log4j2.jmx.notify.asyncfalse for web apps, true otherwiseIf true, log4j's JMX notifications are sent from a separate background thread, otherwise they are sent from the caller thread. If the javax.servlet.Servlet class is on the classpath, the default behaviour is to use the caller thread to send JMX notifications.
log4j.skipJansifalseIf true, the ConsoleAppender will not try to use the Jansi output stream on Windows.
log4j.ignoreTCLfalseIf true, classes are only loaded with the default class loader. Otherwise, an attempt is made to load classes with the current thread's context class loader before falling back to the default class loader.
org.apache.logging.log4j.uuidSequence0System property that may be used to seed the UUID generation with an integer value.
org.apache.logging.log4j.simplelog .showContextMapfalseIf true, the full ThreadContext map is included in each SimpleLogger log message.
org.apache.logging.log4j.simplelog .showlognamefalseIf true, the logger name is included in each SimpleLogger log message.
org.apache.logging.log4j.simplelog .showShortLognametrueIf true, only the last component of a logger name is included in SimpleLogger log messages. (E.g., if the logger name is "mycompany.myproject.mycomponent", only "mycomponent" is logged.
org.apache.logging.log4j.simplelog .showdatetimefalseIf true, SimpleLogger log messages contain timestamp information.
org.apache.logging.log4j.simplelog .dateTimeFormat"yyyy/MM/dd HH:mm:ss:SSS zzz"Date-time format to use. Ignored if org.apache.logging.log4j.simplelog.showdatetime is false.
org.apache.logging.logj.simplelog .logFilesystem.err"system.err" (case-insensitive) logs to System.err, "system.out" (case-insensitive) logs to System.out, any other value is interpreted as a file name to save SimpleLogger messages to.
org.apache.logging.log4j.simplelog .levelERRORDefault level for new SimpleLogger instances.
org.apache.logging.log4j.simplelog.<loggerName>levelSimpleLogger default log levelLog level for a the SimpleLogger instance with the specified name.
org.apache.logging.log4j.simplelog .StatusLogger.levelERRORThis property is used to control the initial StatusLogger level, and can be overridden in code by callingStatusLogger.getLogger().setLevel(someLevel). Note that the StatusLogger level is only used to determine the status log output level until a listener is registered. In practice, a listener is registered when a configuration is found, and from that point onwards, status messages are only sent to the listeners (depending on their statusLevel).
Log4jDefaultStatusLevelERROR

The StatusLogger logs events that occur in the logging system to the console. During configuration, AbstractConfiguration registers a StatusConsoleListener with the StatusLogger that may redirect status log events from the default console output to a file. The listener also supports fine-grained filtering. This system property specifies the default status log level for the listener to use if the configuration does not specify a status level.

Note: this property is used by the log4j-core implementation only after a configuration file has been found.

log4j2.StatusLogger.levelWARN

The initial "listenersLevel" of the StatusLogger. If StatusLogger listeners are added, the "listenerLevel" is changed to that of the most verbose listener. If any listeners are registered, the listenerLevel is used to quickly determine if an interested listener exists.

By default, StatusLogger listeners are added when a configuration is found and by the JMX StatusLoggerAdmin MBean. For example, if a configuration contains <Configuration status="trace">, a listener with statusLevel TRACE is registered and the StatusLogger listenerLevel is set to TRACE, resulting in verbose status messages displayed on the console.

If no listeners are registered, the listenersLevel is not used, and the StatusLogger output level is determined byStatusLogger.getLogger().getLevel() (see property org.apache.logging.log4j.simplelog .StatusLogger.level).

log4j2.status.entries200Number of StatusLogger events that are kept in a buffer and can be retrieved with StatusLogger.getStatusData().
AsyncLogger.ExceptionHandler See Async Logger System Properties for details.
AsyncLogger.RingBufferSize256 * 1024See Async Logger System Properties for details.
AsyncLogger.WaitStrategySleepSee Async Logger System Properties for details.
AsyncLogger.ThreadNameStrategyCACHEDSee Async Logger System Properties for details.
AsyncLoggerConfig.ExceptionHandler See Mixed Async/Synchronous Logger System Properties for details.
AsyncLoggerConfig.RingBufferSize256 * 1024See Mixed Async/Synchronous Logger System Properties for details.
AsyncLoggerConfig.WaitStrategySleepSee Mixed Async/Synchronous Logger System Properties for details.
log4j.jul.LoggerAdapterorg.apache.logging.log4j.jul .ApiLoggerAdapterDefault LoggerAdapter to use in the JUL adapter. By default, if log4j-core is available, then the classorg.apache.logging.log4j.jul .CoreLoggerAdapter will be used. Otherwise, the ApiLogggerAdapter will be used. Custom implementations must provide a public default constructor.