How can I achieve the following using log4j:
- log only events coming from a specific category , i.e.
com.example.app
but notcom.example.app.context
orcom.example.dao
; - log all events with a level of
WARN
or higher.
How can I achieve the following using log4j:
com.example.app
but not com.example.app.context
or com.example.dao
;WARN
or higher.You need to set up the catergory filters in order with the appender threshold set to the higer filter limit.
<appender name="LOG_FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="log_file.log" />
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<category name="com.example.app">
<priority value="DEBUG" />
<appender-ref ref="LOG_FILE" />
</category>
<category name="com.example.app.context">
<priority value="WARN" />
<appender-ref ref="LOG_FILE" />
</category>
In your log4j.properties file you can set the global log level for your app on the rootLogger:
log4j.rootLogger=DEBUG, APPENDER
You can use WARN, INFO, ERROR and FATAL instead of DEBUG.
A package with subpackages can be given its own log level like so:
log4j.logger.com.example.app=DEBUG
to remove logging from its sub-packages, set their loglevel to error or fatal:
#log4j.logger.com.example.app.context=ERROR
log4j.logger.com.example.app.dao=ERROR
Edit: commented out the app.context
line so that it inherits the DEBUG level from its parent package.