Migrating custom logging configurations to Log4j 2
Step 1: Migrate logging level configurations
In the following example, we’ll migrate the MarketplaceClient
class logging level configuration.
In Log4j 1.x, the logging level configuration for the MarketplaceClient
class would look like this:
log4j.category.com.atlassian.marketplace.client.MarketplaceClient=WARN
The same configuration in Log4j 2 requires that we migrate the MarketplaceClient
category configuration to a logger under the name marketplaceClient
:
logger.marketplaceClient.level = WARN
logger.marketplaceClient.name = com.atlassian.marketplace.client.MarketplaceClient
As part of this migration, we gave the new logger the following properties:
level
defines the logging level (in this case, the logging level isWARN
)name
indicates the class we are configuring the logging level for (in this case,com.atlassian.marketplace.client.MarketplaceClient
)
Step 2: Migrate Log4j appender configurations
Appender property definitions have also changed in Log4j 2. In the following example, we’ll migrate the BambooRollingFile
appender with the same configuration as in Log4j 1.x.
This is what the BambooRollingFile
appender configuration would look like in Log4j 1.x:
#using 'bamboo home aware' appender. If the File is relative a relative Path the file goes into {bamboo.home}/logs
log4j.appender.filelog=com.atlassian.bamboo.log.BambooRollingFileAppender
log4j.appender.filelog.File=atlassian-bamboo.log
log4j.appender.filelog.MaxFileSize=100MB
log4j.appender.filelog.MaxBackupIndex=5
log4j.appender.filelog.layout=org.apache.log4j.PatternLayout
log4j.appender.filelog.layout.ConversionPattern=%d %p [%t] [%c{1}] %m%n
And this is what the configuration should look like in a Log4j 2-compatible format:
#using 'bamboo home aware' appender. If the File is relative a relative Path the file goes into {bamboo.home}/logs
appender.filelog.type = BambooRollingFile
appender.filelog.name = filelog
appender.filelog.fileName = atlassian-bamboo.log
appender.filelog.filePattern = atlassian-bamboo.log.%i
appender.filelog.layout.type = PatternLayout
appender.filelog.layout.pattern = %d{DEFAULT} %p [%t] [%C{1}] %m%n
appender.filelog.policies.type = Policies
appender.filelog.policies.size.type = SizeBasedTriggeringPolicy
appender.filelog.policies.size.size = 100MB
appender.filelog.strategy.type = DefaultRolloverStrategy
appender.filelog.strategy.max = 5
appender.filelog.strategy.fileIndex = min
Step 3: Migrate custom logging configurations on remote agents
If you’re using custom logging configurations on your remote agents, migrate them to the Log4j 2-compatible format by applying the previous steps, and then change the remote agents' configuration parameters in wrapper.conf
.
Under Log4j 1.x, the wrapper.conf
file would point to the log4j.properties
file by means of the log4j.configuration
parameter:
wrapper.java.additional.4=-Dlog4j.configuration=/path/to/log4j.properties
Now, the parameter is called log4j2.configurationFile
, so to make this work with Log4j 2, adjust the parameter name and value:
wrapper.java.additional.4=-Dlog4j2.configurationFile=/path/to/log4j2.properties
If your custom configuration has few changes over the original file, you might find it easier to start with a clean log4j2.properties
file and customize it accordingly. Simply use the default Log4j 2 configuration file as a template, and migrate your custom configurations from the old properties file to the new one.
Once you’ve completed all the steps above, your custom configuration should be perfectly compatible with Log4j 2.