I have package mypack and MyClass in this package. I would like to log just INFO level for this class and trying to set it using log4j.properties:
log4j.debug=true
log4j.rootLogger=ALL, debugLogfile
log4j.rootCategory=, debugLogFile
#log4j.category.mypack =INFO
log4j.logger.mypack =INFO
log4j.appender.debugLogfile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogfile.File=mylog.log
log4j.appender.debugLogfile.Threshold=ALL
log4j.appender.debugLogfile.MaxFileSize=100MB
log4j.appender.debugLogfile.MaxBackupIndex=4
log4j.appender.debugLogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLogfile.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
Unfortunately class logs DEBUG level also. What is wrong?
I create logger in this way:
public final Logger log = Logger.getLogger(getClass());
You could also choose to use the XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC
"-//log4j/log4j Configuration//EN" "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="ALL" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
</appender>
<!-- categories -->
<category name="org.hibernate">
<priority value="WARN" />
</category>
<category name="org.hibernate.type">
<priority value="TRACE" />
</category>
<category name="mypack">
<priority value="INFO" />
</category>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
With your settings, the appender to the file:
<!-- file -->
<appender name="ROLLOUT" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="mylog.log" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="4" />
<param name="threshold" value="ALL" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%d %-4r [%t] %-5p %c %x - %m%n" />
</layout>
</appender>
See also:
What you need is to set package
logger
access at Category
or Logger
level, to achive this try following :
# make default and debugLogFile both as root category
log4j.rootCategory=, debugLogFile
# set package wide logger Level (via Category, older way)
log4j.category.mypack=INFO
# OR set package wide logger Level (via Logger, newer way)
log4j.logger.mypack=INFO
# an example of turning all apache package logs to WARN
log4j.category.org.apache=WARN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With