Apache Log4j

Apache Log4j is a versatile, industrial-grade Java logging framework composed of an API, its implementation, and components to assist the deployment for various use cases. Log4j is used by 8% of the Maven ecosystem and listed as one of the top 100 critical open source software projects. The project is actively maintained by a team of several volunteers and supported by a big community.

Features

We share below some highlights from Log4j features.

Batteries included

Log4j bundles a rich set of components to assist various use cases.

  • Appenders targeting files, network sockets, databases, SMTP servers, etc.

  • Layouts that can render CSV, HTML, JSON, Syslog, etc. formatted outputs

  • Filters that can be configured using log event rates, regular expressions, scripts, time, etc.

  • Lookups for accessing system properties, environment variables, log event fields, etc.

API separation

The API for Log4j (i.e., log4j-api) is separate from the implementation (i.e., log4j-core) making it clear for application developers which classes and methods they can use while ensuring forward compatibility. (See API Separation for details.) The Log4j API also provides the most feature rich logging facade in the market; support for various Message types (Object, Map, etc.) besides plain String, lambda expressions, parametrized logging, markers, levels, diagnostic contexts (aka. MDC/NDC), etc. Check out the Java API, Kotlin API, and Scala API pages for further information.

No vendor lock-in

Even though the Log4j API is implemented by the Log4j at its fullest, users can choose to use another logging backend. This can be achieved by either using another backend implementing the Log4j API, or forwarding Log4j API calls to another logging facade (e.g., SLF4J) and using a backend for that particular facade.

Performance

When configured correctly, Log4j can deliver excelling performance without almost any burden on the Java garbage collector. This is made possible via an asynchronous logger founded on the LMAX Disruptor technology (having its roots in the demanding industry of financial trading) and the garbage-free features baked at hot paths. Check out the Performance page for details.

Extensibility

Log4j contains a fully-fledged plugin support that users can leverage to extend its functionality. You can easily add your own components (layouts, appenders, filters, etc.) or customizing existing ones (e.g., adding new directives to the Pattern or JSON Template Layout). Check out the Extending Log4j page.