Change Spring framework log level in simple example project?

后端 未结 1 537
庸人自扰
庸人自扰 2020-12-09 23:18

When following this Spring example I was expecting to see output like this:

Creating tables
Inserting customer record for John Woo
Inserting customer record          


        
相关标签:
1条回答
  • 2020-12-09 23:58

    This is the works of Spring Boot that is dealing underneath with logging routing jul, jcl and log4j over slf4j and employing Logback via slf4j as you can tell by the distinguishable shortened namespace class names.

    All this is nicely visible via the IntelliJ diagram tool directly on the pom file:

    enter image description here

    This setup is following the best practise as described/depicted in the SLF4J site:

    The log is chatty because of the Spring DEBUG level. To alter that:

    1) Create a resources directory under <projectDir>/src/main as you would have in a Maven project. 2) Create a logback.xml file in it containing:

    <configuration>
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>web - %date [%thread] %-5level %logger{36} - %message%n
                </pattern>
            </encoder>
        </appender>
    
        <logger name="org.springframework" level="WARN" />
    
        <root level="INFO">
            <appender-ref ref="STDOUT" />
        </root>
    
    </configuration>
    

    and voila!

    Creating tables
    Inserting customer record for John Woo
    Inserting customer record for Jeff Dean
    Inserting customer record for Josh Bloch
    Inserting customer record for Josh Long
    Querying for customer records where first_name = 'Josh':
    Customer[id=3, firstName='Josh', lastName='Bloch']
    Customer[id=4, firstName='Josh', lastName='Long']
    
    0 讨论(0)
提交回复
热议问题