# Log4j

**Log4j** 是一个基于 [Java](https://www.edgepedia.org/zh/java-software-platform) 的日志框架，由 Apache 软件基金会（Apache Software Foundation）旗下的 Apache Logging Services 项目管理维护，允许开发者以可配置的严重级别记录应用程序消息，并将其路由到可配置的目的地。它起源于 1999 年，后被重写为 Log4j 2，成为 Java 生态系统中部署最广泛的库之一；2021 年，Log4j 2 中的 [Log4Shell](https://www.edgepedia.org/zh/log4shell) 漏洞引发了软件史上规模最大的安全修复行动之一。<sup>[1](https://news.apache.org/foundation/entry/apache_logging_services_project_announces)</sup><sup> • </sup><sup>[2](https://logging.apache.org/log4j/2.x/)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 分层日志记录器（hierarchical logger）的构想由 N. Asokan、Ceki Gülcü 和 Michael Steiner 于 1996 年为欧盟资助的 SEMPER 项目提出；后在 IBM 苏黎世研究实验室（IBM Zurich Research Laboratory）进一步完善<sup>[3](https://svn.apache.org/repos/asf/logging/log4j/tags/TAG_CHAINSAW2_MOVE/docs/history.html)</sup> |
| 首次发布 / Log4j 1 生命周期终止 | 首次发布于 1999 年；Log4j 1.x 在历经 21 个版本（最后一个于 2012 年发布）后，于 2015 年 8 月 5 日被宣布生命周期终止<sup>[1](https://news.apache.org/foundation/entry/apache_logging_services_project_announces)</sup> |
| 采用率 | 在 New Relic 2024 年调查中使用率最高的 Java 日志框架，76% 的 Java 应用使用它<sup>[4](https://newrelic.com/sites/default/files/2024-05/new-relic-state-of-the-java-ecosystem-2024-05-29.pdf)</sup> |
| Log4Shell | CVE-2021-44228，CVSS 评分 10.0，2021 年 12 月 10 日披露，影响 Log4j 2.0-beta9 至 2.14.1；通过 JNDI 查找解析实现远程代码执行<sup>[5](https://www.cyber.gov.au/sites/default/files/2023-02/Joint%20Cyber%20Security%20Advisory%20Mitigating%20Log4Shell%20and%20Other%20Log4j-Related%20Vulnerabilities_0.pdf)</sup><sup> • </sup><sup>[6](https://logging.apache.org/security.html?trk=article-ssr-frontend-pulse_little-text-block)</sup> |
| 紧急版本 | 2.15.0（2021 年 12 月 6 日）、2.16.0（2021 年 12 月 13 日，默认禁用 JNDI）、2.17.0（2021 年 12 月 17 日，禁用递归查找）<sup>[7](https://logging.apache.org/log4j/2.x/release-notes.html)</sup> |
| 当前版本线 | Log4j 2.x（Java 8）处于活跃维护中；Log4j 3 处于测试阶段，3.0.0-beta3 于 2024 年 11 月 9 日发布<sup>[7](https://logging.apache.org/log4j/2.x/release-notes.html)</sup><sup> • </sup><sup>[8](https://logging.apache.org/log4j/3.x/release-notes.html)</sup> |
| 残余风险 | 2025 年，近 3 亿次 Log4j 下载中约有 4000 万次（约 13%）仍为存在漏洞的版本<sup>[9](https://www.sonatype.com/whitepapers/the-persistence-of-open-source-vulnerabilities)</sup> |

## 历史

Log4j 最早的祖先是为欧盟资助的 SEMPER 项目编写的。N. Asokan、Ceki Gülcü 和 Michael Steiner 于 1996 年提出了分层日志记录器的构想，这一设计至今仍是 Log4j 的核心。此后数年间，该软件包在 IBM 苏黎世研究实验室得到了大幅改进，不过 Log4j 现已不再与 IBM 有关联。<sup>[3](https://svn.apache.org/repos/asf/logging/log4j/tags/TAG_CHAINSAW2_MOVE/docs/history.html)</sup> Log4j 于 1999 年首次发布，据该项目自述，它迅速成为有史以来使用最广泛的日志框架。<sup>[1](https://news.apache.org/foundation/entry/apache_logging_services_project_announces)</sup> Apache 软件基金会于 2023 年 12 月发布的项目 20 周年纪念博客文章将 Log4j 的诞生时间定为 2003 年，即它转入 Apache Logging Services 项目之时；这两个日期指的是不同的里程碑——代码库的首次发布和它作为 Apache 项目的生命起点。<sup>[10](https://news.apache.org/foundation/entry/apache-logging-services-celebrates-20th-anniversary-of-log4j)</sup>

**Log4j 1 及其退役。** Logging Services 项目管理委员会于 2015 年 8 月 5 日宣布 Log4j 1.x 已到达生命周期终点，将不再发布任何版本，包括不再提供安全修复；用户被引导迁移至 Log4j 2 以及一个将 Log4j 1 调用转发至 Log4j 2 的桥接组件。<sup>[1](https://news.apache.org/foundation/entry/apache_logging_services_project_announces)</sup> 当 2021 年 12 月 Log4j 2 紧急发布期间有人请求重新考虑该决定时，项目管理委员会一致重申了生命周期终止的决定，声明不会在 Log4j 1 代码库上投入任何资源，相关拉取请求将以“不予修复”（Won't Fix）关闭。<sup>[11](https://apache.googlesource.com/logging-log4j1/)</sup> 2015 年 8 月之后针对 Log4j 1 报告的漏洞不予核查，也不会修复。<sup>[12](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html)</sup>

## Log4j 2 的架构

Log4j 2 是从头重写的。它使用了新的包命名空间 `org.apache.logging.log4j` 而非 `org.apache.log4j`，并将日志 API（Log4j API）与其参考实现（Log4j Core）分离，因此应用程序也可以将该 API 与其他实现（如 Logback 或 `java.util.logging`）搭配使用。<sup>[2](https://logging.apache.org/log4j/2.x/)</sup><sup> • </sup><sup>[12](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html)</sup>

核心组件的工作方式如下。`LoggerContext` 与 `Configuration` 一同创建，锚定整个组合结构，并创建应用程序代码所调用的 `Logger` 对象。每个日志记录器的行为由 `LoggerConfig` 管控，后者携带一个严重级别；没有自身配置的日志记录器从层级结构中继承有效级别。`Appender` 将 `LogEvent` 投递到控制台、文件、数据库或网络套接字等目标，通常使用 `Layout` 对事件进行编码，而 `Filter` 决定某次日志调用或事件是否应被进一步处理。除非关闭可加性（additivity），Appender 会沿 `LoggerConfig` 层级以可加方式向下继承。<sup>[13](https://logging.apache.org/log4j/2.x/manual/architecture.html)</sup>

在此骨架之外，Log4j 2 还附带了面向文件、套接字、数据库和 SMTP 服务器的 Appender；渲染 CSV、HTML、JSON 和 [Syslog](https://www.edgepedia.org/zh/syslog) 格式的 Layout；基于速率、正则表达式、脚本和时间的 Filter；以及将系统属性、环境变量和日志事件字段解析为配置值的查找（lookup）机制。它可以在不丢失日志事件的情况下重新加载配置，项目文档还记载了参数化消息和 `Supplier` 参数等性能实践，以避免为将被丢弃的事件执行格式化工作。<sup>[2](https://logging.apache.org/log4j/2.x/)</sup><sup> • </sup><sup>[14](https://logging.apache.org/log4j/2.x/manual/performance.html)</sup>

## Log4Shell 与 2021 年 12 月的漏洞

Log4Shell 于 2021 年 12 月 10 日披露，是一个影响 Log4j 2.0-beta9 至 2.14.1 版本的远程代码执行漏洞。该缺陷在于 Java 命名与目录接口（JNDI）解析变量的方式：受影响版本包含 JNDI 功能（如消息查找替换），但未对攻击者控制的 LDAP、DNS 及其他 JNDI 端点加以防护。能够控制日志消息或其参数的攻击者可使系统执行任意代码并完全控制系统。<sup>[5](https://www.cyber.gov.au/sites/default/files/2023-02/Joint%20Cyber%20Security%20Advisory%20Mitigating%20Log4Shell%20and%20Other%20Log4j-Related%20Vulnerabilities_0.pdf)</sup> Apache 为其分配了 CVE-2021-44228 编号，CVSS 评分为 10.0，并指出仅 `log4j-core` JAR 受影响；仅使用 `log4j-api` 的应用程序不受影响。<sup>[6](https://logging.apache.org/security.html?trk=article-ssr-frontend-pulse_little-text-block)</sup>

响应接踵而至。2021 年 12 月 6 日发布的 Log4j 2.15.0 通过限制 JNDI 协议并默认禁用日志消息中的查找，修复了 CVE-2021-44228。该修复在某些非默认配置下被证明不完整：2021 年 12 月 13 日披露、评级为 9.0 的 CVE-2021-45046 允许通过线程上下文查找（Thread Context Lookup）实现远程代码执行或拒绝服务，该漏洞在 2.16.0 中修复，此版本同时默认完全禁用 JNDI。2021 年 12 月 16 日披露的 CVE-2021-45105 允许通过自引用查找引发的失控递归导致拒绝服务，在 2.17.0 中修复，该版本禁用了查找的递归求值，并要求通过单独的系统属性来启用 JNDI 功能。<sup>[7](https://logging.apache.org/log4j/2.x/release-notes.html)</sup><sup> • </sup><sup>[6](https://logging.apache.org/security.html?trk=article-ssr-frontend-pulse_little-text-block)</sup> 对于无法升级的环境，CISA、FBI、NSA 及合作机构发布的联合公告建议在 Java 8 或更高版本上升级至 2.17.0，或在 Java 7 上升级至 2.12.3，并介绍了设置 `log4j2.formatMsgNoLookups=true` 或从类路径中移除 `JndiLookup` 类等变通方案。<sup>[5](https://www.cyber.gov.au/sites/default/files/2023-02/Joint%20Cyber%20Security%20Advisory%20Mitigating%20Log4Shell%20and%20Other%20Log4j-Related%20Vulnerabilities_0.pdf)</sup>

严重性源于 Java 在 IT 和运营技术平台中的普遍存在、利用的简便性以及应用缓解措施的成本。利用活动始于 2021 年 12 月 1 日前后，FBI 观察到大规模扫描以部署加密货币挖矿和僵尸网络恶意软件。<sup>[5](https://www.cyber.gov.au/sites/default/files/2023-02/Joint%20Cyber%20Security%20Advisory%20Mitigating%20Log4Shell%20and%20Other%20Log4j-Related%20Vulnerabilities_0.pdf)</sup> 披露后的最初 72 小时内发起了近百万次攻击尝试。<sup>[15](https://www.itpro.com/software/open-source/a-concerning-number-of-log4j-downloads-are-still-vulnerable-four-years-on)</sup> 一年后，国土安全部网络安全审查委员会将 Log4j 描述为“地方性流行”（endemic）问题，指出许多公司缺乏甚至判断自身是否受影响所需的资产清单记录。<sup>[16](https://www.axios.com/2022/12/09/log4j-year-anniversary-cybersecurity)</sup>

## 后续影响与持续暴露

Log4Shell 重塑了软件供应链政策。在美国，该事件促使第 14028 号行政命令要求提供软件物料清单（SBOM），而欧洲则以 NIS2 指令和《[网络弹性法案](https://www.edgepedia.org/zh/cyber-resilience-act)》（Cyber Resilience Act）作出更严格的监管回应。<sup>[15](https://www.itpro.com/software/open-source/a-concerning-number-of-log4j-downloads-are-still-vulnerable-four-years-on)</sup> 然而，修复工作进展滞后。2023 年 12 月 Veracode 的一份报告发现，近 3900 个组织仍在超过 38000 个应用中使用存在漏洞的 Log4j 版本，其中 32% 的应用依赖自 2015 年起便不再受支持的 Log4j 1.x。<sup>[17](https://www.itpro.com/security/zero-day-exploit/361819/what-is-log4shell-log4j-vulnerability)</sup>

下载数据在数年后显示出同样的模式。2025 年，[Maven Central](https://www.edgepedia.org/zh/maven-central) 记录的 Log4j 总下载量近 3 亿次，其中约 13%（约 4000 万次）仍为存在漏洞的版本。在开发者人口最多的十个国家中，8% 至 29% 的 Log4j 下载仍包含 Log4Shell，其中印度为 29%，中国为 28%，日本为 22%。<sup>[9](https://www.sonatype.com/whitepapers/the-persistence-of-open-source-vulnerabilities)</sup> Sonatype 将这种持续存在归因于版本被固定在构建文件中并在服务之间复制，导致只有在数据泄露、合规审计或服务中断迫使人们处理这一问题时，才会重新审视这些选择。<sup>[15](https://www.itpro.com/software/open-source/a-concerning-number-of-log4j-downloads-are-still-vulnerable-four-years-on)</sup>

## 当前版本

Log4j 2 仍处于活跃开发中。版本策略仅将最新大版本的最新小版本保持在活跃维护中，接受缺陷和安全修复，但稳定分支不接受新功能。<sup>[18](https://logging.apache.org/log4j/2.x/versioning.html)</sup> Java 支持范围随时间收窄：Log4j 2.3.2 是最后一个支持 Java 6 的版本，Log4j 2.12.4 是最后一个支持 Java 7 的版本，当前版本要求 Java 8。<sup>[7](https://logging.apache.org/log4j/2.x/release-notes.html)</sup>

下一个大版本线 Log4j 3 处于测试阶段。2024 年 11 月 9 日发布的 3.0.0-beta3 完成了模块化进程，将脚本、JNDI、JPA 和 JMS 等可选组件移入各自的模块，从而使带有不需要行为的 jar 直接缺席，而非通过系统属性禁用；插件现在通过 Java 的 ServiceLoader 定位，Log4j 1 到 2 的桥接模块已被移除。<sup>[8](https://logging.apache.org/log4j/3.x/release-notes.html)</sup> 每个 Log4j 构件现在都附带[软件物料清单](https://www.edgepedia.org/zh/software-bill-of-materials)，发布构件的可复现性由 [Reproducible Builds](https://www.edgepedia.org/zh/reproducible-builds) 项目针对 Maven Central 独立验证。<sup>[19](https://logging.apache.org/log4j/3.x/download.html)</sup>

## References

1. [Apache Logging Services Project Announces Log4j 1 End-Of-Life; Recommends Upgrade to Log4j 2, The ASF Blog (August 2015)](https://news.apache.org/foundation/entry/apache_logging_services_project_announces)
2. [Apache Log4j project home](https://logging.apache.org/log4j/2.x/)
3. [Log4j project history, Apache Logging Services](https://svn.apache.org/repos/asf/logging/log4j/tags/TAG_CHAINSAW2_MOVE/docs/history.html)
4. [2024 State of the Java Ecosystem, New Relic](https://newrelic.com/sites/default/files/2024-05/new-relic-state-of-the-java-ecosystem-2024-05-29.pdf)
5. [Joint Cybersecurity Advisory: Mitigating Log4Shell and Other Log4j-Related Vulnerabilities (AA21-356A), CISA and partner agencies](https://www.cyber.gov.au/sites/default/files/2023-02/Joint%20Cyber%20Security%20Advisory%20Mitigating%20Log4Shell%20and%20Other%20Log4j-Related%20Vulnerabilities_0.pdf)
6. [Apache Log4j Security Vulnerabilities, Apache Logging Services](https://logging.apache.org/security.html?trk=article-ssr-frontend-pulse_little-text-block)
7. [Log4j 2 release notes, Apache Logging Services](https://logging.apache.org/log4j/2.x/release-notes.html)
8. [Log4j 3 release notes, Apache Logging Services](https://logging.apache.org/log4j/3.x/release-notes.html)
9. [Open Source Vulnerabilities: Persistent Risks and Insights, Sonatype](https://www.sonatype.com/whitepapers/the-persistence-of-open-source-vulnerabilities)
10. [Apache Logging Services Celebrates 20th Anniversary of Log4j, The ASF Blog (December 2023)](https://news.apache.org/foundation/entry/apache-logging-services-celebrates-20th-anniversary-of-log4j)
11. [Apache Log4j 1 repository statement reconfirming EOL, Apache Logging Services](https://apache.googlesource.com/logging-log4j1/)
12. [Migrating from Log4j 1, Apache Log4j documentation](https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html)
13. [Architecture, Apache Log4j 2 manual](https://logging.apache.org/log4j/2.x/manual/architecture.html)
14. [Performance, Apache Log4j 2 manual](https://logging.apache.org/log4j/2.x/manual/performance.html)
15. [A concerning number of Log4j downloads are still vulnerable four years on, IT Pro](https://www.itpro.com/software/open-source/a-concerning-number-of-log4j-downloads-are-still-vulnerable-four-years-on)
16. [One year later, the widespread Log4j threat lingers on, Axios](https://www.axios.com/2022/12/09/log4j-year-anniversary-cybersecurity)
17. [What is the Log4Shell vulnerability?, IT Pro](https://www.itpro.com/security/zero-day-exploit/361819/what-is-log4shell-log4j-vulnerability)
18. [Versioning and maintenance policy, Apache Log4j](https://logging.apache.org/log4j/2.x/versioning.html)
19. [Download, Apache Log4j 3](https://logging.apache.org/log4j/3.x/download.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Mobile and platform frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/log4j

English original: https://www.edgepedia.org/log4j
