六、日志系统

2021-03-30  本文已影响0人  honest涛

二、具体操作

2.1 初始状态

Spring使用commons-logging日志包。打印的日志是下面这样的。不用细看,截图放在这是为了和后面日志打印的情况对比。 image.png

2.2 加入 slf4j+logback

image.png
        <!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
代码不变,日志情况如下: image.png

2.3 我们主动打印的日志

把查询到的Admin对象以日志的方式打印出来,代码如下

package com.atguigu.crowd.test;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mapper.AdminMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// 指定 Spring 给 Junit 提供的运行器类
@RunWith(SpringJUnit4ClassRunner.class)
// 加载 Spring 配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class CrowdSpringTest {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private AdminMapper adminMapper;
    @Test
    public void testLog() {

        // 1.获取Logger对象,这里传入的Class对象就是当前打印日志的类
        Logger logger = LoggerFactory.getLogger(CrowdSpringTest.class);

        // 2.按照 Debug 级别打印日志
        Admin admin = adminMapper.selectByPrimaryKey(2);
        logger.debug(admin.toString());
    }
    @Test
    public void testAdminMapperAutowired() {
        Admin admin = new Admin(null,"zhangsan","123123","张三","zhangsan@163.com",null);
        int count = adminMapper.insert(admin);

        // 如果在实际开发中,所有想查看数值的地方都使用sysout方式打印,会给项目上线运行带来问题!
        // sysout本质上是一个IO操作,通常IO的操作是比较消耗性能的。如果项目中sysout很多,那么对性能的影响就比较大了。
        // 即使上线前专门花时间删除代码中的sysout,也很可能有遗漏,而且非常麻烦。
        // 而如果使用日志系统,那么通过日志级别就可以批量的控制信息的打印。
        System.out.println("受影响的行数:"+count);
    }
    @Test
    public void testDataSource() throws SQLException {
        // 1.通过数据源对象获取数据源连接
        Connection connection = dataSource.getConnection();
        // 2.打印数据库连接
        System.out.println(connection);
    }
}
image.png

这里我们看到除了 Druid 数据源打印了两条日志,Spring 和 MyBatis 并没有通
过 slf4j 打印日志。所以下一步我们要考虑的就是如何将框架所使用的日志系统统一到 slf4j。

2.4 更换框架的日志系统

排除 commons-logging
../atcrowdfunding01-admin-parent/atcrowdfunding02-admin-webui/pom.xml

        <!-- junit测试 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

../atcrowdfunding01-admin-parent/atcrowdfunding03-admin-component/pom.xml

       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

加入转换包
../atcrowdfunding01-admin-parent/atcrowdfunding03-admin-component/pom.xml

        <!-- 其他日志框架的中间转换包 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
打印效果局部: image.png

2.5 logback 配置文件

logback 工作时的具体细节可以通过 logback.xml 来配置。 image.png
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <!-- 指定日志输出的位置 -->
    <appender name="STDOUT"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- 日志输出的格式 -->
            <!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 -->
            <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
        </encoder>
    </appender>
    
    <!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR -->
    <!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 -->
    <root level="DEBUG">
        <!-- 指定打印日志的appender,这里通过“STDOUT”引用了前面配置的appender -->
        <appender-ref ref="STDOUT" />
    </root>

    <!-- 根据特殊需求指定局部日志级别 -->
    <logger name="com.atguigu.crowd.mapper" level="DEBUG"/>
    
</configuration>
image.png
上一篇 下一篇

猜你喜欢

热点阅读