vico
vico

Reputation: 18175

Log4j setting log level for individual class

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());

Upvotes: 7

Views: 21379

Answers (5)

Paul Vargas
Paul Vargas

Reputation: 42010

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:

Upvotes: 9

richardtz
richardtz

Reputation: 4993

try adding this :

log4j.logger.mypack =INFO, debugLogfile
log4j.additivity.mypack = false
log4j.rootLogger=ALL, debugLogfile

Your class is using the config for the rootLogger and also its own. By adding the additivity parameter, you will avoid it.

Hope it helps.

Upvotes: 0

Pratik Shelar
Pratik Shelar

Reputation: 3214

As far as i remember you can specify it like this

log4j.logger.com.proj.eqd.exo.http.filter=INFO

Upvotes: 0

Oleksandr Tarasenko
Oleksandr Tarasenko

Reputation: 1454

Try

log4j.rootLogger=DEBUG, stdoutdebugLogfile
log4j.logger.mypack = INFO

instead of

log4j.appender.debugLogfile.mypack =INFO

To set DEBUG level for whole app and INFO for 'mypack' package.

Upvotes: 0

harsh
harsh

Reputation: 7692

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

Upvotes: 6

Related Questions