Search code examples
javalog4jlog4j2

Migrating from log4j to log4j2 - properties file configuration


I have a Java application which is using log4j configured as below.

log4j.properties:

log4j.rootLogger=INFO, R
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = /trace.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{HH:mm:ss} %c{1} [%p] %m%n

I would like to migrate to log4j2 with the same configuration as above. Haven't found anything related to log4j2 properties configuration file as this support recently included.

How would be my log4j2.properties file with the same configuration above ?


Solution

  • Here is what I constructed after going through the documentation and worked.

    rootLogger.level = INFO
    property.filename = trace.log
    appenders = R, console
    
    appender.console.type = Console
    appender.console.name = STDOUT
    appender.console.layout.type = PatternLayout
    appender.console.layout.pattern = %d %5p [%t] (%F:%L) - %m%n
    
    appender.R.type = RollingFile
    appender.R.name = File
    appender.R.fileName = ${filename}
    appender.R.filePattern = ${filename}.%d{yyyy-MM-dd}
    appender.R.layout.type = PatternLayout
    appender.R.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
    appender.R.policies.type = Policies
    appender.R.policies.time.type = TimeBasedTriggeringPolicy
    appender.R.policies.time.interval = 1
    
    rootLogger.appenderRefs = R, console
    
    rootLogger.appenderRef.console.ref = STDOUT
    rootLogger.appenderRef.R.ref = File