Robert Munteanu
Robert Munteanu

Reputation: 68328

Trimming down log output using log4j

How can I achieve the following using log4j:

Upvotes: 1

Views: 1484

Answers (2)

rsp
rsp

Reputation: 23373

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.

Upvotes: 2

Antz
Antz

Reputation: 281

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>

Upvotes: 2

Related Questions