本文共 2134 字,大约阅读时间需要 7 分钟。
项目的开发中,日志是必不可少的组件,所以也会相应的在项目中实现和构建我们所需要的日志框架。
logback是Springboot推荐并且默认使用的日志系统。 官方推荐使用的xml名字的格式为:logback-spring.xml 在resource下创建logback-spring.xml文件${CONSOLE_LOG_PATTERN} UTF-8 ${LOG_PATH}/mybatis.log %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8 ${LOG_PATH}/mybatis-%d{yyyy-MM-dd}.%i.log 10MB 30
将此文件放到这篇文章项目的resource目录下,开发环境可以在application.properties添加配置
logging.level.com.example.mybatis.mapper=debug
com.example.mybatis.mapper为你的dao层/mapper层路径
TestService.java改变控制台输出方式,代码如下package com.example.mybatis.service;import com.example.mybatis.entity.Test1;import com.example.mybatis.entity.Test2;import com.example.mybatis.mapper.test1.Test1Mapper;import com.example.mybatis.mapper.test2.Test2Mapper;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class TestService { private static final Logger log = LoggerFactory.getLogger(TestService.class); @Autowired private Test1Mapper test1Mapper; @Autowired private Test2Mapper test2Mapper; public void getStudents() { Listtest1 = test1Mapper.getAll(); List test2 = test2Mapper.getAll(); log.info("数据源DB1:" + test1); log.info("数据源DB2:" + test2); }}
然后启动项目访问http://localhost:8080/test
转载地址:http://odvkk.baihongyu.com/