问题
When following this Spring example I was expecting to see output like this:
Creating tables
Inserting customer record for John Woo
Inserting customer record for Jeff Dean
...
Instead, I got some DEBUG
log messages interspersed between every line:
Creating tables
12:31:16.474 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing SQL statement [drop table customers if exists]
12:31:16.484 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
12:31:16.484 [main] DEBUG o.s.j.d.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem]
...
These various answers seem to indicate that this can be resolved by changing the log level in my log4j.properties
file. However, in following the Spring example a log4j.properties
file is never mentioned.
Interestingly, Spring does appear to be using log4j
internally:
$ grep -R "log4j" *
Binary file build/libs/gs-relational-data-access-0.1.0.jar matches
I imagine I could use log4j
to fix this problem, but the manual doesn't seem to have information on where to put log4j.properties
or how to integrate it into this project.
How do I change the log level to remove those DEBUG
statements?
If I need to use a log4j.properties
file, where do I place it? Do I need to tie it to my build.gradle
file, or reference it in my .java
files somehow?
回答1:
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:
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']
来源:https://stackoverflow.com/questions/23252743/change-spring-framework-log-level-in-simple-example-project