Log4j setting log level for individual class

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:

  • Log4j XML Configuration Primer
  • Configure Log4j (log4j.xml)
  • Log4j XML Configuration
  • Basic steps to configure Log4j using xml and properties file

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

Tags:

Java

Log4J