在简单的示例项目中更改Spring框架日志级别? [英] Change Spring framework log level in simple example project?

查看:258
本文介绍了在简单的示例项目中更改Spring框架日志级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当关注



由于Spring DEBUG 级别,日志很繁琐。要改变:



1)在< projectDir> /下创建资源目录src / main 就像在Maven项目中一样。
2)在其中创建一个 logback.xml 文件,其中包含:

 <结构> 

< appender name =STDOUTclass =ch.qos.logback.core.ConsoleAppender>
< encoder>
< pattern> web - %date [%thread]%-5level%logger {36} - %message%n
< / pattern>
< / encoder>
< / appender>

< logger name =org.springframeworklevel =WARN/>

< root level =INFO>
< appender-ref ref =STDOUT/>
< / root>

< / configuration>

瞧!

 创建表
为John Woo插入客户记录
为Jeff Dean插入客户记录
为Josh Bloch插入客户记录
插入Josh Long的客户记录
查询客户记录,其中first_name ='Josh':
客户[id = 3,firstName ='Josh',lastName ='Bloch']
客户[id = 4,firstName ='Josh' ,lastName ='长']


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?

解决方案

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']

这篇关于在简单的示例项目中更改Spring框架日志级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆