Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j setting log level for individual class

Tags:

java

log4j

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());
like image 748
vico Avatar asked Oct 22 '13 09:10

vico


2 Answers

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
like image 105
Paul Vargas Avatar answered Sep 29 '22 21:09

Paul Vargas


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
like image 37
harsh Avatar answered Sep 29 '22 20:09

harsh