pom.xml
Spring Boot 默认的日志框架是logback,改用log4j2的话,需要改写pom,web项目中需要排除spring-boot-starter-logging,再自行添加spring-boot-starter-log4j2依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除自带的logback -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 日志框架改用log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
application.yml
自定义log4j2配置的话,需要在yml中指定配置文件
logging:
config: classpath:log4j2-spring-dev.xml
log4j2-spring-dev.xml
在resources目录下,新建log4j2-spring-dev.xml。不同环境,建议使用不一样的配置文件log4j2-spring-{profile}.xml
开发环境可能只需要打印到控制台即可,而生产环境日志则需要打印到日志
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN" monitorInterval="30">
<Properties>
<!-- 日志文件保存目录 -->
<Property name="LOG_HOME">./logs</Property>
<!-- 控制台日志格式(带颜色) -->
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
<!-- 普通日志格式 -->
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40c{1.} : %m%n%xwEx</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}"/>
</Console>
<!-- 每天生成一个日志文件 -->
<RollingFile name="dailyLog" fileName="${LOG_HOME}/yclouds.log" filePattern="${LOG_HOME}/yclouds-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="warn">
<!-- 输出到控制台 -->
<AppenderRef ref="console"/>
<!-- 输出到文件 -->
<!--<AppenderRef ref="dailyLog"/>-->
</Root>
<!-- 指定包com.yclouds日志级别为debug,其余包日志级别用Root的warn -->
<logger name="com.yclouds" level="debug"/>
</Loggers>
</configuration>
到此,springboot集成log4j2大功告成。