Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop displaying logger output to console from dependencies

I have a few Maven dependencies in my Java project that clutter the console output with redundant log info. I want to disable such logging.

Setting the additivity property to false might help. But could not use it properly.

I am looking for a log4j.xml config that will only print log output (warn, error, ...) from my project and not from any dependencies.

like image 307
Saikat Avatar asked Oct 16 '25 14:10

Saikat


2 Answers

Redirect all the third party lib logs in a target appender, use another appender for your app

log4j.rootLogger=debug,thirdPartyLibAppender 
log4j.logger.com.yourapp=debug, yourAppAppender
log4j.additivity.com.yourapp=false

# define where do you want third party lib logs to land : in a file
log4j.appender.thirdPartyLibAppender=org.apache.log4j.FileAppender
log4j.appender.thirdPartyLibAppender.append=true
log4j.appender.thirdPartyLibAppender.file=/tmp/app.log
log4j.appender.thirdPartyLibAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.thirdPartyLibAppender.layout.ConversionPattern=[%p] %c:%m%n

# define where do you want your app logs to land : stdout
log4j.appender.yourAppAppender=org.apache.log4j.ConsoleAppender
log4j.appender.yourAppAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.yourAppAppender.layout.ConversionPattern=[%p] %c:%m%n

Setting additivity to false will prevent that your app logs end in the thirdPartyLibAppender

In those 2 lines, don't forget to replace com.yourapp by the top level package name

log4j.logger.com.yourapp=debug, yourAppAppender
log4j.additivity.com.yourapp=false
like image 63
ToYonos Avatar answered Oct 19 '25 02:10

ToYonos


It looks like the log4j2.xml was overriding all other configs. As of now, I have switched that dependency off. Maybe log4j2 > log4j hence the issue. Also, XML gets a higher priority over properties as I have seen somewhere.

like image 21
Saikat Avatar answered Oct 19 '25 04:10

Saikat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!