Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 why would you use it over log4j? [closed]

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?

like image 560
Scott Neville Avatar asked May 03 '15 21:05

Scott Neville


People also ask

What is difference between Log4j and Log4j2?

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.

Which is better Log4j or Log4j2?

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.

Can we use Log4j and Log4j2 together?

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.

Can I replace Log4j with Log4j2?

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.


1 Answers

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> 
like image 83
Remko Popma Avatar answered Oct 01 '22 03:10

Remko Popma