Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback's architecture.
Information
- Spring framework implementation
- Changes http://logging.apache.org/log4j/2.x/changes-report.html#a2.7
- Latest version: 2.7 - 2016-10-02
- Frequently Asked Questions - https://logging.apache.org/log4j/2.0/faq.html
- Migrating from Log4j 1.x - http://logging.apache.org/log4j/2.x/manual/migration.html
- Dependencies
- Using Log4j in your Apache Maven build - http://logging.apache.org/log4j/2.x/maven-artifacts.html
- Log4j Runtime Dependencies - https://logging.apache.org/log4j/2.0/runtime-dependencies.html
- Log4j 1.2 bridge - http://logging.apache.org/log4j/2.x/log4j-1.2-api/
- Configuration
- Programmatic Configuration http://logging.apache.org/log4j/2.x/manual/customconfig.html
- Plugins - http://logging.apache.org/log4j/2.x/manual/plugins.html
Problems
- New package
org.apache.log4j changed to org.apache.logging.log4j
We may need to change in a bunch of modules when we try to convert to the new package
- DOM and Property Configurator was removed
- They're available in new version - https://issues.apache.org/jira/browse/LOG4J2-340
- Replaced with an empty implementation if we used Log4j 1.2 bridge
- Log4J 2 over the old fashioned properties file is supported so far
Support configuration from version 1.x log4j.properties - https://issues.apache.org/jira/browse/LOG4J2-63
- API for setLevel / getEffectiveLevel - http://apache-logging.6191.n7.nabble.com/API-and-setLevel-getEffectiveLevel-td36238.html
- Unit-testing
Customer Appender
Writer loggerOut = new StringWriter(); final Layout layout = new EnhancedPatternLayout(EnhancedPatternLayout.TTCC_CONVERSION_PATTERN); Logger.getRootLogger().setLevel(Level.INFO); // Other tests might have set this to silent Logger.getRootLogger().addAppender(new WriterAppender(layout, loggerOut));
EnhancedPatternLayout is removed. Maybe use PatternLayout directly
Need an another way to setLevel for logger - http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2
Logger.getRootLogger().addAppender can not be used anymore. Should use a Log4j2 plugin to add an appender (http://stackoverflow.com/questions/24205093/how-to-create-a-custom-appender-in-log4j2)
WriterAppender is removed also
Code Snippets