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: log4j.configuration | Instead, use the following syntax: log4j.configurationFile |
Setting the root logger | Uses the following syntax: log4j.rootLogger=INFO | Instead, use the following syntax: log4j.rootLogger.level=INFO |
Property substitution | Not applicable | If you have a system property called For example, you can use the following syntax: log4j.appender.rolling.fileName=${sys:log.location} |
Automatic reconfiguration | When file reconfiguration was on or off in Log4j 1, it uses the following syntax: log4j.configuration.watched=true
| File reconfiguration in Log4j 2 is applied using the For example, you can set the interval to 60 seconds using the following syntax: log4j.monitorInterval=60 |
Setting a logger | Uses the following syntax: log4j.logger.nexj.core.rpc=INFO Also, supports the following syntax: log4j.logger.nexj.core.container=WARN log4j.logger.nexj.core.rpc.Auditor=DEBUG | Instead, use the following syntax: log4j.logger.<unique name>.name = <fully qualified class name> log4j.logger.<unique name>.level = <level> For example, use: log4j.logger.nexj-core-rpc.name=nexj.core.rpc log4j.logger.nexj-core-rpc.level=INFO 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: log4j.logger.nexj-core-container.name=nexj.core.container log4j.logger.nexj-core-container.level=WARN log4j.logger.nexj‑core‑rpc‑tomcat‑Auditor.name=nexj.core.rpc.Auditor log4j.logger.nexj-core-rpc-tomcat-Auditor.level=DEBUG |
Setting an appender | Uses the following syntax to add an appender to the root logger: log4j.rootLogger=INFO, console Uses the following syntax to define an appender: log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d[%t]%‑5p%c‑%m%n | To add an appender to the root logger, use the following syntax: Log4j.rootLogger.appenderRef.stdout.ref=STDOUT To define an appender, use the following syntax: log4j.appender.console.type=Console log4j.appender.console.name=STDOUT log4j.appender.console.layout.type=PatternLayout log4j.appender.console.layout.pattern=%d[%t]%‑5p%c‑%m%n |
Setting a rolling file appender | Uses the following syntax to add an appender to the root logger (console appender is also added): log4j.rootLogger=INFO,console,R Uses the following syntax to define the type: log4j.appender.R=org.apache.log4j.RollingFileAppender Uses the following syntax to set the pattern: log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d[%t]% 5p%c %m%n Uses the following syntax to set the maximum file size for the log: log4j.appender.R.MaxFileSize=256MB Uses the following syntax to set the file location: log4j.appender.R.File=e:\console.log Uses the following syntax to set the maximum number of rollover files: log4j.appender.R.MaxBackupIndex=5 Supports the following additional options: log4j.appender.R.immediateFlush=true log4j.appender.R.Append=false | To add an appender to the root logger, use the following syntax: log4j.rootLogger.appenderRef.rolling.ref=RollingFile To define a type (and also set the name), use the following syntax: log4j.appender.rolling.type=RollingFile log4j.appender.rolling.name=RollingFile To set a pattern, use the following syntax: log4j.appender.rolling.layout.type=PatternLayout log4j.appender.rolling.layout.pattern=%d[%t]%‑5p%c‑%m%n 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. log4j.appender.rolling.policies.type=Policies log4j.appender.rolling.policies.size.type=SizeBasedTriggeringPolicy log4j.appender.rolling.policies.size.size=256MB To set a file location, use the following syntax: filePattern is a new attribute, and it defines the look of the rollover file name.log4j.appender.rolling.fileName=e:\console.log log4j.appender.rolling.filePattern=e:\%d.%i.log.gz To set the max number of rollover files, use the following syntax: log4j.appender.rolling.strategy.type=DefaultRolloverStrategy log4j.appender.rolling.strategy.max=5 Supports the following additional options: log4j.appender.rolling.immediateFlush=true log4j.appender.rolling.append=false The following code shows an example: log4j.rootLogger.appenderRef.rolling.ref=RollingFile log4j.appender.rolling.type=RollingFile log4j.appender.rolling.name=RollingFile log4j.appender.rolling.fileName=${sys:log.location} log4j.appender.rolling.filePattern=${sys:log.location}-%i.log.gz log4j.appender.rolling.layout.type=PatternLayout log4j.appender.rolling.layout.pattern="%d %-5p [%c] (%t:%x) %m%n" log4j.appender.rolling.policies.type=Policies log4j.appender.rolling.policies.size.type=SizeBasedTriggeringPolicy log4j.appender.rolling.policies.size.size=256MB log4j.appender.rolling.filter.threshold.type=ThresholdFilter log4j.appender.rolling.filter.threshold.level=ALL |
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.