Apache Log4j | |
Developer: | Apache Software Foundation |
Released: | [1] |
Latest Release Date: | [2] |
Operating System: | Cross-platform |
Programming Language: | Java |
Genre: | Logging |
License: | Apache License 2.0 |
Apache Log4j is a Java-based logging utility originally written by Ceki Gülcü. It is part of the Apache Logging Services, a project of the Apache Software Foundation. Log4j is one of several Java logging frameworks.
Gülcü has since created SLF4J, Reload4j,[3] and Logback[4] which are alternatives to Log4j.[5]
The Apache Log4j team developed Log4j 2[6] in response to the problems of Log4j 1.2, 1.3, java.util.logging
and Logback, addressing issues which appeared in those frameworks.[7] In addition, Log4j 2 offered a plugin architecture which makes it more extensible than its predecessor. Log4j 2 is not backwards compatible with 1.x versions,[8] although an "adapter" is available. On August 5, 2015, the Apache Logging Services Project Management Committee announced that Log4j 1 had reached end of life and that users of Log4j 1 were advised to upgrade to Apache Log4j 2.[9] On January 12, 2022, a forked and renamed log4j version 1.2 was released by Ceki Gülcü as Reload4j version 1.2.18.0 with the aim of fixing the most urgent issues in log4j 1.2.17 that had accumulated since its release in 2013.[10]
On December 9, 2021, a zero-day vulnerability involving arbitrary code execution in Log4j 2 was published by the Alibaba Cloud Security Team and given the descriptor "Log4Shell". It has been characterized by Tenable as "the single biggest, most critical vulnerability of the last decade".
Apache Log4j 2 is the successor of Log4j 1 which was released as GA version in July 2014. The framework was rewritten from scratch and has been inspired by existing logging solutions, including Log4j 1 and java.util.logging. The main differences[11] [12] from Log4j 1 are:
One of the most recognized features of Log4j 2 is the performance of the "Asynchronous Loggers".[13] Log4j 2 makes use of the LMAX Disruptor.[14] The library reduces the need for kernel locking and increases the logging performance by a factor of 12. For example, in the same environment Log4j 2 can write more than 18,000,000 messages per second, whereas other frameworks like Logback and Log4j 1 just write < 2,000,000 messages per second.
The following table defines the built-in log levels and messages in Log4j, in decreasing order of severity. The left column lists the log level designation in Log4j and the right column provides a brief description of each log level.
Level | Description | |
---|---|---|
OFF | The highest possible rank and is intended to turn off logging. | |
FATAL | Severe errors that cause premature termination. Expect these to be immediately visible on a status console. | |
ERROR | Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console. | |
WARN | Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console. | |
INFO | Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum. | |
DEBUG | Detailed information on the flow through the system. Expect these to be written to logs only. Generally speaking, most lines logged by your application should be written as DEBUG. | |
TRACE | Most detailed information. Expect these to be written to logs only. Since version 1.2.12.[15] |
Log4j 2 allows users to define their own log levels.[16] A source code generator tool is provided to create Loggers that support custom log levels identically to the built-in log levels. Custom log levels can either complement or replace the built-in log levels.
Log4j can be configured[17] through a configuration file or through Java code. Configuration files can be written in XML, JSON, YAML, or properties file format. Within a configuration, you can define three main components: Loggers, Appenders, and Layouts. Configuring logging via a file has the advantage that logging can be turned on or off without modifying the application that uses Log4j. The application can be allowed to run with logging off until there's a problem, for example, and then logging can be turned back on simply by modifying the configuration file.
Loggers[18] are named log message destinations. They are the names that are known to the Java application. Each logger is independently configurable as to what level of logging (FATAL, ERROR, etc.) it currently logs. In early versions of Log4j, these were called category and priority, but now they're called logger and level, respectively. A Logger can send log messages to multiple Appenders.
The actual outputs are done by Appenders.[19] There are numerous Appenders available, with descriptive names, such as FileAppender, RollingFileAppender, ConsoleAppender, SocketAppender, SyslogAppender, and SMTPAppender. Log4j 2 added Appenders that write to Apache Flume, the Java Persistence API, Apache Kafka, NoSQL databases, Memory-mapped files, Random Access files[20] and ZeroMQ endpoints. Multiple Appenders can be attached to any Logger, so it's possible to log the same information to multiple outputs; for example to a file locally and to a socket listener on another computer.
Appenders use Layouts[21] to format log entries. A popular way to format one-line-at-a-time log files is PatternLayout, which uses a pattern string, much like the C / C++ function printf. There are also HTMLLayout and XMLLayout formatters for use when HTML or XML formats are more convenient, respectively. Log4j 2 added Layouts for CSV, Graylog Extended Log Format (GELF),[22] JSON, YAML and RFC-5424.[23]
In Log4j 2, Filters[24] can be defined on configuration elements to give more fine-grained control over which log entries should be processed by which Loggers and Appenders. In addition to filtering by log level and regular expression matching on the message string, Log4j 2 added burst filters, time filters, filtering by other log event attributes like Markers or Thread Context Map and JSR 223 script filters.
To debug a misbehaving configuration:
status
attribute to TRACE to send internal status logging output to standard out. To enable status logging before the configuration is found, use the Java VM property -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace
.-Dlog4j.debug
.To find out where a log4j2.xml configuration file was loaded from inspect getClass.getResource("/log4j2.xml")
.
There is also an implicit "unconfigured" or "default" configuration of Log4j, that of a Log4j-instrumented Java application which lacks any Log4j configuration. This prints to stdout a warning that the program is unconfigured, and the URL to the Log4j web site where details on the warning and configuration may be found. As well as printing this warning, an unconfigured Log4j application will only print ERROR or FATAL log entries to standard out.
TTCC is a message format used by log4j.[25] TTCC is an acronym for Time Thread Category Component. It uses the following pattern:
%r [%t] %-5p %c %x - %m%n
Where
Mnemonic | Description | |
---|---|---|
%r | Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. | |
%t | Used to output the name of the thread that generated the logging event. | |
%p | Used to output the priority of the logging event. | |
%c | Used to output the category of the logging event. | |
%x | Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.[26] | |
%X | Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event for specified key.[27] | |
%m | Used to output the application supplied message associated with the logging event. | |
%n | Used to output the platform-specific newline character or characters. |
Example output
467 [main] INFO org.apache.log4j.examples.Sort – Exiting main method.
See main article: Log4Shell. A zero-day vulnerability involving remote code execution in Log4j 2, given the descriptor "Log4Shell" (CVE-2021-44228), was found and reported to Apache by Alibaba on November 24, 2021, and published in a tweet on December 9, 2021.[39] Affected services include Cloudflare, iCloud, ,[40] Steam, Tencent QQ, and Twitter.[41] [42] [43] The Apache Software Foundation assigned the maximum CVSS severity rating of 10 to Log4Shell, as millions of servers could be potentially vulnerable to the exploit.[43] The vulnerability was characterized by cybersecurity firm Tenable as "the single biggest, most critical vulnerability of the last decade"[44] and Lunasec's Free Wortley characterized it as "a design failure of catastrophic proportions".[45]
In the United States, the director of the Cybersecurity and Infrastructure Security Agency (CISA), Jen Easterly, termed the exploit "critical" and advised vendors to prioritize software updates,[46] and the German agency Federal Office for Information Security (BSI) designated the exploit as being at its highest threat level, calling it an "extremely critical threat situation" (translated).[47] [48] The Canadian Centre for Cyber Security (CCCS) called on organisations to take on immediate action.[49]
The feature causing the vulnerability could be disabled with a configuration setting, which had been removed[50] in Log4j version 2.15.0-rc1 (officially released on December 6, 2021, three days before the vulnerability was published), and replaced by various settings restricting remote lookups, thereby mitigating the vulnerability.[51] [52] For additional security, all features using JNDI, on which this vulnerability was based, will be disabled by default, and support for message lookups removed from version 2.16.0 onward.[53] [54]