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 Level | intLevel | Level | Description |
| 0 | Integer.MAX_VALUE | ALL | All levels including custom levels. |
| 1 | 600 | TRACE | Finer-grained informational events than the DEBUG |
| 2 | 500 | DEBUG | Useful to debug an application |
| 3 | 400 | INFO | Highlight the progress of the application at coarse-grained level |
| 4 | 300 | WARN | Highlight potentially harmful situations. |
| 5 | 200 | ERROR | Highlight the error events that might still allow the application to continue running. |
| 6 | 100 | FATAL | Highlight very severe error events that will presumably lead the application to abort. |
| 7 | 0 | OFF | Highest 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