简体   繁体   中英

silencing log4j

I'm making use of a library that outputs to the console using log4j. However, the method that makes use of the library gets called many times and this means that the console is always full. I need a way of stopping the log4j outputting to the console.

I have seen a similar question but could not work out what to do.

I think (if I have understood) that there is a method or variable in log4j that I have to reference but have know idea what exactly. Also, what do I import to access log4j?

Thanks a lot for any help!

Just realised: the output looks like it could be stopped by fixing something? Please check below.

Output:

log4j:WARN No appenders could be found for logger (eu.medsea.mimeutil.TextMimeDetector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

You can change the level log to ERROR for instance to limit the amount of log you have.

Usually the log level is located in the log4j.properties file.

Ex:

# Root logger option
log4j.rootLogger=ERROR

just realised when running in the terminal it works fine (rather than running in Eclipse). Guessing that means that it cannot find the file mentioned in your answers. Cheers.

The message WARN Please initialize the log4j system properly means (as stated above) that log4j is not initialized properly because your application can't find the lo4j.properties file at runtime.

You can get a basic version of this file here: log4j sample configuration file (properties file)

Just put it in your classpath. After we have done this you'll be able to configure the logging level as you want. For example, to turn off all messagges (and keep only actual error messages), in the lo4j.properties file you can set the root logger level to error, as explained by TheEwook .

If you still have the problem then you should check your build procedure, but the specific check depends by the tool you use to compile. For example, if you use Eclipse, usually the lo4j.properties should be located in the resources directory, which should be set a source folder. If you use Maven, usually such file should be located in the src/main/resources directory. If you use Ant, probably you need to copy such file using a Ant's task.

In other words, regardless of your build procedure, make sure that the lo4j.properties is made visible in the runtime classpath.

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