SpringBoot 整合 Shiro和Log4j2
SpringBoot 整合 Shiro和Log4j2
引入POM.xml
(最新依赖 https://mvnrepository.com/ )
shiro
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.7.1</version>
</dependency>
log4j
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
log4j-slf4j-impl
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.1</version>
</dependency>
排除SpringBoot自带的logging
spring-boot的依赖中默认支持日志spring-boot-starter-logging
,需要去掉之后log4j2才可以生效,否则包冲突。
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusions>
自带的依赖会又很多携带日志,一点要全部排除:
示例
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
...
配置log4j2.xml
默认地址在resources下,示例(特别简单)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<!--<patternlayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%p] %c %m
%n"/>-->
<patternlayout pattern="🐳 [%p] %m %n"/>
</console>
</appenders>
ALL -->
<loggers>
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
如何自定义地址?
添加配置文件
application.yml
logging:
config: classpath:/mylog/mylog4j2.xml
或者
application.properties
logging.config=classpath:/mylog/mylog4j2.xml