Migrating from Log4j 1 to Log4j 2
The logging framework in NexJ CRM has been upgraded from Log4j version 1 to Log4j version 2 in the 21.12.1.0 release. The syntax for configuring logging has changed, and the table below has been provided to assist with your transition. It is important to review each row item and apply the corresponding changes after upgrading.
Task | Log4j 1 syntax | Log4j 2 syntax |
---|---|---|
Setting the system property for the configuration file | Uses the following syntax:
TEXT
| Instead, use the following syntax:
TEXT
|
Setting the root logger | Uses the following syntax:
TEXT
| Instead, use the following syntax:
TEXT
|
Property substitution | Not applicable | If you have a system property called For example, you can use the following syntax:
TEXT
|
Automatic reconfiguration | When file reconfiguration was on or off in Log4j 1, it uses the following syntax:
CODE
| File reconfiguration in Log4j 2 is applied using the For example, you can set the interval to 60 seconds using the following syntax:
TEXT
|
Setting a logger | Uses the following syntax:
TEXT
Also, supports the following syntax:
TEXT
TEXT
| Instead, use the following syntax:
TEXT
For example, use:
TEXT
nexj-core-rpc is considered a single token for Log4j 2. The unique name used here is only an example. The developer configuring the logger may choose unique names as appropriate for their purpose.
Additional examples are provided below:
TEXT
TEXT
|
Setting an appender | Uses the following syntax to add an appender to the root logger:
TEXT
Uses the following syntax to define an appender:
TEXT
| To add an appender to the root logger, use the following syntax:
TEXT
To define an appender, use the following syntax:
TEXT
|
Setting a rolling file appender | Uses the following syntax to add an appender to the root logger (console appender is also added):
TEXT
Uses the following syntax to define the type:
TEXT
Uses the following syntax to set the pattern:
TEXT
Uses the following syntax to set the maximum file size for the log:
TEXT
Uses the following syntax to set the file location:
TEXT
Uses the following syntax to set the maximum number of rollover files:
TEXT
Supports the following additional options:
TEXT
| To add an appender to the root logger, use the following syntax:
TEXT
To define a type (and also set the name), use the following syntax:
TEXT
To set a pattern, use the following syntax:
TEXT
To set a max file size for the log, use the following syntax:
If you are adding one or more policies, the first line must be added.
TEXT
To set a file location, use the following syntax: filePattern is a new attribute, and it defines the look of the rollover file name.
TEXT
To set the max number of rollover files, use the following syntax:
TEXT
Supports the following additional options:
TEXT
The following code shows an example:
TEXT
|
The examples listed above include the “log4j.” prefix, which is used when you are setting system properties through VM arguments. When you are configuring properties using a properties file, you will need to omit this prefix to conform to the standard Log4j 2 syntax.
Additional references
For additional Log4j 2 information, see:
- Configuration with properties - https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
- Property substitution – https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
Troubleshooting
The common error shown below is caused by Log4j 2 attempting to parse a Log4j 1 property (for example, log4j.logger.nexj.core.rpc=INFO
). Log4j 2 expects a token “name” after nexj
and is unable to find it.
Exception in thread "Thread-0" java.lang.ExceptionInInitializerError
...
Caused by: nexj.core.util.UncheckedException: err.logger.initFailed
...
Caused by: org.apache.logging.log4j.core.config.ConfigurationException: No name attribute provided for Logger nexj
Logging configuration issues can be evaluated in more detail through the logs by setting the log4j.debug=true
property in the application environment file.