0
log4j.threshold=INFO
log4j.rootCategory=DEBUG, DEFAULT, ERROR

log4j.appender.DEFAULT=org.apache.log4j.RollingFileAppender
log4j.appender.DEFAULT.File=/logs/appname/${SERVER_NAME}/log.log
log4j.appender.DEFAULT.MaxFileSize=10MB
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.DEFAULT.layout=org.apache.log4j.PatternLayout
log4j.appender.DEFAULT.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n

log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.maxFileSize=10MB
log4j.appender.ERROR.maxBackupIndex=10
log4j.appender.ERROR.File=/logs/appname/${SERVER_NAME}/logError.log
log4j.appender.ERROR.threshold=ERROR
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n

log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO

Hello. Could you please help me with migrating my log4j properties to log4j2? The first snippet of code is the original .properties, the second one is my attempt to create log4j2 .properties.

appender.DEFAULT.type = RollingFile
appender.DEFAULT.name = fileLogger1
appender.DEFAULT.fileName= /logs/appname/${SERVER_NAME}/log.log
appender.DEFAULT.layout.type = PatternLayout
appender.DEFAULT.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.DEFAULT.policies.type = Policies
appender.DEFAULT.policies.size.type = SizeBasedTriggeringPolicy
appender.DEFAULT.policies.size.size = 10MB

appender.ERROR.type = RollingFile
appender.ERROR.name = fileLogger2
appender.ERROR.fileName= /logs/appname/${SERVER_NAME}/logError.log
appender.ERROR.layout.type = PatternLayout
appender.ERROR.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.ERROR.policies.type = Policies
appender.ERROR.policies.size.type = SizeBasedTriggeringPolicy
appender.ERROR.policies.size.size = 10MB

rootLogger.appenderRefs = DEFAULT, ERROR
rootLogger.appenderRef.DEFAULT.ref = fileLogger1
rootLogger.appenderRef.ERROR.ref = fileLogger2

Is the second snippet correct? How should I implement the following lines in log4j2?

log4j.threshold=INFO 
log4j.appender.ERROR.threshold=ERROR
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.ERROR.maxBackupIndex=10
log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO
SteirA
  • 1
  • 1

2 Answers2

0

Your configuration seems alright.

The MaxBackupIndex property is not gone, but it was moved to the rollover strategy. You need to add to your appenders a DefaultRolloverStrategy with a max attribute:

appender.DEFAULT.strategy.type = DefaultRolloverStrategy
appender.DEFAULT.strategy.max = 10
appender.ERROR.strategy.type = DefaultRolloverStrategy
appender.ERROR.strategy.max = 10

The remaining properties are filters. There are many equivalent ways to add filters to components (cf. filters documentation):

  • the log4j.threshold property can be replaced by a global ThresholdFilter:

    filter.1.type = ThresholdFilter
    filter.1.level = INFO
    
  • in the same way you can add a Threshold filter to the appenders, but that can be done more efficiently by setting the level property on the AppenderRefs:

    rootLogger.appenderRef.ERROR.level = ERROR
    
  • the log4j.logger prefix is used in Log4j 1.x to configure the loggers. In Log4j 2.x you can do it like:

    logger.1.name = org.apache
    logger.1.level = INFO
    logger.2.name = org.opensaml
    logger.2.level = INFO
    
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • Thank you very much! One more question: after I added new configuration to my application, I got a message "log4j: Could not find root logger information. Is this ok?" and my logger does not work. Any idea how to solve this? I'm using log4j-api-2.17.1, log4j-core-2.17.1 and log4j-slf4j-impl-2.17.1. – SteirA Feb 28 '22 at 17:08
  • That is a Log4j 1.x message. Did you remove Log4j 1.x from the classpath? What is the fully qualified name of you `Logger` class? – Piotr P. Karwasz Feb 28 '22 at 17:29
  • I found out, that my logging initializer was still using log4j-1.2.16 library to configure Loger using PropertyConfigurator.configure(). Is it a good idea to replace this existing library with log4j-1.2-api-2.17.1 so this part of code remains compatible? Full name of my Logger class is org.slf4j.Logger.class from slf4j-api-1.7.25 library. – SteirA Feb 28 '22 at 23:09
  • Programmatic Log4j 1.x configuration is not officially supported, but you can try with version 2.17.2 of `log4j-1.2-api` and set the `log4j1.compatibility` system property to `true`. In that case remove `log4j2.properties` and use the original `log4j.properties`. (also check whether `slf4j-log4j12` is **not** on your classpath). – Piotr P. Karwasz Mar 01 '22 at 00:19
0

Not Exactly Related to this question, but if your are migrating from log4j to log4j2, i suggest reading my below stackoverflow answer. https://stackoverflow.com/a/76363764/2606228

nikhil2000
  • 178
  • 3
  • 15