SpringBoot 配置logging日志及在控制台中输出

这里以执行的SQL语句在控制台中输出为例,在Spring Boot项目中配置logging日志并输出SQL语句。

个人在最近的项目中出错频频,其中大部分后台报错都与SQLException有关,SQL异常一般又很难查到错误地方,尤其是在映射文件Mapper.xml中,找到你对应的出错的位置,如果的你的sql很简单,那还好,但是那是不可能,一般项目中涉及到的SQL语句也没有就是简单的增删改查。所以在你的项目中配置logging日志很有必要,这样就很好定位是哪条SQL语句出了问题。


在Spring Boot中,默认使用的是SLF4J+LogBack实现日志框架,这点我们从
spring-boot-starter-logging-1.5.7.RELEASE.jar里面的pom.xml文件也看得出来:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starters</artifactId>
		<version>1.5.7.RELEASE</version>
	</parent>
	<artifactId>spring-boot-starter-logging</artifactId>
	<name>Spring Boot Logging Starter</name>
	<description>Starter for logging using Logback. Default logging starter</description>
	<url>http://projects.spring.io/spring-boot/</url>
	<organization>
		<name>Pivotal Software, Inc.</name>
		<url>http://www.spring.io</url>
	</organization>
	<properties>
		<main.basedir>${
   basedir}/../..</main.basedir>
	</properties>
	<dependencies>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jul-to-slf4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
		</dependency>
	</dependencies>
</project>


既然SpringBoot已经默认帮我们配置了依赖包,我们只需要在项目的resources文件夹中间一个config文件,放入logback-spring.xml文件,这里面配置了一些日志的输出格式等等内容,我们可以根据需要进行配置。下面贴出我的logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true"
               scanPeriod="10 seconds">
    <!--继承spring boot提供的logback配置-->
    
  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值