Over a million developers have joined DZone.

log consolidation with log4j2

DZone's Guide to

log consolidation with log4j2

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

log4j, slf4j, logback, commons-logging... I keep wondering which logging strategy I should use.

Recently, I consolidated the logging library for my project to log4j2 . Yes, it's a new version of log4j!

1. My (your) project policy

  • Develop new java/web project.
  • Use log4j2.x logger for new code.
  • The other log that is output through slf4j commons-logging should be also consolidated to log4j2.
  • One configuration file. log4j2.xml
  • Use maven as a build tool.

2. pom.xml

    ... other dependency...

3. src/main/resources/log4j.xml

        <Console name="console">
            <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} %-5level %logger{32} %msg%n" />
        <logger name="org.springframework"
              level="debug" additivity="false">
            <appender-ref ref="console" />
        <logger name="com.example.myapp"
              level="debug" additivity="false">
            <appender-ref ref="console" />
        <root level="info">
            <appender-ref ref="console" />

4. Write code

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Foo {
    private static Logger logger = LogManager.getLogger();
    public void bar(String a, String b) {
        logger.debug("a={},b={}", a, b);

The point of the sample code is place holder, that is new functionality of log4j2.x .

If you wrote the code like 'logger.debug("a=" + a + ",b=" + b); ' , The logging performance will be down. See log4j2 performance for detail.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}