I must be missing something but I have been looking at this for a few days now, but why on earth would you ever use log4j2 over log4j (other than the performance)?
From what I have seen so far, log4j2 is advertised as simpler to configure, but its actually vastly more complicated (been three days now and I still cant get it to write a log in my home directory). The auto configuration simply does not work for me (or at least I cant get it to work), the configuration file itself is substantially more complex in its structure and appears to be much harder to add things in at runtime to help diagnose.
So other than the performance is there any reason to use log4j2 over original log4j?
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. Automatically reload its configuration upon modification without losing log events while reconfiguring.
Conclusion. Log4j, Logback, and Log4j2 are good logging frameworks that are broadly used. So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks.
Log4j 2 provides support for the Log4j 1 logging methods by providing alternate implementations of the classes containing those methods. These classes may be found in the log4j-1.2-api jar distributed with the project.
You may be able to convert an application to Log4j 2 without any code changes by replacing the Log4j 1. x jar file with Log4j 2's log4j-1.2-api.
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.
Be aware
log4j-1.2-api
adapter but customizations that rely on Log4j 1.2 internals may not work. Tips when upgrading
Common issues people are having when getting started with log4j2:
log4j.configurationFile
system property <Configuration status="trace">
in the beginning of your config fileIf 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With