简体   繁体   English

使用Log4J记录Spring bean初始化

[英]Logging Spring bean initialization with Log4J

When I run my application, it stops when beans are initializing but doesn't show any logs entries. 当我运行我的应用程序时,它会在bean初始化时停止,但不会显示任何日志条目。 So I don't know what happened: 所以我不知道发生了什么:

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初始化时,是否有可能在日志中获得这样的输出?

You need to set " org.springframework.beans.factory.support.DefaultListableBeanFactory " to debug level. 您需要将“ org.springframework.beans.factory.support.DefaultListableBeanFactory ”设置为调试级别。 The output looks something like this: 输出看起来像这样:

... - 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. 请记住,Spring正在使用commons-logging框架 ,因此这些行不会出现在Log4J日志中。 To redirect them use SLF4J . 重定向它们,请使用SLF4J Add slf4j-api.jar, jcl-over-slf4j.jar, slf4j-log4j12.jar and log4j.jar to your lib directory and remove commons-logging.jar from it. slf4j-api.jar,jcl-over-slf4j.jar,slf4j-log4j12.jar和log4j.jar添加到lib目录中,并从中删除commons-logging.jar。

add to your log4j xml (check if you already have one with lavel ERROR, change it either INFO or DEBUG 添加到你的log4j xml(检查你是否已经有一个lavel ERROR,改为INFO或DEBUG

<logger name="org.springframework">
        <level value="INFO" />
</logger>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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