Manual
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. The project is actively maintained by a team of volunteers and supported by a big community.
Logging is an essential part of the software development process. It provides a way to track the flow of execution in a program, allowing developers to understand the application’s behavior without needing a debugger. This is particularly useful when tracking bugs or understanding why a particular code runs slowly.
The original concept for Log4j was conceived in early 1996 when the E.U. SEMPER project decided to develop its own tracing API. In 2003, the project was donated to the Apache Software Foundation, which became Apache Log4j.
Since then, Log4j has seen numerous releases and has become a widely adopted solution.
When should you use Log4j?
Log4j is an excellent choice for any Java application that needs logging capabilities. It is user-friendly, fast, and flexible. You can use it to log messages at different levels of severity, from debug to fatal, and you can configure it to log messages to various destinations, such as files, databases, or the console. See Getting started for an introduction.
When not to use Log4j?
While Log4j is a highly suitable choice for many applications, it may be challenging to locate the information you require when logging a high volume of messages. Additionally, logging can impact your application’s performance.
Log4j offers solutions to address these concerns. However, if you are in a unique situation where you are concerned about logging overhead or volume, you may wish to consider not using logging at all.
What does Log4j offer?
Log4j offers numerous features, including:
- 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 based on log event rates, regular expressions, scripts, time, etc.
-
Lookups for accessing system properties, environment variables, log event fields, etc.
-
- Reliability
-
Log4j is built with solid reliability in mind. It can automatically reload its configuration upon modification and will do so without losing log events while reconfiguration occurs.
- Performance
-
When configured correctly, Log4j can deliver excelling performance without almost any burden on the Java garbage collector, and it will do so without sacrificing reliability. Check out the Performance page for details.
- Extensibility
-
Log4j contains a fully-fledged plugin support that users can leverage to extend functionality. You can easily add your components (layouts, appenders, filters, etc.) or customize existing ones (e.g., adding new directives to Pattern Layout or JSON Template Layout). Check out the Extending Log4j page.
- Powerful API
-
Log4j is a logging system where the API (called Log4j API) and its implementation (called Log4j Core) is distinctly separate from each other. Log4j API provides the most feature-rich logging facade in the market; support for various
Message
types (such asObject
orMap
) besides plainString
, lambda expressions, parameterized logging, markers, levels, diagnostic contexts (aka. MDC/NDC), etc. Log4j team takes backward compatibility very seriously and makes sure people relying on Log4j API gets a logging facade that is straightforward to use in a correct and future-proof way. Check out the Java API, Kotlin API, and Scala API pages for further information. - No vendor lock-in
-
Log4j API is a generic logging facade for various logging frameworks. While Log4j Core implements it at its fullest, you can easily switch to other implementations such as Logback or JUL (
java.util.logging
).