How to exclude a package from a log4j appender

2020-04-04 08:53发布

问题:

I have configuration file (log4j.properties), see the packages below and and question at the end.

  com.bitguiders.package1
  com.bitguiders.package2
  com.bitguiders.package3
  com.bitguiders.package4
  com.bitguiders.package5
  com.bitguiders.package6
  com.bitguiders.package7

I want to log everything except this package

 com.bitguiders.package2 

How can i do that ? (Assume i have 50 packages and still i wanted to exclude one or two pacakges)

回答1:

You can set the log Level to OFF for some packages:

# set the log level 
log4j.logger.com.bitguiders=INFO
log4j.logger.com.bitguiders.package2=OFF
log4j.logger.com.bitguiders.packageX=OFF

Edit:

You can log to a file using a FileAppender, like:

log4j.rootLogger=INFO, FILE

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/somepath/somefile.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=[%p] %d %c %M - %m%n

Edit:

In order to send log from different packages to different files:

log4j.rootLogger=WARN, FILEA, FILEB
log4j.logger.com.bitguiders.package1=INFO, FILEA
log4j.logger.com.bitguiders.package2=INFO, FILEA
log4j.logger.com.bitguiders.package3=INFO, FILEB

This was answered here: Using Log4J 1.*, how can I write two packages to two separate files?