java 使用 Log4J 记录 Spring bean 初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25285710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging Spring bean initialization with Log4J
提问by Mateusz
When I run my application, it stops when beans are initializing but doesn't show any logs entries. So I don't know what happened:
当我运行我的应用程序时,它会在 bean 初始化时停止,但不显示任何日志条目。所以我不知道发生了什么:
Log4j.properties
Log4j.properties
log4j.rootLogger=DEBUG, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
org.springframework=DEBUG
org.springframework.beans.factory.support=DEBUG
log4j.logger.org.springframework.beans.factory.support=DEBUG
log4j.logger.org.springframework.beans=DEBUG
log4j.category.org.springframework.beans.factory=DEBUG
log4j.logger.org.springframework=DEBUG
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.logger.org.hibernate.type=trace
log4j.additivity.org.hibernate.SQL=false
log4j.logger.org.hibernate.transaction=debug
log4j.logger.java.sql.Statement=DEBUG
log4j.appender.stdout.layout.ConversionPattern=%d %t %C{1}- %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${log4j.appender.R.File}
log4j.appender.R.MaxFileSize=2MB
log4j.appender.R.MaxBackupIndex=0
log4j.appender.R.Append=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %t (%l) - %m%n
I want to get something like:
我想得到类似的东西:
"BeanName" initialized
"BeanName" initialized
etc...
So then I would know where the initialization stopped. Is it possible to get such an output in the logs, when beans are initializing?
那么我就会知道初始化在哪里停止。当 bean 初始化时,是否有可能在日志中获得这样的输出?
采纳答案by Stefan
You need to set "org.springframework.beans.factory.support.DefaultListableBeanFactory" to debuglevel. The output looks something like this:
您需要将“ org.springframework.beans.factory.support.DefaultListableBeanFactory”设置为调试级别。输出如下所示:
... - Creating instance of bean ...
... - Finished creating instance of bean ...
Update:
更新:
Add this to log4j.properties:
将此添加到 log4j.properties:
log4j.logger.org.springframework.beans.factory.support.DefaultListableBeanFactory=DEBUG
Keep in mind that Spring is using the commons-logging framework, therefore these lines will not appear in your Log4J logs. To redirectthem use SLF4J. Add slf4j-api.jar, jcl-over-slf4j.jar, slf4j-log4j12.jarand log4j.jar to your lib directory and remove commons-logging.jar from it.
请记住,Spring 使用的是commons-logging 框架,因此这些行不会出现在您的 Log4J 日志中。要重定向它们,请使用SLF4J。将slf4j-api.jar、jcl-over-slf4j.jar、slf4j-log4j12.jar和 log4j.jar 添加到您的 lib 目录并从中删除 commons-logging.jar。
回答by Pravin
add to your log4j xml (check if you already have one with lavel ERROR, change it either INFO or DEBUG
添加到您的 log4j xml(检查您是否已经有一个带有 lavel 错误的,将其更改为 INFO 或 DEBUG
<logger name="org.springframework">
<level value="INFO" />
</logger>