Stop displaying logger output to console from dependencies

半城伤御伤魂 提交于 2021-02-08 06:21:37

问题


I am new to log4j hence asking SOs help.

In my Java project I have couple of Maven dependencies which clutter the console output with redundant log info. I want to disable such logging.

I understand setting additivity property to false might help. But could not use it properly.

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


回答1:


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



回答2:


It looks like the log4j2.xml was overriding all other config. As of now I have switched that dependency off. Maybe log4j2 > log4j hence the issue. Also xml > properties as I have seen somewhere.



来源:https://stackoverflow.com/questions/26280337/stop-displaying-logger-output-to-console-from-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!