1

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

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
log4j.debug=false

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c %x - %m%n
#log4j.appender.A1.threshold=DEBUG

# DEBUG, INFO, WARN, ERROR, FATAL
log4j.category.com.myorganization.MyClass=INFO
log4j.category.com.myorganization.MyOtherClass=WARN

I would like to migrate to log4j2 with the same configuration as above. While I have found lots of documentation, especially Migrating from log4j to log4j2 - properties file configuration none of the examples contain category configuration as I have it in the last two lines.

Please can anyone help me out how would be my log4j2.properties file with the same configuration above ?

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • 1
    [`Category`](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html) is just a deprecated version of `Logger`. Check [this question](https://stackoverflow.com/q/64368442/11748454) for an example of migration of logger configurations. – Piotr P. Karwasz Dec 14 '21 at 12:52

1 Answers1

1

Thanks to Piotr's explanation, the solution to my question looks like this:

appenders=xyz

appender.xyz.type = Console
appender.xyz.name = myOutput
appender.xyz.layout.type = PatternLayout
appender.xyz.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:SSS}] [%p] [%c:%L] - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = abc
rootLogger.appenderRef.abc.ref = myOutput

logger.c1.name=com.myorganization.MyClass
logger.c1.level=INFO
logger.c2.name=com.myorganization.MyOtherClass
logger.c2.level=WARN

The only drawback seems that the loggers now need to have names (c1, c2, whatever unique names).

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • The `*.properties` format in Log4j 2.x is quite verbose. Using the XML format you wouldn't need those unique ids (cf. [examples](https://logging.apache.org/log4j/2.x/manual/configuration.html#XML)). – Piotr P. Karwasz Dec 15 '21 at 04:12
  • Agree for log4j2. I preferred properties on log4j 1 because it was more efficient than XML. – Queeg Dec 15 '21 at 08:36