Enabling DEBUG only for some packages and restrict all others in log4j

In this case, first need to understand the how log level (org.apache.log4j.Level) works. There are eight log leveles defines in the framework and you can also define your custom levels by sub-classing the Level class.

Here, the “Restrictive Level” of a Logger level is determined by considering it’s “intLevel” value. Higher the intLevel, lower the Restrictive Level.

Restrictive LevelintLevelLevelDescription
0Integer.MAX_VALUE
ALLAll levels including custom levels.
1600TRACEFiner-grained informational events than the DEBUG
2500DEBUGUseful to debug an application
3400INFOHighlight the progress of the application at coarse-grained level
4300WARNHighlight potentially harmful situations.
5200ERRORHighlight the error events that might still allow the application to continue running.
6100FATALHighlight very severe error events that will presumably lead the application to abort.
70OFFHighest possible rank / restrctive level and is intended to turn off logging.

In logger configuration, root-logger is for system wide. Package level configuration can be defined under logger@category/level@name.

So, to get this done, we give more restrictive level to root-logger and give less restrictive levels to specified packages through logger@category/level@name.

eg :
com.some.package – > DEBUG
In this package all the log levels below and including DEBUG will be logged. (eg : DEBUG, INFO, WARN, ERROR etc)

com.some.another.package – > INFO
In this package all the log levels below and including INFO will be logged. (eg : INFO, WARN, ERROR etc). But DEBUGs within this packages will not be logged

com.some.unwanted.package – > ERROR
In this package all the log levels below and including ERROR will be logged. This is mostly like ERROR only (eg : ERROR, FATAL, OFF, TRACE etc)

root -> INFO
Since root level is INFO, all the log levels below and including INFO will be logged (eg : INFO, WARN, ERROR etc) but DEBUGs will not be logged unless they have not been specified above

eg :

<subsystem xmlns="urn:jboss:domain:logging:6.0">
    <console-handler name="CONSOLE">
        <formatter>
            <named-formatter name="COLOR-PATTERN"/>
        </formatter>
    </console-handler>
    <periodic-rotating-file-handler name="FILE" autoflush="true">
        <formatter>
            <named-formatter name="PATTERN"/>
        </formatter>
        <file relative-to="jboss.server.log.dir" path="server.log"/>
        <suffix value=".yyyy-MM-dd"/>
        <append value="true"/>
    </periodic-rotating-file-handler>
    <logger category="com.some.package ">
        <level name="INFO"/>
    </logger>
    <logger category="com.some.another.package">
        <level name="INFO"/>
    </logger>
    <logger category="com.some.unwanted.package ">
        <level name="ERROR"/>
    </logger>
    <root-logger>
        <level name="INFO"/>
        <handlers>
            <handler name="CONSOLE"/>
            <handler name="FILE"/>
        </handlers>
    </root-logger>
    <formatter name="PATTERN">
        <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] %s%e%n"/>
    </formatter>
    <formatter name="COLOR-PATTERN">
        <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] %s%e%n"/>
    </formatter>
</subsystem>

References :

  • https://logging.apache.org/log4j/2.x/manual/customloglevels.html
  • https://www.tutorialspoint.com/log4j/log4j_logging_levels.htm

#log4j, #logging, #wildfly