简体   繁体   中英

log4j, foreign logging

When my logger is set to "all", i am seeing messages that my code does not explicitly place. I am using a jar a friend of mine gave me to do some things (and i suspect he is logging stuff himself)

I would like to ONLY log stuff I ask to be logged by issuing the

logger.info ("something clever"); command

Below is my log4j.properties

please advise.

# ***** Set root logger level to WARN and its two appenders to stdout and R.
log4j.rootLogger=all, R

# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log/something.log
# ***** Max file size is set to 100KB
log4j.appender.R.MaxFileSize=100KB
# ***** Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Take a look at this excellent log4j cheatsheet:

Basically, you need to set something like this:

log4j.rootCategory=error, R
log4j.category.com.your.package=debug

or, alternatively:

log4j.category.com.your.friends.package=error

Replace the package names as necessary.

These would make the root logger set to error (so any package - including other libraries you might be using, like Hibernate, Spring, etc.) will not log anything low-level (debug, info, warn), but log only errors.

It will also set your package (include your top level package or packages) to log on a debug level, so all your loggers will log normally. It will also set your friend's package to error, so it doesn't output anything.

If you have a common root package, just use it. Eg if you have packages:

  • com.example
  • com.example.a
  • com.example.a.aa
  • com.example.b
  • com.example.cd

just include com.example and it will inherit for the packages below by default.

You could, of course, override it, eg specify:

  • com.example - warn
  • com.example.a.aa - debug

or something similar. Take a look here for a detailed explanation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM