Migrating from Log4j 2
If you are migrating from Log4j 1, check the Migrating from Log4j 1 guide in the Log4j 2 manual. |
Since Log4j 3 uses Log4j 2 API as logging API, no code changes should be necessary for most users.
In order to migrate to Log4j 3, you only need to:
-
if you use configuration properties to fine tune the Log4j 3 backend, check your configuration properties,
-
if you used the Java properties configuration format, migrate your configuration file,
Runtime dependencies
Log4j 2 Core contains many features that depend on optional runtime dependencies. In order to comply with the Java Platform Module System and help users managing those dependencies, some features were moved to separate Maven modules. This change has the following advantages:
-
you don’t need to consult the documentation to find out, which third-party libraries are necessary to use a specific feature: adding the appropriate Log4j 3 artifact is enough.
-
Log4j 3 does not have optional dependencies, so third-party libraries used by Log4j will be upgraded automatically by your dependency management tool, when you upgrade Log4j.
The following log4j-core
features were moved to their own modules or were removed:
Log4j 2 feature | Log4j 3 module |
---|---|
removed without replacement [1] |
|
removed without replacement [1] |
|
replaced by
|
|
|
|
replaced by external
|
|
removed without replacement [1] |
|
removed without replacement [1] |
|
removed without replacement [1] |
|
removed without replacement [1] |
|
replaced by external
|
|
removed without replacement [1] |
|
removed without replacement [1] |
|
integrated into Spring Boot 3 |
Migrate configuration properties
In order to support per-logger context configuration properties, the configuration properties sub-system of Log4j has been rewritten and the naming convention of properties have changed. To help you with the migration process a backward compatibility system has been implemented, which translates Log4j 2 property names to their Log4j 3 equivalents.
If you use configuration properties to finely tune Log4j Core:
-
you are advised to migrate to the new Log4j 3 naming convention for configuration properties,
-
in the meantime you can continue to use the Log4j 2 naming convention introduced in Log4j 2.10,
-
other naming conventions, including variations in the case of property names are not supported.
Java properties configuration file format
The Java properties configuration file format in Log4j 3 has been rewritten to use
jackson-dataformat-properties
to convert Java properties into the native JSON configuration file format.
See
Jackson documentation
for more details on the conversion.
The only significant changes between the Log4j 2 and Log4j 3 properties format are:
-
As an alternative to using the
type
configuration attribute to specify the plugin type of a Log4j component, you can append the plugin type to the prefix of the parent component. For example, instead of:appender.0.type = Console appender.0.name = CONSOLE appender.1.type = File appender.1.name = MAIN appender.1.fileName = logs/main.log appender.2.type = File appender.2.name = DEBUG_LOG appender.2.fileName = logs/debug.log
you can write:
Appenders.Console.name = CONSOLE Appenders.File[1].name = MAIN Appenders.File[1].fileName = logs/main.log Appenders.File[2].name = DEBUG_LOG Appenders.File[2].fileName = logs/debug.log
-
The Java properties format specific quirks are no longer recognized, which means that:
-
The following direct children of
Configuration
need to use the same prefix as the other formats:-
You need to replace the
appender
prefix withAppenders
, -
You need to replace the
logger
prefix withLoggers
, -
You need to replace the
script
prefix withScripts
.
-
-
Properties that start with
property
, which are used for property substitution, need to be rewritten from:property.<key> = <value>
to
Properties.Property[<n>].key = <key> Properties.Property[<n>].value = <value>
where
<n>
is an increasing positive integer. -
Properties that start with
customLevel
, which are used to define custom levels, need to be rewritten from:customLevel.<name> = <intLevel>
to
CustomLevels.CustomLevel[<n>].name = <name> CustomLevels.CustomLevel[<n>].intLevel = <intLevel>
where
<n>
is an increasing positive integer. -
You need to replace the
rootLogger
prefix withLoggers.Root
. -
The shorthand notation:
rootLogger = INFO, APPENDER1, APPENDER2
must be rewritten into:
Loggers.Root.level = INFO Loggers.Root.AppenderRef[1] = APPENDER1 Loggers.Root.AppenderRef[2] = APPENDER2
-
All the prefixes of the form:
logger.<name>.appenderRef.<id>
where
<name>
and<id>
are arbitrary, must be rewritten toLoggers.Logger[<n>].AppenderRef[<m>]
where
<n>
and<m>
are increasing positive integers.
-