Debugging Spring configuration

137,182

Solution 1

Yes, Spring framework logging is very detailed, You did not mention in your post, if you are already using a logging framework or not. If you are using log4j then just add spring appenders to the log4j config (i.e to log4j.xml or log4j.properties), If you are using log4j xml config you can do some thing like this

<category name="org.springframework.beans">
    <priority value="debug" />
</category>

or

<category name="org.springframework">
    <priority value="debug" />
</category>

I would advise you to test this problem in isolation using JUnit test, You can do this by using spring testing module in conjunction with Junit. If you use spring test module it will do the bulk of the work for you it loads context file based on your context config and starts container so you can just focus on testing your business logic. I have a small example here

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:springContext.xml"})
@Transactional
public class SpringDAOTest 
{
    @Autowired
    private SpringDAO dao;

    @Autowired
    private ApplicationContext appContext;

    @Test
    public void checkConfig()
    {
        AnySpringBean bean =  appContext.getBean(AnySpringBean.class);
        Assert.assertNotNull(bean);
    }
}

UPDATE

I am not advising you to change the way you load logging but try this in your dev environment, Add this snippet to your web.xml file

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

UPDATE log4j config file


I tested this on my local tomcat and it generated a lot of logging on application start up. I also want to make a correction: use debug not info as @Rayan Stewart mentioned.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{HH:mm:ss} %p [%t]:%c{3}.%M()%L - %m%n" />
        </layout>
    </appender>

    <appender name="springAppender" class="org.apache.log4j.RollingFileAppender"> 
        <param name="file" value="C:/tomcatLogs/webApp/spring-details.log" /> 
        <param name="append" value="true" /> 
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{MM/dd/yyyy HH:mm:ss}  [%t]:%c{5}.%M()%L %m%n" />
        </layout>
    </appender>

    <category name="org.springframework">
        <priority value="debug" />
    </category>

    <category name="org.springframework.beans">
        <priority value="debug" />
    </category>

    <category name="org.springframework.security">
        <priority value="debug" />
    </category>

    <category
        name="org.springframework.beans.CachedIntrospectionResults">
        <priority value="debug" />
    </category>

    <category name="org.springframework.jdbc.core">
        <priority value="debug" />
    </category>

    <category name="org.springframework.transaction.support.TransactionSynchronizationManager">
        <priority value="debug" />
    </category>

    <root>
        <priority value="debug" />
        <appender-ref ref="springAppender" />
        <!-- <appender-ref ref="STDOUT"/>  -->
    </root>
</log4j:configuration>

Solution 2

If you use Spring Boot, you can also enable a “debug” mode by starting your application with a --debug flag.

java -jar myapp.jar --debug

You can also specify debug=true in your application.properties.

When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG level.

Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio).

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

Share:
137,182
jprusakova
Author by

jprusakova

Updated on September 22, 2020

Comments

  • jprusakova
    jprusakova over 3 years

    I am working on a Java application that uses Spring and Hibernate and runs on a Websphere. I have run into a problem, where I expect Spring to load a Dao into my object, but for some reason that doesn't happen. (Another Dao that is specified in much the same way is loaded fine.)

    The question is - how can I debug how Spring decides what to load in? Can I turn on logging for Spring, and where?

  • Ryan Stewart
    Ryan Stewart over 12 years
    +1: debug level is more appropriate for tracking down problems like this, though.
  • Dan
    Dan over 12 years
    Agreed with @RyanStewart. If the problem is a missing dependency, you may need DEBUG level to find out where it's coming from.
  • jprusakova
    jprusakova over 12 years
    @PrasannaTalakanti I added these to log4j.xml: <appender name="springAppender" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="./logs/spring-details.log" /> <param name="append" value="true" /> </appender> <category name="org.springframework"> <priority value="info" /> <appender-ref ref="springAppender"/> </category> But no log file.
  • Prasanna Talakanti
    Prasanna Talakanti over 12 years
    do you see other logging?, how are you loading your log4j config either (are you using org.springframework.web.util.Log4jConfigListener)
  • jprusakova
    jprusakova over 12 years
    @PrasannaTalakanti yes, the rest of the logging configured in that file works. Where should I reference org.springframework.web.util.Log4jConfigListener ?
  • Prasanna Talakanti
    Prasanna Talakanti over 12 years
    I am not sure why you are not seeing logging, I have an update to the answer regarding Log4jCongiListener
  • jprusakova
    jprusakova over 12 years
    @PrasannaTalakanti Updated web.xml with the snippet as you suggested. Now the spring-details.log is being created, but remains empty, no logging is happening. No log4j initialization errors.
  • Prasanna Talakanti
    Prasanna Talakanti over 12 years
    I tried the same thing can you go with debug, It looks like debug is printing way more logging then info. I am also posting the complete log4j just for you reference.