github地址:https://github.com/JackrayWang/springbootshiro
项目目录如下:
pom特别注意,在由spring-boot的依赖中默认添加了日志,需要去掉之后log4j2才可以生效,否则包冲突。
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<?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-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jakcray</groupId>
<artifactId>springbootshiro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootshiro</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="patternLayout">[%d{yyyy-MM-dd'T'HH:mm:ss,SSSXXX}] %-5p [%t] %c{2} - %m%n]</Property>
<!--<Property name="logdir">${sys:catalina.home}</Property>-->
<!--<Property name="logdir">/data/logs</Property>-->
<Property name="logdir">E:/logs</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${patternLayout}"/>
</Console>
<Async name="ASYNC">
<AppenderRef ref="CONSOLE"/>
</Async>
<!--<RollingFile name="DebugLogger" fileName="${logdir}/logs/debug/log.log"
filePattern="${logdir}/logs/debug/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-log-%i.log" append="true">-->
<RollingFile name="DebugLogger" fileName="${logdir}/debug/log.log"
filePattern="${logdir}/debug/log-%i.log" append="true">
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${patternLayout}"/>
<!--<SizeBasedTriggeringPolicy size="1MB"/>-->
<DefaultRolloverStrategy max="10"/>
<Policies>
<!--根据时间来滚动-->
<!--<TimeBasedTriggeringPolicy modulate="true" interval="1"/>-->
<!--根据文件大小来滚动-->
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<RollingFile name="InfoLogger" fileName="${logdir}/info/log.log"
filePattern="${logdir}/info/log-%i.log" append="true">
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${patternLayout}"/>
<!--<SizeBasedTriggeringPolicy size="1MB"/>-->
<DefaultRolloverStrategy max="10"/>
<Policies>
<!--根据时间来滚动-->
<!--<TimeBasedTriggeringPolicy modulate="true" interval="1"/>-->
<!--根据文件大小来滚动-->
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<RollingFile name="WarnLogger" fileName="${logdir}/warn/log.log"
filePattern="${logdir}/warn/log-%i.log" append="true">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${patternLayout}"/>
<!--<SizeBasedTriggeringPolicy size="1MB"/>-->
<DefaultRolloverStrategy max="10"/>
<Policies>
<!--根据时间来滚动-->
<!--<TimeBasedTriggeringPolicy modulate="true" interval="1"/>-->
<!--根据文件大小来滚动-->
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<RollingFile name="ErrorLogger" fileName="${logdir}/error/log.log"
filePattern="${logdir}/error/log-%i.log" append="true">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${patternLayout}"/>
<!--<SizeBasedTriggeringPolicy size="1MB"/>-->
<DefaultRolloverStrategy max="10"/>
<Policies>
<!--根据时间来滚动-->
<!--<TimeBasedTriggeringPolicy modulate="true" interval="1"/>-->
<!--根据文件大小来滚动-->
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
</Appenders>
<loggers>
<root level="info">
<AppenderRef ref="ASYNC"/>
<appender-ref ref="DebugLogger" level="debug"/>
<appender-ref ref="InfoLogger" level="info"/>
<appender-ref ref="WarnLogger" level="warn"/>
<appender-ref ref="ErrorLogger" level="error"/>
</root>
</loggers>
</Configuration>
pom引入log4j2.xml
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xq?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=xq
spring.datasource.password=xq123@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#mybatis.config-location=classpath:mybatis-config.xml
#mybatis mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=com.jackray.springbootshiro.mybatisutils.mapper
logging.config=classpath:log4j2/log4j2.xml
##jsp##
#spring.mvc.view.prefix=/jsp/
#spring.mvc.view.suffix=.html
controller
package com.jakcray.springbootshiro.controller;
import com.jakcray.springbootshiro.mybatisutils.dao.UserDAO;
import com.jakcray.springbootshiro.mybatisutils.mapper.UserDAOMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
UserDAOMapper userDAOMapper;
private static final Logger logger = LoggerFactory.getLogger(TestController.class.getName());
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String Test(){
UserDAO userDAO = userDAOMapper.selectByPrimaryKey("12345");
logger.info("success");
// System.out.println(userDAO.getEmail());
//
// System.out.println("success");
return "big success";
}
}
test
访问:http://localhost:8080/test
控制台打印日志
log日志文件目录
日志文件打印日志(写入文件)