| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <title>Migrating from Log4j 1 :: Apache Log4j</title> |
| <link rel="canonical" href="https://logging.apache.org/log4j/2.x/migrate-from-log4j1.html"> |
| <meta name="generator" content="Antora 3.2.0-alpha.8"> |
| <link rel="stylesheet" href="./_/css/site.css"> |
| <link rel="icon" href="./_/../_images/favicon.ico" type="image/x-icon"> |
| <!-- `@asciidoctor/tabs` extension styles --> |
| <link rel="stylesheet" href="./_/css/vendor/tabs.css"> |
| <style> |
| /* Swap colors of `IMPORTANT` and `WARNING` blocks */ |
| .doc .admonitionblock.important .icon { background-color: #f70; } |
| .doc .admonitionblock.warning .icon { background-color: #e40046; } |
| /* Default `h4`, `h5`, and `h6` are smaller than the normal text, fix header font sizing: */ |
| .doc h1 { font-size: 1.9rem; } |
| .doc h2 { font-size: 1.7rem; } |
| .doc h3 { font-size: 1.5rem; font-weight: 400; } |
| .doc h4 { font-size: 1.3rem; font-weight: 500; } |
| .doc h5 { font-size: 1.1rem; font-weight: 500; text-decoration: underline; } |
| .doc h6 { font-size: 0.9rem; font-weight: 500; text-decoration: underline; } |
| /* Default `code`, `pre`, and `.colist` (source code annotations) fonts are too big, adjust them: */ |
| .doc .colist>table code, .doc p code, .doc thead code { font-size: 0.8em; } |
| .doc pre { font-size: 0.7rem; } |
| .doc .colist { font-size: 0.75rem; } |
| /* Make links more visible: */ |
| .doc a { text-decoration: underline; } |
| .doc a code { text-decoration: underline; color: #1565c0; } |
| /* Tab header fonts aren't rendered good, adjusting the font weight: */ |
| .tablist > ul li { font-weight: 500; } |
| /* `page-toclevels` greater than 4 are not supported by Antora UI, patching it: */ |
| .toc .toc-menu li[data-level="4"] a { |
| padding-left: 2.75rem; |
| } |
| /* Replace the default highlight.js color for strings from red (unnecessarily signaling something negative) to green: */ |
| .hljs-string { |
| color: #0f8532; |
| } |
| </style> |
| </head> |
| <body class="article"> |
| <header class="header"> |
| <nav class="navbar"> |
| <div class="navbar-brand"> |
| <div class="navbar-item"> |
| <img src="./_/../_images/logo-small-white.png" alt="Apache Log4j"/> |
| </div> |
| </div> |
| <div id="topbar-nav" class="navbar-menu"> |
| <div class="navbar-end"> |
| <a class="navbar-item" href="https://logging.apache.org">a subproject of <strong>Apache Logging Services</strong></a> |
| </div> |
| </div> |
| </nav> |
| </header> |
| <div class="body"> |
| <div class="nav-container" data-component="ROOT" data-version=""> |
| <aside class="nav"> |
| <div class="panels"> |
| <div class="nav-panel-menu is-active" data-panel="menu"> |
| <nav class="nav-menu"> |
| <button class="nav-menu-toggle" aria-label="Toggle expand/collapse all" style="display: none"></button> |
| <h3 class="title"><a href="index.html">Home</a></h3> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="0"> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="download.html">Download</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="release-notes.html">Release notes</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="https://logging.apache.org/support.html">Support</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="https://logging.apache.org/security.html">Security</a> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="0"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/index.html">Manual</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="manual/getting-started.html">Getting started</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="manual/installation.html">Installation</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/api.html">API</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="2"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/api.html#loggers">Loggers</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/eventlogging.html">Event Logger</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/simple-logger.html">Simple Logger</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/status-logger.html">Status Logger</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="manual/logbuilder.html">Fluent API</a> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/api.html#fish-tagging">Fish tagging</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/customloglevels.html">Levels</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/markers.html">Markers</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/thread-context.html">Thread Context</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="manual/messages.html">Messages</a> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="manual/flowtracing.html">Flow Tracing</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/implementation.html">Implementation</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="2"> |
| <a class="nav-link" href="manual/architecture.html">Architecture</a> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/config-intro.html">Configuration</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/configuration.html">Configuration file</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/systemproperties.html">Configuration properties</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/customconfig.html">Programmatic configuration</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/appenders.html">Appenders</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/file.html">File appenders</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/rolling-file.html">Rolling file appenders</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/database.html">Database appenders</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/network.html">Network Appenders</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/message-queue.html">Message queue appenders</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/appenders/delegating.html">Delegating Appenders</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/layouts.html">Layouts</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/json-template-layout.html">JSON Template Layout</a> |
| </li> |
| <li class="nav-item" data-depth="4"> |
| <a class="nav-link" href="manual/pattern-layout.html">Pattern Layout</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/lookups.html">Lookups</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/filters.html">Filters</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/scripts.html">Scripts</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/jmx.html">JMX</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/extending.html">Extending</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/plugins.html">Plugins</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="2"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="manual/performance.html">Performance</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/async.html">Asynchronous loggers</a> |
| </li> |
| <li class="nav-item" data-depth="3"> |
| <a class="nav-link" href="manual/garbagefree.html">Garbage-free logging</a> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="0"> |
| <button class="nav-item-toggle"></button> |
| <span class="nav-text">References</span> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="plugin-reference.html">Plugin reference</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="javadoc.html">Java API reference</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="0"> |
| <button class="nav-item-toggle"></button> |
| <span class="nav-text">Resources</span> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="faq.html">F.A.Q.</a> |
| </li> |
| <li class="nav-item is-current-page" data-depth="1"> |
| <a class="nav-link" href="migrate-from-log4j1.html">Migrating from Log4j 1</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="migrate-from-logback.html">Migrating from Logback</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="migrate-from-slf4j.html">Migrating from SLF4J</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="graalvm.html">Building GraalVM native images</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="hibernate.html">Integrating with Hibernate</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="jakarta.html">Integrating with Jakarta EE</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="soa.html">Integrating with service-oriented architectures</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="development.html">Development</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="0"> |
| <button class="nav-item-toggle"></button> |
| <a class="nav-link" href="components.html">Components</a> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="log4j-iostreams.html">Log4j IOStreams</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="log4j-spring-boot.html">Log4j Spring Boot Support</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="log4j-spring-cloud-config-client.html">Log4j Spring Cloud Configuration</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="log4j-jul.html">JUL-to-Log4j bridge</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="log4j-to-jul.html">Log4j-to-JUL bridge</a> |
| </li> |
| </ul> |
| </li> |
| <li class="nav-item" data-depth="0"> |
| <button class="nav-item-toggle"></button> |
| <span class="nav-text">Related projects</span> |
| <ul class="nav-list"> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/jakarta/index.html">Log4j Jakarta EE</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/jmx-gui/index.html">Log4j JMX GUI</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/kotlin/index.html">Log4j Kotlin</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/scala/index.html">Log4j Scala</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/tools/index.html">Log4j Tools</a> |
| </li> |
| <li class="nav-item" data-depth="1"> |
| <a class="nav-link" href="https://logging.apache.org/log4j/transform/index.html">Log4j Transformation Tools</a> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </nav> |
| </div> |
| </div> |
| </aside> |
| </div> |
| <main class="article"> |
| <div class="toolbar" role="navigation"> |
| <button class="nav-toggle"></button> |
| <a href="index.html" class="home-link"></a> |
| <nav class="breadcrumbs" aria-label="breadcrumbs"> |
| <ul> |
| <li><a href="index.html">Home</a></li> |
| <li>Resources</li> |
| <li><a href="migrate-from-log4j1.html">Migrating from Log4j 1</a></li> |
| </ul> |
| </nav> |
| <div class="edit-this-page"><a href="https://github.com/apache/logging-log4j2/edit/2.x/src/site/antora/modules/ROOT/pages/migrate-from-log4j1.adoc">Edit this Page</a></div> |
| </div> |
| <div class="content"> |
| <aside class="toc sidebar" data-title="Contents" data-levels="4"> |
| <div class="toc-menu"></div> |
| </aside> |
| <article class="doc"> |
| <h1 class="page">Migrating from Log4j 1</h1> |
| <div id="preamble"> |
| <div class="sectionbody"> |
| <div class="admonitionblock warning"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-warning" title="Warning"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p><a href="http://logging.apache.org/log4j/1.x">Log4j 1</a> has <a href="https://news.apache.org/foundation/entry/apache_logging_services_project_announces">reached End of Life</a> in 2015, and is no longer supported. |
| Vulnerabilities reported after August 2015 against Log4j 1 are not checked and will not be fixed. |
| Users should <a href="#">upgrade to Log4j 2</a> to obtain security fixes.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="paragraph"> |
| <p>Since Log4j 2 has been rewritten from scratch, it introduces many breaking changes to its predecessor. |
| Most notably:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>It uses a new package namespace (<code>org.apache.logging.log4j</code>), whereas Log4j 1 used the <code>org.apache.log4j</code> namespace,</p> |
| </li> |
| <li> |
| <p>It features a |
| <a href="manual/api.html" class="xref page">logging API</a> |
| which is independent of its reference implementation,</p> |
| </li> |
| <li> |
| <p>It uses a new more flexible |
| <a href="manual/configuration.html" class="xref page">configuration file format</a>, |
| which is <strong>incompatible</strong> with the format used by Log4j 1.</p> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="prepare-to-migrate"><a class="anchor" href="#prepare-to-migrate"></a>Prepare to migrate</h2> |
| <div class="sectionbody"> |
| <div class="admonitionblock tip"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-tip" title="Tip"></i> |
| </td> |
| <td class="content"> |
| <div class="title">Are you a library developer?</div> |
| <div class="paragraph"> |
| <p>If you are developing a library, which functionality is not related to logging, you only need to rewrite the library to use Log4j 2 API. |
| Skip directly to <a href="#api-migration">Log4j 1 API migration</a>.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="paragraph"> |
| <p>In order to migrate your application to Log4j 2 you need to assess first how your application <strong>and</strong> its dependencies use Log4j 1. |
| While Log4j 1 didn’t have a formal split between a logging API and a logging backend, for the purpose of this guide, we’ll introduce the following split:</p> |
| </div> |
| <div id="log4j-1-api-methods" class="dlist"> |
| <dl> |
| <dt class="hdlist1">Log4j 1 API</dt> |
| <dd> |
| <div class="paragraph"> |
| <p>It is the part of the Log4j 1 library that produces log events, and it is the most commonly used. |
| The methods that are considered part of the Log4j 1 API are listed below:</p> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 1. Log4j 1 API methods</caption> |
| <colgroup> |
| <col style="width: 33.3333%;"> |
| <col style="width: 66.6667%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Class name</th> |
| <th class="tableblock halign-left valign-top">Methods</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/MDC.html">org.apache.log4j.MDC</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/NDC.html">org.apache.log4j.NDC</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Priority.html">org.apache.log4j.Priority</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Level.html">org.apache.log4j.Level</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Category.html">org.apache.log4j.Category</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods, except: methods for the |
| <a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/spi/AppenderAttachable.html">AppenderAttachable</a> interface, <code>callsAppenders</code> and <code>setLevel</code>.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Logger.html">org.apache.log4j.Logger</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Same as <code>Category</code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/LogManager.html">org.apache.log4j.LogManager</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">All methods</p></td> |
| </tr> |
| </tbody> |
| </table> |
| </dd> |
| <dt class="hdlist1">Log4j 1 Backend</dt> |
| <dd> |
| <div class="paragraph"> |
| <p>This is the part of the logging library that consumes log events, formats them and writes to their destination. |
| It also allows to configure Log4j 1 programmatically. |
| It is usually <strong>not used</strong> in code, since the recommended way to configure Log4j 1 is through a configuration file.</p> |
| </div> |
| </dd> |
| </dl> |
| </div> |
| <div class="admonitionblock tip"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-tip" title="Tip"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>While it is fairly simple to check which classes and methods in the <code>org.apache.log4j</code> package are used by your own application, the task is much more complex, when it comes to your application dependencies.</p> |
| </div> |
| <div class="paragraph"> |
| <p>All the libraries that use <strong>Log4j 1 API</strong> in their code must have a compile dependency on either |
| <a href="https://central.sonatype.com/artifact/log4j/log4j">log4j:log4j</a> |
| or its clone |
| <a href="https://central.sonatype.com/artifact/ch.qos.reload4j/reload4j">ch.qos.reload4j:reload4j</a>. |
| There are however misconfigured libraries that declare those dependencies, even if they don’t directly use Log4j 1 <strong>at all</strong>.</p> |
| </div> |
| <div class="paragraph"> |
| <p>To distinguish between libraries that use Log4j 1 and those that don’t, you can look for the presence of other logging APIs. If a library <strong>directly</strong> depends on:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/commons-logging/commons-logging"><code>commons-logging:commons-logging</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-api"><code>org.apache.logging.log4j:log4j-api</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/org.slf4j/slf4j-api"><code>org.slf4j:slf4j-api</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/org.jboss.logging/jboss-logging"><code>jboss-logging</code></a>,</p> |
| </li> |
| <li> |
| <p>other logging APIs,</p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>it is fair to assume that it uses those libraries <strong>instead</strong> of <strong>Log4j 1 API</strong>, even if it has a direct dependency on <code>log4j:log4j</code> or <code>ch.qos.reload4j:reload4j</code>.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="paragraph"> |
| <p>The following sections explain how to migrate from Log4j 1 to Log4j 2, depending on the way Log4j 1 is used in your application:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>if your application or one of its dependencies is coded against Log4j 1 API, see <a href="#api-migration">Log4j 1 API migration</a>,</p> |
| </li> |
| <li> |
| <p>if your application uses Log4j 1 only as logging backend, see <a href="#backend-migration">Log4j 1 Backend migration</a>.</p> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="api-migration"><a class="anchor" href="#api-migration"></a>Log4j 1 API migration</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>To migrate an application that uses Log4j 1 API as logging API, the recommended approach is to modify your code. |
| See <a href="#api-code-migration">Migrate code from Log4j 1 API to Log4j 2 API</a> for details.</p> |
| </div> |
| <div class="paragraph"> |
| <p>In the case one of your libraries uses Log4j 1 API or you can not modify your logging code at the moment, a Log4j 1 to Log4j 2 bridge is available. |
| See <a href="#api-use-bridge">Use Log4j 1 to Log4j 2 API bridge</a> for details.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="api-code-migration"><a class="anchor" href="#api-code-migration"></a>Migrate code from Log4j 1 API to Log4j 2 API</h3> |
| <div class="admonitionblock tip"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-tip" title="Tip"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>You can migrate your code from Log4j 1 to Log4j 2 automatically, by using the |
| <a href="https://docs.openrewrite.org/recipes/java/logging/log4j/log4j1tolog4j2">Log4j1ToLog4j2</a> OpenRewrite recipe. |
| See |
| <a href="https://docs.openrewrite.org/recipes/java/logging/log4j/log4j1tolog4j2">OpenRewrite site</a> for more details.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="paragraph"> |
| <p>Except the change in the package name from <code>org.apache.log4j</code> to <code>org.apache.logging.log4j</code>, most of the class and method names in Log4j 2 API are inherited from Log4j 1 API.</p> |
| </div> |
| <div class="paragraph"> |
| <p>In order to migrate you code, you need to:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>modify the imports and types used by your application, according to the following table:</p> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 2. Migration of types from Log4j 1 to Log4j 2</caption> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Migrate Log4j 1 type</th> |
| <th class="tableblock halign-left valign-top">to Log4j type</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/MDC.html">org.apache.log4j.MDC</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/ThreadContext.html">org.apache.logging.log4j.ThreadContext</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/NDC.html">org.apache.log4j.NDC</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/ThreadContext.html">org.apache.logging.log4j.ThreadContext</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Priority.html">org.apache.log4j.Priority</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/Level.html">org.apache.logging.log4j.Level</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Level.html">org.apache.log4j.Level</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/Level.html">org.apache.logging.log4j.Level</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Category.html">org.apache.log4j.Category</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/Logger.html">org.apache.logging.log4j.Logger</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Logger.html">org.apache.log4j.Logger</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/Logger.html">org.apache.logging.log4j.Logger</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/LogManager.html">org.apache.log4j.LogManager</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/LogManager.html">org.apache.logging.log4j.LogManager</a></code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| </li> |
| <li> |
| <p>Some Log4j 1 methods were renamed or moved to a different class. |
| Therefore, you need to replace the following methods with their Log4j 2 API equivalents:</p> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 3. Migration of methods from Log4j 1 to Log4j 2</caption> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Migrate Log4j 1 method</th> |
| <th class="tableblock halign-left valign-top">to Log4j method</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Logger.html#getLogger(java.lang.String)">Logger.getLogger()</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/LogManager.html#getLogger()">LogManager.getLogger()</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Logger.html#getRootLogger()">Logger.getRootLogger()</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/LogManager.html#getRootLogger()">LogManager.getRootLogger()</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Category.html#getEffectiveLevel()">Category.getEffectiveLevel()</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="javadoc/log4j-api/org/apache/logging/log4j/Logger.html#getLevel()">Logger.getLevel()</a></code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| </li> |
| <li> |
| <p>Finally, some methods need specific conversion rules to be applied:</p> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 4. Special Log4j 1 method migration rules</caption> |
| <colgroup> |
| <col style="width: 25%;"> |
| <col style="width: 75%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Method</th> |
| <th class="tableblock halign-left valign-top">Description</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/Logger.html#getLogger(java.lang.String,%20org.apache.log4j.spi.LoggerFactory)"><code>Logger.getLogger(String, LoggerFactory)</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Remove the <code>LoggerFactory</code> parameter and use one of Log4j 2’s other extension mechanisms.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/LogManager.html#shutdown()"><code>LogManager.shutdown()</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Since Log4j 2.6, an equivalent |
| <a href="javadoc/log4j-api/org/apache/logging/log4j/LogManager.html#shutdown()"><code>o.a.l.l.LogManager.shutdown()</code></a> |
| method can be used.</p> |
| <p class="tableblock">The utility of this method call depends upon the logging backend used with Log4j API.</p> |
| <p class="tableblock">The Log4j Core backend automatically adds a JVM shutdown hook on start up to perform any cleanups, so the <code>LogManager.shutdown()</code> call can be safely removed. |
| Starting in Log4j 2.1, you can also specify a custom |
| <a href="javadoc/log4j-core/org/apache/logging/log4j/core/util/ShutdownCallbackRegistry.html">ShutdownCallbackRegistry</a>.</p> |
| <p class="tableblock">See |
| <a href="manual/systemproperties.html#log4j2.shutdownCallbackRegistry" class="xref page">log4j2.shutdownCallbackRegistry</a> |
| for more details.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Non Log4j 1 API methods</p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Methods outside of those listed in <a href="#log4j-1-api-methods">[log4j-1-api-methods]</a>, such as <code>Logger.setLevel()</code> need to be replaced with a method |
| call specific to the logging backend used by the application.</p> |
| <p class="tableblock">Some third-party integrators, such as Spring Boot, provide utility methods that let you abstract the logging backend, e.g. the |
| <a href="https://docs.spring.io/spring-boot/api/java/org/springframework/boot/logging/LoggingSystem.html#setLogLevel(java.lang.String,org.springframework.boot.logging.LogLevel)"><code>LoggingSystem.setLogLevel()</code></a> |
| Spring Boot method.</p></td> |
| </tr> |
| </tbody> |
| </table> |
| </li> |
| </ul> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>To prevent a performance penalty from string concatenation in disabled log statements, Log4j 1 required the use of <code>is*Enabled()</code> guards:</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">if (LOGGER.isInfoEnabled()) { |
| LOGGER.info("Hello " + username + "!"); |
| }</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>Since Log4j 2 API introduces |
| <a href="manual/api.html#best-practice-concat" class="xref page">parameterized logging</a> |
| these guards are no longer necessary and the same statement can be rewritten as:</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">LOGGER.info("Hello {}!", username);</code></pre> |
| </div> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="api-use-bridge"><a class="anchor" href="#api-use-bridge"></a><a id="APICompatibility"></a>Use Log4j 1 to Log4j 2 API bridge</h3> |
| <div class="paragraph"> |
| <p>If you can not modify your application’s code or one of your dependencies is using |
| <a href="#log4j-1-api-methods">Log4j 1 API</a> |
| as logging API, you can delay the migration process by |
| <a href="#log4j-1-2-api-installation">installing the Log4j 1 to Log4j 2 bridge</a>.</p> |
| </div> |
| <div class="paragraph"> |
| <p>Since forwarding Log4j 1 API calls to Log4j 2 API calls is the basic functionality of the bridge, no further configuration is required from your part.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="backend-migration"><a class="anchor" href="#backend-migration"></a>Log4j 1 Backend migration</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>If your application uses Log4j 1 <strong>only</strong> as logging backend bound to another logging API, such as Apache Commons Logging (JCL) or SLF4J, you only need to:</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Configure all logging bridges to log to Log4j 2 API instead. |
| This can be done by replacing the following dependencies on your application’s runtime classpath:</p> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 5. Dependency migration from Log4j 1 to Log4j 2</caption> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Replace Log4j 1 dependency</th> |
| <th class="tableblock halign-left valign-top">with Log4j 2 dependency</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/log4j/log4j"><code>log4j:log4j</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-core"><code>org.apache.logging.log4j:log4j-core</code></a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/ch.qos.reload4j/reload4j"><code>ch.qos.reload4j:reload4j</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-core"><code>org.apache.logging.log4j:log4j-core</code></a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/commons-logging/commons-logging"><code>commons-logging:commons-logging</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">either upgrade to version 1.3.0 (or later)</p> |
| <p class="tableblock">or replace with |
| <a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl"><code>org.apache.logging.log4j:log4j-jcl</code></a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.slf4j/slf4j-log4j12"><code>org.slf4j:slf4j-log4j12</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl"><code>org.apache.logging.log4j:log4j-slf4j2-impl</code></a></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.slf4j/slf4j-reload4j"><code>org.slf4j:slf4j-reload4j</code></a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl"><code>org.apache.logging.log4j:log4j-slf4j2-impl</code></a></p></td> |
| </tr> |
| </tbody> |
| </table> |
| <div class="paragraph"> |
| <p>See <a href="manual/installation.html" class="xref page">Installation</a> for more details.</p> |
| </div> |
| </li> |
| <li> |
| <p>Convert your configuration files from the Log4j 1 to the Log4j 2 configuration format. |
| See <a href="#configuration-file-migration">Log4j 1 Configuration file migration</a> below for more details.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="sect2"> |
| <h3 id="migrate-custom-components"><a class="anchor" href="#migrate-custom-components"></a>Migrate Log4j 1 custom components</h3> |
| <div class="paragraph"> |
| <p>Since Log4j 1 offered a limited amount of appenders and layouts, over the years users implemented many <strong>custom</strong> components that offered additional features. |
| If you are currently using a custom Log4j 1 component you should proceed as follows:</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Log4j 2 provides many improvements to Log4j 1 components and many new components. |
| Check if the feature offered by your custom component is not already available in Log4j 2. |
| If you can not find the feature, ask on our <a href="/support.html">support channels</a>.</p> |
| </li> |
| <li> |
| <p>Since Log4j 2.17.2, the Log4j 1 to Log4j 2 bridge has a limited support for using native Log4j 1 appenders and layouts. |
| Native Log4j 1 components can only be configured using Log4j 1 configuration files (see <a href="#ConfigurationCompatibility">Use Log4j 1 to Log4j 2 bridge</a>) |
| and require the <a href="#log4j-1-2-api-installation">installation of the Log4j 1 to Log4j 2 bridge</a>.</p> |
| <div class="admonitionblock warning"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-warning" title="Warning"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>Mixing Log4j 1 and Log4j 2 components will most certainly reduce the performance of the logging system.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </li> |
| <li> |
| <p>If your Log4j 1 native component is not supported by the Log4j 1 to Log4j 2 bridge, we suggest to rewrite it directly as Log4j 2 component. |
| See <a href="manual/extending.html" class="xref page">Extending</a> for more details.</p> |
| </li> |
| </ol> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="configuration-file-migration"><a class="anchor" href="#configuration-file-migration"></a>Log4j 1 Configuration file migration</h2> |
| <div class="sectionbody"> |
| <div class="sect2"> |
| <h3 id="Log4j2ConfigurationFormat"><a class="anchor" href="#Log4j2ConfigurationFormat"></a>Convert configuration file from Log4j 1 to Log4j 2</h3> |
| <div class="paragraph"> |
| <p>Although the Log4j 2 configuration syntax is different from that of Log4j 1, most, if not all, of the same functionality is available.</p> |
| </div> |
| <div class="admonitionblock tip"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-tip" title="Tip"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>The <code>log4j-1.2-api</code> bridge contains a small utility that converts <code>log4j.properties</code> files into <code>log4j2.xml</code> file. |
| In order to use it you need to:</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Download the <code>log4j-api</code>, <code>log4j-core</code> and <code>log4j-1.2-api</code> artifacts. |
| To retrieve them all at once, see the <a href="download.html" class="xref page">Download</a> page.</p> |
| </li> |
| <li> |
| <p>Set the <code>CLASSPATH</code> environment variable to contain the artifacts mentioned above.</p> |
| </li> |
| <li> |
| <p>Run</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-shell hljs" data-lang="shell">java org.apache.log4j.config.Log4j1ConfigurationConverter \ |
| --in log4j.properties --out log4j2.xml</code></pre> |
| </div> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="sect3"> |
| <h4 id="configuration-interpolation"><a class="anchor" href="#configuration-interpolation"></a>Interpolation</h4> |
| <div class="paragraph"> |
| <p>Log4j 1 only supports interpolation using system properties and properties from the <code>log4j.properties</code> file using the <code>${foo}</code> syntax. |
| Log4j 2 extended this mechanism, by introducing pluggable <a href="manual/lookups.html" class="xref page">Lookups</a>.</p> |
| </div> |
| <div class="paragraph"> |
| <p>In order to convert a Log4j 1 configuration file that uses interpolation to a Log4j 2 configuration file, replace all occurrences of <code>${foo}</code> with <code>${sys:foo}</code>.</p> |
| </div> |
| </div> |
| <div class="sect3"> |
| <h4 id="configuration-appenders"><a class="anchor" href="#configuration-appenders"></a>Appenders</h4> |
| <div class="paragraph"> |
| <p>Log4j 2 contains an equivalent for most Log4j 1 appenders:</p> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 6. Log4j 2 equivalents of Log4j 1 appenders</caption> |
| <colgroup> |
| <col style="width: 25%;"> |
| <col style="width: 25%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Log4j 1 appender</th> |
| <th class="tableblock halign-left valign-top">Log4j 2 equivalent</th> |
| <th class="tableblock halign-left valign-top">Notes</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/AsyncAppender.html">org.apache.log4j.AsyncAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/delegating.html#AsyncAppender" class="xref page">Async</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/ConsoleAppender.html">org.apache.log4j.ConsoleAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders.html#ConsoleAppender" class="xref page">Console</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/DailyRollingFileAppender.html">org.apache.log4j.DailyRollingFileAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/rolling-file.html#RollingFileAppender" class="xref page">RollingFile</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">See <a href="#rolling-file-conversion">additional steps below</a>.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/FileAppender.html">org.apache.log4j.FileAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/file.html#FileAppender" class="xref page">File</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/RollingFileAppender.html">org.apache.log4j.RollingFileAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/rolling-file.html#RollingFileAppender" class="xref page">RollingFile</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">See <a href="#rolling-file-conversion">additional steps below</a>.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/jdbc/JDBCAppender.html">org.apache.log4j.jdbc.JDBCAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/database.html#JdbcAppender" class="xref page">JDBC</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/net/JMSAppender.html">org.apache.log4j.net.JMSAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/message-queue.html#JmsAppender" class="xref page">JMS</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/net/SocketAppender.html">org.apache.log4j.net.SocketAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/network.html#SocketAppender" class="xref page">Socket</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/net/SMTPAppender.html">org.apache.log4j.net.SMTPAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/network.html#SmtpAppender" class="xref page">SMTP</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/net/SyslogAppender.html">org.apache.log4j.net.SyslogAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/network.html#SyslogAppender" class="xref page">Syslog</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Does not support custom layouts.</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/rewrite/RewriteAppender.html">org.apache.log4j.rewrite.RewriteAppender</a></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders/delegating.html#RewriteAppender" class="xref page">Rewrite</a></code></p></td> |
| <td class="tableblock halign-left valign-top"></td> |
| </tr> |
| </tbody> |
| </table> |
| <div id="rolling-file-conversion" class="paragraph"> |
| <p>The rolling file appender in Log4j 2 is based on the |
| <a href="/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html">org.apache.log4j.rolling.RollingFileAppender</a> |
| from |
| <a href="/log4j/extras/index.html">Apache™ Extras for Apache Log4j®</a> |
| and additional care must be taken to convert the Log4j 1 rolling appenders to their Log4j 2 equivalent:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>Log4j 2 by default uses a different strategy to determine the index of the archived log files. |
| Log4j 1 always rolls the current log file (e.g. <code>app.log</code>) to a log file with index <code>1</code> (e.g. <code>app.log.1</code>). |
| Log4j 2 on the other hand uses the first available index (e.g. <code>app.log.42</code> if files <code>app.log.1</code> thru <code>app.log.41</code> already exist).</p> |
| <div class="paragraph"> |
| <p>To use the same algorithm to determine the index of the logged file in Log4j 1 and Log4j 2, you need to configure the <code>fileIndex</code> attribute of the |
| <a href="manual/appenders/rolling-file.html#DefaultRolloverStrategy" class="xref page">default rollover strategy</a> to <code>min</code>.</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><DefaultRolloverStrategy fileIndex="min"/></code></pre> |
| </div> |
| </div> |
| </li> |
| <li> |
| <p>The two rolling file appenders available in Log4j 1, use an implicit file pattern and triggering policy for the archived log files. |
| If the current log file is called <code>app.log</code>, you need to configure the Log4j 2 rolling file appender with the following <code>filePattern</code> and triggering policy configuration options:</p> |
| </li> |
| </ul> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <caption class="title">Table 7. Rolling file appender conversion</caption> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 25%;"> |
| <col style="width: 25%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Log4j 1 appender</th> |
| <th class="tableblock halign-left valign-top">Log4j 2 <code>filePattern</code></th> |
| <th class="tableblock halign-left valign-top">Log4j 2 triggering policy</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>org.apache.log4j.DailyRollingFileAppender</code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>app.%d{YYYY-MM-dd}</code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders.html" class="xref page">TimeBasedTriggeringPolicy</a></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>org.apache.log4j.RollingFileAppender</code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>app.%i</code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><a href="manual/appenders.html" class="xref page">SizeBasedTriggeringPolicy</a></code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| <div class="sect3"> |
| <h4 id="configuration-layouts"><a class="anchor" href="#configuration-layouts"></a>Layouts</h4> |
| <div class="paragraph"> |
| <p>Log4j 1 layouts can be converted to Log4j 2 layouts using the following conversion rules:</p> |
| </div> |
| <div class="dlist"> |
| <dl> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/EnhancedPatternLayout.html">org.apache.log4j.EnhancedPatternLayout</a></dt> |
| <dd> |
| <p>can be converted to a |
| <a href="manual/layouts.html#PatternLayout" class="xref page"><code>PatternLayout</code></a> |
| that uses the same pattern,</p> |
| </dd> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/HTMLLayout.html">org.apache.log4j.HTMLLayout</a></dt> |
| <dd> |
| <p>can be converted to an |
| <a href="manual/layouts.html#HTMLLayout" class="xref page"><code>HtmlLayout</code></a>.</p> |
| </dd> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/PatternLayout.html">org.apache.log4j.PatternLayout</a></dt> |
| <dd> |
| <p>can be converted to a |
| <a href="manual/layouts.html#PatternLayout" class="xref page"><code>PatternLayout</code></a> |
| that uses the same pattern,</p> |
| </dd> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/SimpleLayout.html">org.apache.log4j.SimpleLayout</a></dt> |
| <dd> |
| <p>can be converted to a |
| <a href="manual/layouts.html#PatternLayout" class="xref page"><code>PatternLayout</code></a> |
| that uses the <code>%p - %m%n</code> pattern,</p> |
| </dd> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/TTCCLayout.html">org.apache.log4j.TTCCLayout</a></dt> |
| <dd> |
| <p>can be converted to a |
| <a href="manual/layouts.html#PatternLayout" class="xref page"><code>PatternLayout</code></a> |
| that uses the <code>%r [%t] %p %c %notEmpty{%x }- %m%n</code> pattern,</p> |
| </dd> |
| <dt class="hdlist1"><a href="https://logging.apache.org/log4j/1.x/apidocs/org/apache/log4j/xml/XMLLayout.html">org.apache.log4j.xml.XMLLayout</a></dt> |
| <dd> |
| <p>does not have an exact equivalent in Log4j 2 Core. |
| If backward compatibility is required, you can <a href="#log4j-1-2-api-installation">install the Log4j 1 to Log4j 2 bridge</a> and use the |
| <a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-layout-Log4j1XmlLayout" class="xref page"><code>Log4j1XmlLayout</code></a> |
| plugin.</p> |
| </dd> |
| </dl> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>The formatting of the <code>%p</code> (when used with custom levels), <code>%x</code> and <code>%X</code> pattern converters slightly changed between Log4j 1 and Log4j 2. |
| If an exact backward compatibility is required, you need to <a href="#log4j-1-2-api-installation">install the Log4j 1 to Log4j 2 bridge</a> and use the following extended patterns:</p> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Log4j 1 pattern</th> |
| <th class="tableblock halign-left valign-top">Log4j 1 to Log4j 2 bridge pattern</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%p</code></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%v1Level</code></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%x</code></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%ndc</code></code></p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%X</code></code></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code><code>%properties</code></code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="ConfigurationCompatibility"><a class="anchor" href="#ConfigurationCompatibility"></a>Use Log4j 1 to Log4j 2 bridge</h3> |
| <div class="paragraph"> |
| <p>If you cannot convert your configuration files from Log4j 1 to Log4j 2, the Log4j 1 to Log4j 2 bridge can convert your configuration files at runtime. |
| To use this feature, you need to <a href="#log4j-1-2-api-installation">Install the Log4j 1 to Log4j 2 bridge</a> and set one of the following configuration properties:</p> |
| </div> |
| <div class="sect3"> |
| <h4 id="log4j1.compatibility"><a class="anchor" href="#log4j1.compatibility"></a><code>log4j1.compatibility</code></h4> |
| <table class="tableblock frame-all grid-all stretch"> |
| <colgroup> |
| <col style="width: 16.6666%;"> |
| <col style="width: 83.3334%;"> |
| </colgroup> |
| <tbody> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Env. variable</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">LOG4J_COMPATIBILITY</p></td> |
| </tr> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Type</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>boolean</code></p></td> |
| </tr> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Default value</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>false</code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| <div class="paragraph"> |
| <p>If set to <code>true</code>, Log4j 2 will:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>Scan the classpath to find Log4j 1 configuration files in the following standard locations:</p> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><code>log4j-test.properties</code>,</p> |
| </li> |
| <li> |
| <p><code>log4j-test.xml</code>,</p> |
| </li> |
| <li> |
| <p><code>log4j.properties</code>,</p> |
| </li> |
| <li> |
| <p><code>log4j.xml</code>.</p> |
| </li> |
| </ul> |
| </div> |
| </li> |
| <li> |
| <p>(since <code>2.24.0</code>) Enable the usage of the <code>o.a.log4j.PropertyConfigurator</code> and <code>o.a.log4j.xml.DOMConfigurator</code> classes in your code.</p> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <div class="sect3"> |
| <h4 id="log4j.configuration"><a class="anchor" href="#log4j.configuration"></a><code>log4j.configuration</code></h4> |
| <table class="tableblock frame-all grid-all stretch"> |
| <colgroup> |
| <col style="width: 16.6666%;"> |
| <col style="width: 83.3334%;"> |
| </colgroup> |
| <tbody> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Env. variable</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">LOG4J_CONFIGURATION_FILE</p></td> |
| </tr> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Type</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html">Path</a> |
| or |
| <a href="https://docs.oracle.com/javase/8/docs/api/java/net/URI.html">URI</a></p></td> |
| </tr> |
| <tr> |
| <th class="tableblock halign-left valign-top"><p class="tableblock">Default value</p></th> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><code>null</code></p></td> |
| </tr> |
| </tbody> |
| </table> |
| <div class="paragraph"> |
| <p>If not <code>null</code>, Log4j 2 will try to retrieve a <strong>Log4j 1</strong> configuration file from the given location. |
| The configuration file name must end in <code>.properties</code> (Log4j 1 properties format) or <code>.xml</code> (Log4j 1 XML format). |
| See also |
| <a href="##limitations-of-the-log4j-1-x-bridge">limitations of Log4j 1 configuration compatibility layer</a>.</p> |
| </div> |
| </div> |
| <div class="sect3"> |
| <h4 id="limitations-of-the-log4j-1-x-bridge"><a class="anchor" href="#limitations-of-the-log4j-1-x-bridge"></a><a id="Log4j1.2BridgeLimitations"></a>Limitations of runtime configuration conversion</h4> |
| <div class="paragraph"> |
| <p>The support for Log4j 1 configuration files uses Log4j 2 Core plugin system and can be extended by implementing a plugin of type |
| <code>org.apache.log4j.builders.Builder</code>.</p> |
| </div> |
| <div class="paragraph"> |
| <p>The Log4j 1 to Log4j 2 bridge supports out-of-the-box the following |
| <a href="https://logging.apache.org/log4j/1.x/index.html">Log4j 1</a> |
| and |
| <a href="/log4j/extras/index.html">Apache Extras for Log4j 1</a> |
| components:</p> |
| </div> |
| <div class="dlist"> |
| <dl> |
| <dt class="hdlist1">Appenders</dt> |
| <dd> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-AsyncAppenderBuilder" class="xref page"><code>org.apache.log4j.AsyncAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-ConsoleAppenderBuilder" class="xref page"><code>org.apache.log4j.ConsoleAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-DailyRollingFileAppenderBuilder" class="xref page"><code>org.apache.log4j.DailyRollingFileAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-FileAppenderBuilder" class="xref page"><code>org.apache.log4j.FileAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-RollingFileAppenderBuilder" class="xref page"><code>org.apache.log4j.RollingFileAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-SocketAppenderBuilder" class="xref page"><code>org.apache.log4j.net.SocketAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-SyslogAppenderBuilder" class="xref page"><code>org.apache.log4j.net.SyslogAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-RewriteAppenderBuilder" class="xref page"><code>org.apache.log4j.rewrite.RewriteAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-EnhancedRollingFileAppenderBuilder" class="xref page"><code>org.apache.log4j.rolling.RollingFileAppender</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-appender-NullAppenderBuilder" class="xref page"><code>org.apache.log4j.varia.NullAppender</code></a>.</p> |
| </li> |
| </ul> |
| </div> |
| </dd> |
| <dt class="hdlist1">Filters</dt> |
| <dd> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-filter-DenyAllFilterBuilder" class="xref page"><code>org.apache.log4j.varia.DenyAllFilter</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-filter-LevelMatchFilterBuilder" class="xref page"><code>org.apache.log4j.varia.LevelMatchFilter</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-filter-LevelRangeFilterBuilder" class="xref page"><code>org.apache.log4j.varia.LevelRangeFilter</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-filter-StringMatchFilterBuilder" class="xref page"><code>org.apache.log4j.varia.StringMatchFilter</code></a>.</p> |
| </li> |
| </ul> |
| </div> |
| </dd> |
| <dt class="hdlist1">Layouts</dt> |
| <dd> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-layout-HtmlLayoutBuilder" class="xref page"><code>org.apache.log4j.HTMLLayout</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-layout-PatternLayoutBuilder" class="xref page"><code>org.apache.log4j.PatternLayout</code></a> and <code>org.apache.log4j.EnhancedPatternLayout</code>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-layout-SimpleLayoutBuilder" class="xref page"><code>org.apache.log4j.SimpleLayout</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-layout-TTCCLayoutBuilder" class="xref page"><code>org.apache.log4j.TTCCLayout</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-layout-XmlLayoutBuilder" class="xref page"><code>org.apache.log4j.xml.XMLLayout</code></a>.</p> |
| </li> |
| </ul> |
| </div> |
| </dd> |
| <dt class="hdlist1">Triggering policies</dt> |
| <dd> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-rolling-CompositeTriggeringPolicyBuilder" class="xref page"><code>org.apache.log4j.rolling.CompositeTriggeringPolicy</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-rolling-SizeBasedTriggeringPolicyBuilder" class="xref page"><code>org.apache.log4j.rolling.SizeBasedTriggeringPolicy</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-rolling-TimeBasedRollingPolicyBuilder" class="xref page"><code>org.apache.log4j.rolling.TimeBasedRollingPolicy</code></a>.</p> |
| </li> |
| </ul> |
| </div> |
| </dd> |
| </dl> |
| </div> |
| <div class="paragraph"> |
| <p>See |
| <a href="plugin-reference.html#org-apache-logging-log4j_log4j-1-2-api_org-apache-log4j-builders-Builder" class="xref page">the plugin reference</a> for a complete list.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="option-1-use-the-log4j-1-x-bridge-log4j-1-2-api"><a class="anchor" href="#option-1-use-the-log4j-1-x-bridge-log4j-1-2-api"></a><a id="Log4j12Bridge"></a>Log4j 1 to Log4j 2 bridge</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>To help users with the migration process, a Log4j 1 to Log4j 2 bridge is available. |
| The bridge can fulfill four separate functions:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>It forwards all <a href="#log4j-1-api-methods">Log4j 1 API</a> method calls to the Log4j 2 API. |
| See <a href="#api-use-bridge">how to use the bridge for Log4j 1 API migration</a> for more details.</p> |
| </li> |
| <li> |
| <p>Since version 2.17.2 the bridge supports the usage of some components written for Log4j 1 inside Log4j 2 Core. |
| See <a href="#migrate-custom-components">how to use the bridge for Log4j 1 Backend migration</a> for more details.</p> |
| </li> |
| <li> |
| <p>It provides limited support for programmatic configuration of Log4j 2 Core, using Log4j 1 method calls. |
| This functionality requires the <a href="#log4j1.compatibility"><code>log4j1.compatibility</code> configuration property</a> to be set to <code>true</code>.</p> |
| </li> |
| <li> |
| <p>It provides a limited support for Log4j 1 configuration file formats. |
| See <a href="#ConfigurationCompatibility">how to use the bridge for Log4j 1 Configuration file migration</a> for more details.</p> |
| </li> |
| </ul> |
| </div> |
| <div class="sect2"> |
| <h3 id="log4j-1-2-api-installation"><a class="anchor" href="#log4j-1-2-api-installation"></a>Installation</h3> |
| <div class="paragraph"> |
| <p>Since the Log4j 1 to Log4j 2 Bridge <strong>replaces</strong> Log4j 1 classes, it is incompatible with the following artifacts:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/log4j/log4j"><code>log4j:log4j</code></a></p> |
| </li> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/ch.qos.reload4j/reload4j"><code>ch.qos.reload4j:reload4j</code></a>,</p> |
| </li> |
| <li> |
| <p><a href="https://central.sonatype.com/artifact/org.slf4j/log4j-over-slf4j"><code>org.slf4j:log4j-over-slf4j</code></a></p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>Before installing the bridge, you need to make sure that none of the artifacts above are present in your runtime classpath.</p> |
| </div> |
| <div class="paragraph"> |
| <p>To install the bridge, add the following dependency to your application:</p> |
| </div> |
| <div id="_tabs_1" class="openblock tabs is-sync is-loading"> |
| <div class="content"> |
| <div class="ulist tablist"> |
| <ul> |
| <li id="_tabs_1_maven" class="tab"> |
| <p>Maven</p> |
| </li> |
| <li id="_tabs_1_gradle" class="tab"> |
| <p>Gradle</p> |
| </li> |
| </ul> |
| </div> |
| <div id="_tabs_1_maven--panel" class="tabpanel" aria-labelledby="_tabs_1_maven"> |
| <div class="paragraph"> |
| <p>We assume you use <a href="components.html#log4j-bom" class="xref page"><code>log4j-bom</code></a> for dependency management.</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><dependency> |
| <groupId>org.apache.logging.log4j</groupId> |
| <artifactId>log4j-1.2-api</artifactId> |
| <scope>runtime</scope> |
| </dependency></code></pre> |
| </div> |
| </div> |
| </div> |
| <div id="_tabs_1_gradle--panel" class="tabpanel" aria-labelledby="_tabs_1_gradle"> |
| <div class="paragraph"> |
| <p>We assume you use <a href="components.html#log4j-bom" class="xref page"><code>log4j-bom</code></a> for dependency management.</p> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-groovy hljs" data-lang="groovy">runtimeOnly 'org.apache.logging.log4j:log4j-1.2-api'</code></pre> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="when-to-stop-using-the-log4j-1-x-bridge"><a class="anchor" href="#when-to-stop-using-the-log4j-1-x-bridge"></a>When to stop using the Log4j 1 to Log4j 2 bridge</h3> |
| <div class="paragraph"> |
| <p>The Log4j 1 to Log4j 2 bridge is not conceived as a long term solution. |
| Once:</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>you have migrated your logging code to use Log4j 2 API (see <a href="#api-code-migration">Migrate code from Log4j 1 API to Log4j 2 API</a>),</p> |
| </li> |
| <li> |
| <p>you have migrated your configuration files to use the Log4j 2 configuration format (see <a href="#configuration-file-migration">Log4j 1 Configuration file migration</a>),</p> |
| </li> |
| <li> |
| <p>upgraded all the dependencies that use Log4j 1 directly to newer versions that use a proper logging API (JCL, Log4j 2 API, JBoss logging, SLF4J),</p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>the bridge is no longer necessary and should be removed.</p> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| <div class="paragraph"> |
| <p>The separation of logging APIs from logging implementations started in 2002, with the release of |
| <a href="https://commons.apache.org/proper/commons-logging/">Apache Commons Logging</a> (formerly known as Jakarta Commons Logging).</p> |
| </div> |
| <div class="paragraph"> |
| <p>We are unaware of any <strong>maintained</strong> library that is currently using Log4j 1. |
| However, if this is your case, please contact the library maintainer and ask them to migrate to one of the available logging APIs.</p> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| </div> |
| </div> |
| </article> |
| </div> |
| </main> |
| </div> |
| <footer class="footer"> |
| <p> |
| Copyright © 1999-2025 <a href="https://www.apache.org/">The Apache Software Foundation</a>. |
| Licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache Software License, Version 2.0</a>. |
| Please read our <a href="https://privacy.apache.org/policies/privacy-policy-public.html">privacy policy</a>. |
| </p> |
| <p> |
| Apache, Log4j, and the Apache feather logo are trademarks or registered trademarks of The Apache Software Foundation. |
| Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| Other names may be trademarks of their respective owners. |
| </p> |
| </footer> |
| <!-- Matomo --> |
| <script> |
| var _paq = window._paq = window._paq || []; |
| _paq.push(["disableCookies"]); |
| _paq.push(['trackPageView']); |
| _paq.push(['enableLinkTracking']); |
| (function() { |
| var u="https://analytics.apache.org/"; |
| _paq.push(['setTrackerUrl', u+'matomo.php']); |
| _paq.push(['setSiteId', '42']); |
| var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| })(); |
| </script> |
| <noscript><p><img src="https://analytics.apache.org/matomo.php?idsite=42&rec=1" style="border:0;" alt="" /></p></noscript> |
| <!-- End Matomo Code --> |
| <script id="site-script" src="./_/js/site.js" data-ui-root-path="./_"></script> |
| <script async src="./_/js/vendor/highlight.js"></script> |
| <!-- `@asciidoctor/tabs` extension scripts --> |
| <script async src="./_/js/vendor/tabs.js"></script> |
| </body> |
| </html> |