参考资料
log4jdbc数据库访问日志框架使用(开源中国)
https://my.oschina.net/cloudcoder/blog/420510
log4jdbc使用介绍
https://blog.csdn.net/u013409283/article/details/79219858
LogBack 实例
https://my.oschina.net/u/943316/blog/802531
mybatis中对logback.xml的使用详解(对logback.xml的详解)
http://www.cnblogs.com/jirglt/archive/2012/10/19/2731456.html
Logback.xml模板
https://www.cnblogs.com/light-zhang/p/8744647.html
Spring boot 使用logback+log4jdbc打印sql日志
https://blog.csdn.net/zj7321/article/details/83144980
使用Spring Boot进行开发时,我们为了调试方便,都需要输出sql语句,简单的日志打印方式,只能显示sql语句,并不能显示具体的占位符的内容,这篇文章主要讲解如何使用logback+log4jdbc打印sql日志并显示占位符内容
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.example
logback-log4jdbc-jpa-demo
0.0.1-SNAPSHOT
logback-log4jdbc-jpa-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: ehealth_user
password: FK7sJp92XF
url: jdbc:mysql://47.104.108.103:3309/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
jpa:
show-sql: true
spring-jpa-show-sql: true 用于输出sql语句,默认为false,使用log4jdbc后可不加此配置
${PATTERN_CONSOLE}
${PATH}/${PROJECT}/${PROJECT}-error.log
${PATTERN_FILE}
ERROR
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-error.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-warn.log
${PATTERN_FILE}
WARN
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-warn.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-debug.log
${PATTERN_FILE}
DEBUG
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-debug.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-info.log
${PATTERN_FILE}
INFO
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-info.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
package com.example.domain;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:14
* @Description:
*/
@Data
@Entity
@Table(name = "menu")
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Date createTime;
private String iFrame;
private String name;
private String component;
private String pid;
private String sort;
private String icon;
private String path;
}
package com.example.repository;
import com.example.domain.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:20
* @Description:
*/
public interface MenuRepository extends JpaRepository
package com.example.controller;
import com.example.domain.Menu;
import com.example.repository.MenuRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:21
* @Description:
*/
@RestController
@Slf4j
public class MenuController {
@Autowired
MenuRepository menuRepository;
@RequestMapping("/all")
public Menu all() {
return menuRepository.findById(10L).get();
}
}
控制台输出如下:
image
可见这种sql输出并不方便于我们的开发
基于上面的项目进行修改
org.bgee.log4jdbc-log4j2
log4jdbc-log4j2-jdbc4.1
1.16
spring:
datasource:
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
username: ehealth_user
password: FK7sJp92XF
url: jdbc:log4jdbc:mysql://47.104.108.103:3309/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
driver-class-name 使用 net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url 中加入log4jdbc
不需要配置spring-jpa-show-sql
${PATTERN_CONSOLE}
${PATH}/${PROJECT}/${PROJECT}-error.log
${PATTERN_FILE}
ERROR
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-error.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-warn.log
${PATTERN_FILE}
WARN
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-warn.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-debug.log
${PATTERN_FILE}
DEBUG
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-debug.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-info.log
${PATTERN_FILE}
INFO
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-info.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
主要加入了监控sql文件的输出
对于log4jdbc的相关配置可参考顶部参考资料
# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
控制台输出如下
image
至此,使用log4jdbc打印sql输出已完成,关于log4jdbc的更多使用可参考参考资料进行学习
项目用到的menu数据库表文件
参考资料
log4jdbc数据库访问日志框架使用(开源中国)
https://my.oschina.net/cloudcoder/blog/420510
log4jdbc使用介绍
https://blog.csdn.net/u013409283/article/details/79219858
LogBack 实例
https://my.oschina.net/u/943316/blog/802531
mybatis中对logback.xml的使用详解(对logback.xml的详解)
http://www.cnblogs.com/jirglt/archive/2012/10/19/2731456.html
Logback.xml模板
https://www.cnblogs.com/light-zhang/p/8744647.html
Spring boot 使用logback+log4jdbc打印sql日志
https://blog.csdn.net/zj7321/article/details/83144980
使用Spring Boot进行开发时,我们为了调试方便,都需要输出sql语句,简单的日志打印方式,只能显示sql语句,并不能显示具体的占位符的内容,这篇文章主要讲解如何使用logback+log4jdbc打印sql日志并显示占位符内容
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.example
logback-log4jdbc-jpa-demo
0.0.1-SNAPSHOT
logback-log4jdbc-jpa-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
org.springframework.boot
spring-boot-maven-plugin
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: ehealth_user
password: FK7sJp92XF
url: jdbc:mysql://47.104.108.103:3309/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
jpa:
show-sql: true
spring-jpa-show-sql: true 用于输出sql语句,默认为false,使用log4jdbc后可不加此配置
${PATTERN_CONSOLE}
${PATH}/${PROJECT}/${PROJECT}-error.log
${PATTERN_FILE}
ERROR
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-error.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-warn.log
${PATTERN_FILE}
WARN
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-warn.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-debug.log
${PATTERN_FILE}
DEBUG
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-debug.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-info.log
${PATTERN_FILE}
INFO
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-info.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
package com.example.domain;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:14
* @Description:
*/
@Data
@Entity
@Table(name = "menu")
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Date createTime;
private String iFrame;
private String name;
private String component;
private String pid;
private String sort;
private String icon;
private String path;
}
package com.example.repository;
import com.example.domain.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:20
* @Description:
*/
public interface MenuRepository extends JpaRepository
package com.example.controller;
import com.example.domain.Menu;
import com.example.repository.MenuRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author: jiangweifan
* @Date: 2019/3/19 14:21
* @Description:
*/
@RestController
@Slf4j
public class MenuController {
@Autowired
MenuRepository menuRepository;
@RequestMapping("/all")
public Menu all() {
return menuRepository.findById(10L).get();
}
}
控制台输出如下:
image
可见这种sql输出并不方便于我们的开发
基于上面的项目进行修改
org.bgee.log4jdbc-log4j2
log4jdbc-log4j2-jdbc4.1
1.16
spring:
datasource:
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
username: ehealth_user
password: FK7sJp92XF
url: jdbc:log4jdbc:mysql://47.104.108.103:3309/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
driver-class-name 使用 net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url 中加入log4jdbc
不需要配置spring-jpa-show-sql
${PATTERN_CONSOLE}
${PATH}/${PROJECT}/${PROJECT}-error.log
${PATTERN_FILE}
ERROR
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-error.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-warn.log
${PATTERN_FILE}
WARN
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-warn.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-debug.log
${PATTERN_FILE}
DEBUG
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-debug.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
${PATH}/${PROJECT}/${PROJECT}-info.log
${PATTERN_FILE}
INFO
ACCEPT
DENY
${PATH}/${PROJECT}/${PROJECT}-info.%d.%i.log.zip
${MAXHISTORY}
${FILESIZE}
主要加入了监控sql文件的输出
对于log4jdbc的相关配置可参考顶部参考资料
# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
控制台输出如下
image
至此,使用log4jdbc打印sql输出已完成,关于log4jdbc的更多使用可参考参考资料进行学习
项目用到的menu数据库表文件
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for menu
-- ----------------------------
DROP TABLE IF EXISTS `menu`;
CREATE TABLE `menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`create_time` datetime DEFAULT NULL COMMENT '创建日期',
`i_frame` bit(1) DEFAULT NULL COMMENT '是否外链',
`name` varchar(255) DEFAULT NULL COMMENT '菜单名称',
`component` varchar(255) DEFAULT NULL COMMENT '组件',
`pid` bigint(20) NOT NULL COMMENT '上级菜单ID',
`sort` bigint(20) NOT NULL COMMENT '排序',
`icon` varchar(255) DEFAULT NULL COMMENT '图标',
`path` varchar(255) DEFAULT NULL COMMENT '链接地址',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
-- ----------------------------
-- Records of menu
-- ----------------------------
INSERT INTO `menu` VALUES ('1', '2018-12-18 15:11:29', '\0', '系统管理', null, '0', '1', 'system', 'system');
INSERT INTO `menu` VALUES ('2', '2018-12-18 15:14:44', '\0', '用户管理', 'system/user/index', '1', '2', 'peoples', 'user');
INSERT INTO `menu` VALUES ('3', '2018-12-18 15:16:07', '\0', '角色管理', 'system/role/index', '1', '3', 'role', 'role');
INSERT INTO `menu` VALUES ('4', '2018-12-18 15:16:45', '\0', '权限管理', 'system/permission/index', '1', '4', 'permission', 'permission');
INSERT INTO `menu` VALUES ('5', '2018-12-18 15:17:28', '\0', '菜单管理', 'system/menu/index', '1', '5', 'menu', 'menu');
INSERT INTO `menu` VALUES ('6', '2018-12-18 15:17:48', '\0', '系统监控', null, '0', '10', 'monitor', 'monitor');
INSERT INTO `menu` VALUES ('7', '2018-12-18 15:18:26', '\0', '操作日志', 'monitor/log/index', '6', '11', 'log', 'logs');
INSERT INTO `menu` VALUES ('8', '2018-12-18 15:19:01', '\0', '系统缓存', 'monitor/redis/index', '6', '13', 'redis', 'redis');
INSERT INTO `menu` VALUES ('9', '2018-12-18 15:19:34', '', 'SQL监控', null, '6', '14', 'sqlMonitor', 'http://api.auauz.net/druid');
INSERT INTO `menu` VALUES ('10', '2018-12-19 13:38:16', '\0', '组件管理', null, '0', '50', 'zujian', 'components');
INSERT INTO `menu` VALUES ('11', '2018-12-19 13:38:49', '\0', '图标库', 'components/IconSelect', '10', '51', 'icon', 'icon');
INSERT INTO `menu` VALUES ('12', '2018-12-24 20:37:35', '\0', '实时控制台', 'monitor/log/msg', '6', '15', 'codeConsole', 'msg');
INSERT INTO `menu` VALUES ('13', '2018-12-27 10:11:26', '\0', '三方工具', '', '0', '30', 'tools', 'tools');
INSERT INTO `menu` VALUES ('14', '2018-12-27 10:13:09', '\0', '邮件工具', 'tools/email/index', '13', '31', 'email', 'email');
INSERT INTO `menu` VALUES ('15', '2018-12-27 11:58:25', '\0', '富文本', 'components/Editor', '10', '52', 'fwb', 'tinymce');
INSERT INTO `menu` VALUES ('16', '2018-12-28 09:36:53', '\0', 'SM.MS图床', 'tools/picture/index', '13', '32', 'image', 'pictures');
INSERT INTO `menu` VALUES ('17', '2018-12-28 15:09:49', '', '项目地址', '', '0', '0', 'github', 'https://github.com/elunez/eladmin');
INSERT INTO `menu` VALUES ('18', '2018-12-31 11:12:15', '\0', '七牛云存储', 'tools/qiniu/index', '13', '33', 'qiniu', 'qiniu');
INSERT INTO `menu` VALUES ('19', '2018-12-31 14:52:38', '\0', '支付宝工具', 'tools/aliPay/index', '13', '34', 'alipay', 'aliPay');
INSERT INTO `menu` VALUES ('21', '2019-01-04 16:22:03', '\0', '多级菜单', '', '0', '900', 'menu', 'nested');
INSERT INTO `menu` VALUES ('22', '2019-01-04 16:23:29', '\0', '二级菜单1', 'nested/menu1/index', '21', '999', 'menu', 'menu1');
INSERT INTO `menu` VALUES ('23', '2019-01-04 16:23:57', '\0', '二级菜单2', 'nested/menu2/index', '21', '999', 'menu', 'menu2');
INSERT INTO `menu` VALUES ('24', '2019-01-04 16:24:48', '\0', '三级菜单1', 'nested/menu1/menu1-1', '22', '999', 'menu', 'menu1-1');
INSERT INTO `menu` VALUES ('27', '2019-01-07 17:27:32', '\0', '三级菜单2', 'nested/menu1/menu1-2', '22', '999', 'menu', 'menu1-2');
INSERT INTO `menu` VALUES ('28', '2019-01-07 20:34:40', '\0', '定时任务', 'system/timing/index', '1', '6', 'timing', 'timing');
INSERT INTO `menu` VALUES ('30', '2019-01-11 15:45:55', '\0', '代码生成', 'generator/index', '1', '8', 'dev', 'generator');
INSERT INTO `menu` VALUES ('32', '2019-01-13 13:49:03', '\0', '异常日志', 'monitor/log/errorLog', '6', '12', 'error', 'errorLog');
INSERT INTO `menu` VALUES ('33', '2019-03-08 13:46:44', '\0', 'Markdown', 'components/MarkDown', '10', '53', 'markdown', 'markdown');
INSERT INTO `menu` VALUES ('34', '2019-03-08 15:49:40', '\0', 'Yaml编辑器', 'components/YamlEdit', '10', '54', 'dev', 'yaml');
欢迎关注公众号,后续文章更新通知,一起讨论技术问题 。