Reasons to upgrade from Log4j 1.x to Log4j 2
Update: since August 2015, Log4j 1.x is officially End of Life and it is recommended to upgrade to Log4j 2. Update 2: Log4j 1.2 is broken in Java 9.
- Community support: Log4j 1.x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed.
- Async Loggers - performance similar to logging switched off
- Custom log levels
- Automatically reload its configuration upon modification without losing log events while reconfiguring.
- Java 8-style lambda support for lazy logging
- Log4j 2 is garbage-free (or at least low-garbage) since version 2.6
- Filtering: filtering based on context data, markers, regular expressions, and other components in the Log event. Filters can be associated with Loggers. You can use a common Filter class in any of these circumstances.
- Plugin Architecture - easy to extend by building custom components
- Supported APIs: SLF4J, Commons Logging, Log4j-1.x and java.util.logging
- Log4j 2 API separate from the Log4j 2 implementation. API supports more than just logging Strings: CharSequences, Objects and custom Messages. Messages allow support for interesting and complex constructs to be passed through the logging system and be efficiently manipulated. Users are free to create their own Message types and write custom Layouts, Filters and Lookups to manipulate them.
- Concurrency improvements: log4j2 uses java.util.concurrent libraries to perform locking at the lowest level possible. Log4j-1.x has known deadlock issues.
- Configuration via XML, JSON, YAML, properties configuration files or programmatically.
Be aware
- log4j2.xml and log4j2.properties formats are different from the Log4j 1.2 configuration syntax
- Log4j 2 is not fully compatible with Log4j 1.x: The Log4j 1.2 API is supported by the
log4j-1.2-api
adapter but customizations that rely on Log4j 1.2 internals may not work.
- Java 6 required for version 2.0 to 2.3. Java 7 is required for Log4j 2.4 and later.
Tips when upgrading
Common issues people are having when getting started with log4j2:
- You need (at least) both log4j-api-2.6.2.jar and log4j-core-2.6.2.jar in your classpath
- Log4j2 looks for a log4j2.xml config file, not a log4j.xml config file
- Config file location: either put it in the classpath or specify its path with the
log4j.configurationFile
system property
- To debug the configuration, use
<Configuration status="trace">
in the beginning of your config file
- I would recommend starting with one of the many sample configurations provided in the log4j2 manual, then add more bells and whistles bit by bit.
If your problem is not one of the above, please show your config and provide more detail on what problem you are experiencing. (Not sure what you expect from auto-configuration, this is a very basic function that logs ERROR events to the console if log4j2 cannot find a configuration file. This will rarely be sufficient.)
To write to your home directory, you can use the system property lookup ${sys:PROPERTYNAME}
. Below is an example configuration to demonstrate:
<Configuration status="trace">
<Properties>
<Property name="logfile">${sys:user.home}/log${date:yyyyMMdd}.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
<File name="FILE" fileName="${sys:logfile}">
<PatternLayout>
<pattern>%d %p [%t] %c{1.} %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="STDOUT" level="ERROR" />
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>