'How to configure log4j2.properties file to create logs for specific package

Below is my project structure:

src->main->java->com->dot->filter->here are many folders (ax,by,cz) which contain the classes

src->main->java->resources->log4j2.properties

Below is my log4j2.properties file:

status = error
name = PropertiesConfig
 
property.filename = C:\\logs\\debug.log
 
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
 
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:\\logs\\PreviousLogs\\-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
 
loggers = rolling
  
logger.rolling.name = com.dite.filter.ax

logger.rolling.level = debug
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref = RollingFile

for the above configuration I couldn't generate the logs

is there anything wrong in this line logger.rolling.name = com.dite.filter.ax?

could any one please tell me what mistake I have done in this configuration?

or

suggest me other way to generate logs for above specified package structure



Solution 1:[1]

You configuration of the com.dite.filter.ax logger is correct, but you didn't configure the root logger.

If you want to ignore all log messages that do not come from com.dite.filter.ax and its descendants use:

rootLogger.level = OFF

Solution 2:[2]

#------------------------------------------------
# Package logger option
#------------------------------------------------

log4j.logger.com.newpackage=DEBUG, packageLogger
log4j.additivity.com.newpackage=false

log4j.appender.packageLogger=org.apache.log4j.RollingFileAppender
log4j.appender.packageLogger.File=C:\\log\\separatepackage.log
log4j.appender.packageLogger.MaxFileSize=1MB
log4j.appender.packageLogger.MaxBackupIndex=5
log4j.appender.packageLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.packageLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Log4j + Separate Logger for specific classes or package

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piotr P. Karwasz
Solution 2 Eskandar Abedini