I am using log4j2 as my logging utility in my spring app. I want to set the log level for specific libraries/packages to something different than the root. For example, I want org.springframework
to INFO and com.google
to be WARN. I found this in the log4j2.properties
:
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
logger.rolling.name = com.test.app
logger.rolling.level = ALL
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = info
I don't understand what logger.rolling
means? I keep finding it in the log4j2 documentation but there's no explanation of what that is or what ALL
means.
How do I add log levels for specific packages and what is this rolling level stuff?
Level intLevel OFF 0 FATAL 100 ERROR 200 WARN 300 INFO 400 DEBUG 500 TRACE 600 ALL Integer.MAX_VALUE
https://logging.apache.org/log4j/2.x/manual/customloglevels.html
A log4j logger will log all events under its threshold, so if is set to ALL it will log every event since it uses the max value for its threshold.
appender.rolling.type = RollingFile appender.rolling.name = RollingFile
This is defining a new rolling file appender named RollingFile. A rolling file appender is an appender that can, for example, grow to a fixed size and then keep adding new entries while removing the oldest entries. They can also be time-based etc.
logger.rolling.name = com.test.app logger.rolling.level = ALL logger.rolling.appenderRef.rolling.ref = RollingFile
This is telling log4j to send any events from the logger named com.test.app to the aforementioned RollingFile appender. log4j will not filter out any events since the level for this logger is set to ALL. "rolling" as in "logger.rolling" is just the identifier for the logger. This is necessary because the properties file is unstructured so you need a way to distinguish which lines go together. Using the XML configuration eliminates that need
rootLogger.level = info
Any events created by other loggers that are not defined in the properties will be filtered and only INFO or below will be logged.
The documentation is here: https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
To answer your question, it depends on how the loggers are created within the application. The loggers are actually created within java, and that is where their name is assigned. The properties file only instructs log4j on how to handle each logger. You will need to add the logger to the properties file using the name defined in the class. If the class itself was used to create the logger, as apache recommends, then the name will always be the fully qualified name of the class. For example:
logger.secondclass.name = com.test.AnotherClass
logger.secondclass.level = DEBUG
logger.secondclass.appenderRef.rolling.ref = RollingFile
Now any DEBUG or below events created by the com.test.AnotherClass will also be sent to the RollingFile appender.
All of this is explained here: https://logging.apache.org/log4j/2.x/manual/architecture.html